Class: Maintain::Backend::ActiveRecord
- Defined in:
- lib/maintain/backend/active_record.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #aggregate(maintainee, name, attribute, states, options = {}) ⇒ Object
- #on(maintainee, attribute, event, state, method, options) ⇒ Object
- #read(instance, attribute) ⇒ Object
- #state(maintainee, name, attribute, value, options = {}) ⇒ Object
- #write(instance, attribute, value) ⇒ Object
Instance Method Details
#aggregate(maintainee, name, attribute, states, options = {}) ⇒ Object
4 5 6 7 |
# File 'lib/maintain/backend/active_record.rb', line 4 def aggregate(maintainee, name, attribute, states, = {}) # named_scope will handle the array of states as "IN" in SQL state(maintainee, name, attribute, states, .merge(:dirty => false)) end |
#on(maintainee, attribute, event, state, method, options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/maintain/backend/active_record.rb', line 9 def on(maintainee, attribute, event, state, method, ) attribute_check = "#{attribute}#{"_was" if event == :exit}_#{state}?" maintainee.before_save method, :if => lambda {|instance| if instance.send("#{attribute}_changed?") && instance.send(attribute_check) if [:if] if [:if].is_a?(Proc) instance.instance_eval(&[:if]) else instance.send([:if]) end elsif [:unless] if [:unless].is_a?(Proc) !instance.instance_eval(&[:unless]) else !instance.send([:unless]) end else true end else false end } end |
#read(instance, attribute) ⇒ Object
34 35 36 |
# File 'lib/maintain/backend/active_record.rb', line 34 def read(instance, attribute) instance.read_attribute(attribute) end |
#state(maintainee, name, attribute, value, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/maintain/backend/active_record.rb', line 38 def state(maintainee, name, attribute, value, = {}) = {:dirty => true}.merge() conditions = {:conditions => {attribute => value}} named_scope_method = defined?(::ActiveRecord::VERSION) && ::ActiveRecord::VERSION::STRING >= '3' ? :scope : :named_scope maintainee.send(named_scope_method, name, conditions) if !maintainee.respond_to?(name) || [:force] maintainee.send(named_scope_method, "#{attribute}_#{name}", conditions) if [:dirty] maintainee.class_eval <<-dirty_tracker def #{attribute}_was_#{name}? #{attribute}_was == self.class.maintainers[:#{attribute}].value(self).value_for(:#{name}) end dirty_tracker end end |
#write(instance, attribute, value) ⇒ Object
53 54 55 |
# File 'lib/maintain/backend/active_record.rb', line 53 def write(instance, attribute, value) instance.send(:write_attribute, attribute, value) end |