class Hobo::Model::Lifecycles::Creator

Public Instance Methods

allowed?(user) click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 15
def allowed?(user)
  record = lifecycle.model.new
  record.with_acting_user(user) { can_run?(record) }
end
candidate(user, attributes=nil) click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 21
def candidate(user, attributes=nil)
  record = lifecycle.model.new
  record.with_acting_user(user) { prepare!(record, attributes) }
  record.exempt_from_edit_checks = true
  record
end
change_state(record) click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 43
def change_state(record)
  state = get_state(record, options[:become])
  record.lifecycle.become state if state
end
extract_attributes(attributes) click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 29
def extract_attributes(attributes)
  model = lifecycle.model
  params = options.fetch(:params, [])
  allowed = params.dup
  params.each do |p|
    if (refl = model.reflections[p.to_s]) && refl.macro == :belongs_to
      allowed << refl.foreign_key.to_s
      allowed << refl.options[:foreign_type] if refl.options[:polymorphic]
    end
  end
  attributes & allowed
end
parameters() click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 67
def parameters
  options[:params] || []
end
run!(user, attributes) click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 49
def run!(user, attributes)
  record = lifecycle.model.new
  record.set_creator user
  record.lifecycle.active_step = self
  record.with_acting_user(user) do
    prepare!(record, attributes)
    if can_run?(record)
      if change_state(record)
        fire_event(record, on_create)
      end
      record
    else
      raise Hobo::PermissionDeniedError
    end
  end
end

Public Class Methods

new(*args) click to toggle source
# File lib/hobo/model/lifecycles/creator.rb, line 7
def initialize(*args)
  super
  self.name = name.to_sym
  lifecycle.creators[name] = self
end