Class: DCA::Models::BaseModel

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Validations, Binder, Storage
Defined in:
lib/dca/models/base_model.rb

Direct Known Subclasses

Position

Constant Summary

Constants included from Binder

DCA::Models::Binder::COMPLEX_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Storage

#destroy, #save, #state

Methods included from Binder

#bind

Constructor Details

#initialize(params = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



18
19
20
# File 'lib/dca/models/base_model.rb', line 18

def initialize(params={})
  params.each { |attr, value| self.instance_variable_set "@#{attr}", value } if params
end

Instance Attribute Details

#base_idObject

Returns the value of attribute base_id.



16
17
18
# File 'lib/dca/models/base_model.rb', line 16

def base_id
  @base_id
end

#created_atObject

Returns the value of attribute created_at.



16
17
18
# File 'lib/dca/models/base_model.rb', line 16

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



16
17
18
# File 'lib/dca/models/base_model.rb', line 16

def id
  @id
end

#updated_atObject

Returns the value of attribute updated_at.



16
17
18
# File 'lib/dca/models/base_model.rb', line 16

def updated_at
  @updated_at
end

Instance Method Details

#attributesObject



32
33
34
35
36
37
38
39
40
# File 'lib/dca/models/base_model.rb', line 32

def attributes
  return @attributes unless @attributes.nil?

  @attributes = Hash[instance_variables.map { |var| [var.to_s.delete('@'), instance_variable_get(var)]}]
  @attributes.delete 'errors'
  @attributes.delete 'validation_context'

  @attributes
end

#before_createObject



46
47
48
# File 'lib/dca/models/base_model.rb', line 46

def before_create
  self.created_at = Time.now.utc
end

#before_updateObject



42
43
44
# File 'lib/dca/models/base_model.rb', line 42

def before_update
  self.updated_at = Time.now.utc
end

#persisted?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dca/models/base_model.rb', line 22

def persisted?
  true
end

#to_hashObject



26
27
28
29
30
# File 'lib/dca/models/base_model.rb', line 26

def to_hash
  include = []
  self.class.associations(true).each { |field, options| include << field.to_s}
  self.serializable_hash include: include
end

#validate_associationsObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dca/models/base_model.rb', line 50

def validate_associations
  self.class.associations.each do |field, options|
    object = self.send(field)
    next if object.nil?

    if object.is_a? Array
      object.each { |item| validate_child item, field }
    else
      validate_child object, field
    end
  end
end