Module: ActForm::Model
- Extended by:
- ActiveSupport::Concern
- Includes:
- Attributes, Merge, ActiveModel::Model, ActiveModel::Validations::Callbacks
- Defined in:
- lib/act_form/model.rb
Overview
rubocop:disable Style/Documentation
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
31 32 33 34 35 |
# File 'lib/act_form/model.rb', line 31 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
20 21 22 |
# File 'lib/act_form/model.rb', line 20 def initialize(attrs = {}) super attrs.select { |k, _| respond_to?("#{k}=") } end |
#persisted? ⇒ Boolean
55 56 57 |
# File 'lib/act_form/model.rb', line 55 def persisted? !!@persisted end |
#record=(record) ⇒ Object
24 25 26 27 28 |
# File 'lib/act_form/model.rb', line 24 def record=(record) raise ArgumentError, 'Record must respond to attributes method!' unless record.respond_to?(:attributes) @record = record end |
#save(target = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/act_form/model.rb', line 45 def save(target = nil) target ||= @record if valid? sync(target) @persisted = target.save else false end end |
#sync(target) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/act_form/model.rb', line 37 def sync(target) self.class.attribute_set.each_key do |attr| next unless target.respond_to?(attr) target.public_send "#{attr}=", public_send(attr) end end |