Module: ActForm::Model
- Extended by:
- ActiveSupport::Concern
- Includes:
- Attributes, Merge, ActiveModel::Model
- Defined in:
- lib/act_form/model.rb
Instance Method Summary collapse
-
#init_by(record, **attrs) ⇒ Object
Record must respond_to attributes method.
- #initialize(attrs = {}) ⇒ Object
- #persisted? ⇒ Boolean
- #record=(record) ⇒ Object
- #save(target = nil) ⇒ Object
- #sync(target) ⇒ Object
Methods included from Attributes
Instance Method Details
#init_by(record, **attrs) ⇒ Object
Record must respond_to attributes method
27 28 29 30 31 |
# File 'lib/act_form/model.rb', line 27 def init_by(record, **attrs) self.record = record _attrs = record.attributes.extract! *self.class.attribute_set.keys.map(&:to_s) assign_attributes _attrs.merge(attrs) end |
#initialize(attrs = {}) ⇒ Object
17 18 19 |
# File 'lib/act_form/model.rb', line 17 def initialize(attrs={}) super attrs.select { |k, _| respond_to?("#{k}=") } end |
#persisted? ⇒ Boolean
50 51 52 |
# File 'lib/act_form/model.rb', line 50 def persisted? !!@persisted end |
#record=(record) ⇒ Object
21 22 23 24 |
# File 'lib/act_form/model.rb', line 21 def record=(record) raise ArgumentError, 'Record must respond to attributes method!' unless record.respond_to?(:attributes) @record = record end |
#save(target = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/act_form/model.rb', line 40 def save(target = nil) target ||= @record if valid? sync(target) @persisted = target.save else false end end |
#sync(target) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/act_form/model.rb', line 33 def sync(target) self.class.attribute_set.keys.each do |attr| next unless target.respond_to?(attr) target.public_send "#{attr}=", public_send(attr) end end |