module ActForm::Model
Public Class Methods
new(attrs={})
click to toggle source
Calls superclass method
# File lib/act_form/model.rb, line 17 def initialize(attrs={}) super attrs.select { |k, _| respond_to?("#{k}=") } end
Public Instance Methods
init_by(record, **attrs)
click to toggle source
Record must respond_to attributes method
# 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
persisted?()
click to toggle source
# File lib/act_form/model.rb, line 50 def persisted? !!@persisted end
record=(record)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
inherited(child_class)
click to toggle source
Calls superclass method
# File lib/act_form/model.rb, line 69 def inherited(child_class) child_class.include Combinable super end
validate_required_attributes()
click to toggle source
# File lib/act_form/model.rb, line 56 def validate_required_attributes self.class.attribute_set.each do |attr_name, arr| _, options = arr next if options.key?(:default) next if !options[:required] if attributes[attr_name].nil? errors.add(attr_name, :required) end end throw(:abort) unless errors.empty? end