Class: Hobo::Model::Lifecycles::Creator
- Inherits:
-
Struct
- Object
- Struct
- Hobo::Model::Lifecycles::Creator
- Includes:
- Actions
- Defined in:
- lib/hobo/model/lifecycles/creator.rb
Instance Attribute Summary collapse
-
#lifecycle ⇒ Object
Returns the value of attribute lifecycle.
-
#name ⇒ Object
Returns the value of attribute name.
-
#on_create ⇒ Object
Returns the value of attribute on_create.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #allowed?(user) ⇒ Boolean
- #candidate(user, attributes = nil) ⇒ Object
- #change_state(record) ⇒ Object
- #extract_attributes(attributes) ⇒ Object
-
#initialize(*args) ⇒ Creator
constructor
A new instance of Creator.
- #parameters ⇒ Object
- #run!(user, attributes) ⇒ Object
Methods included from Actions
#acting_user_is?, #apply_user_becomes!, #available_to, #available_to_acting_user?, #can_run?, #fire_event, #get_state, #guard_ok?, #prepare!, #publishable?, #publishable_by, #routable_for?, #run_hook
Constructor Details
#initialize(*args) ⇒ Creator
Returns a new instance of Creator.
7 8 9 10 11 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 7 def initialize(*args) super self.name = name.to_sym lifecycle.creators[name] = self end |
Instance Attribute Details
#lifecycle ⇒ Object
Returns the value of attribute lifecycle
5 6 7 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 5 def lifecycle @lifecycle end |
#name ⇒ Object
Returns the value of attribute name
5 6 7 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 5 def name @name end |
#on_create ⇒ Object
Returns the value of attribute on_create
5 6 7 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 5 def on_create @on_create end |
#options ⇒ Object
Returns the value of attribute options
5 6 7 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 5 def @options end |
Instance Method Details
#allowed?(user) ⇒ Boolean
15 16 17 18 |
# 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) ⇒ Object
21 22 23 24 25 26 |
# 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) ⇒ Object
43 44 45 46 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 43 def change_state(record) state = get_state(record, [:become]) record.lifecycle.become state if state end |
#extract_attributes(attributes) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 29 def extract_attributes(attributes) model = lifecycle.model params = .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.[:foreign_type] if refl.[:polymorphic] end end attributes & allowed end |
#parameters ⇒ Object
67 68 69 |
# File 'lib/hobo/model/lifecycles/creator.rb', line 67 def parameters [:params] || [] end |
#run!(user, attributes) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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 |