tablatom / hobo

The web app builder for Rails

hobo / hobo / lib / hobo / lifecycles / creator.rb
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 1 module Hobo
2 module Lifecycles
db40016d » tablatom 2008-06-13 Removing all trailing white... 3
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 4 class Creator < Struct.new(:lifecycle, :name, :on_create, :options)
db40016d » tablatom 2008-06-13 Removing all trailing white... 5
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 6 def initialize(*args)
7 super
4df7f93f » tablatom 2008-11-12 Lifecycles -- state, creato... 8 self.name = name.to_sym
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 9 lifecycle.creators[name] = self
10 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 11
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 12 include Actions
db40016d » tablatom 2008-06-13 Removing all trailing white... 13
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 14 def allowed?(user)
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 15 record = lifecycle.model.new
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 16 record.with_acting_user(user) { can_run?(record) }
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 17 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 18
19
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 20 def candidate(user, attributes=nil)
21 record = lifecycle.model.new
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 22 record.with_acting_user(user) { prepare!(record, attributes) }
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 23 record.exempt_from_edit_checks = true
24 record
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 25 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 26
27
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 28 def extract_attributes(attributes)
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 29 model = lifecycle.model
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 30 params = options.fetch(:params, [])
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 31 allowed = params.dup
32 params.each do |p|
33 if (refl = model.reflections[p]) && refl.macro == :belongs_to
34 allowed << refl.primary_key_name.to_s
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 35 allowed << refl.options[:foreign_type] if refl.options[:polymorphic]
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 36 end
37 end
38 attributes & allowed
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 39 end
40
db40016d » tablatom 2008-06-13 Removing all trailing white... 41
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 42 def change_state(record)
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 43 state = get_state(record, options[:become])
44 record.lifecycle.become state if state
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 45 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 46
47
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 48 def run!(user, attributes)
49 record = lifecycle.model.new
8fccb31a » tablatom 2008-10-16 Fixes to lifecycles keeping... 50 record.lifecycle.active_step = self
540af749 » tablatom 2008-11-12 Lifecycles -- use acting_us... 51 record.with_acting_user(user) do
89d8331f » tablatom 2008-11-21 Major overhaul of the lifec... 52 prepare!(record, attributes)
53 if can_run?(record)
540af749 » tablatom 2008-11-12 Lifecycles -- use acting_us... 54 if change_state(record)
55 fire_event(record, on_create)
56 end
57 record
58 else
b9151678 » tablatom 2008-12-05 Fixes to raising permission... 59 raise Hobo::PermissionDeniedError
cd301a97 » tablatom 2008-09-08 New lifecycle semantics. 60 end
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 61 end
62 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 63
64
9d3c5687 » tablatom 2008-04-14 More work on lifecycles 65 def parameters
66 options[:params] || []
67 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 68
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 69 end
db40016d » tablatom 2008-06-13 Removing all trailing white... 70
3f85e668 » tablatom 2008-04-11 Initial work on lifecycles 71 end
72 end