Module: ActForm::Model

Extended by:
ActiveSupport::Concern
Includes:
Attributes, Merge, ActiveModel::Model
Included in:
Base
Defined in:
lib/act_form/model.rb

Instance Method Summary collapse

Methods included from Attributes

#attributes

Instance Method Details

#init_by(record, **attrs) ⇒ Object

Record must respond_to attributes method



22
23
24
25
26
# File 'lib/act_form/model.rb', line 22

def init_by(record, **attrs)
  @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

Returns:

  • (Boolean)


45
46
47
# File 'lib/act_form/model.rb', line 45

def persisted?
  !!@persisted
end

#save(target = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/act_form/model.rb', line 35

def save(target = nil)
  target ||= @record
  if valid?
    sync(target)
    @persisted = target.save
  else
    false
  end
end

#sync(target) ⇒ Object



28
29
30
31
32
33
# File 'lib/act_form/model.rb', line 28

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