Class: Aform::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/aform/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, form, attributes = {}, destroy_key = :_destroy) ⇒ Model

Returns a new instance of Model.



5
6
7
8
9
10
11
# File 'lib/aform/model.rb', line 5

def initialize(object, form, attributes = {}, destroy_key = :_destroy)
  @destroy = attributes.delete(destroy_key)
  @object = object
  @form = form
  sync_with_model
  @attributes.merge! attributes_for_save(attributes)
end

Class Method Details

.build_klass(params, validations, builder = Aform::Builder.new(Aform::Model)) ⇒ Object



13
14
15
# File 'lib/aform/model.rb', line 13

def self.build_klass(params, validations, builder = Aform::Builder.new(Aform::Model))
  builder.build_model_klass(params, validations)
end

.model_nameObject



17
18
19
# File 'lib/aform/model.rb', line 17

def self.model_name
  ActiveModel::Name.new(self, nil, "Aform::Model")
end

.validates_uniqueness_of(*attr_names) ⇒ Object



21
22
23
# File 'lib/aform/model.rb', line 21

def self.validates_uniqueness_of(*attr_names)
  validates_with UniquenessValidator, _merge_attributes(attr_names)
end

Instance Method Details

#save(association = nil) ⇒ Object

AR saves children with parent if it’s new object but dont save children with parent when children is updated



27
28
29
30
31
32
33
34
35
# File 'lib/aform/model.rb', line 27

def save(association = nil)
  @object.assign_attributes(@attributes)
  if @destroy
    @object.destroy
  else
    association << @object if association
    @object.save
  end
end

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/aform/model.rb', line 37

def valid?
  @destroy || super
end