Module: BazaModels::Model::Manipulation

Included in:
BazaModels::Model
Defined in:
lib/baza_models/model/manipulation.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
# File 'lib/baza_models/model/manipulation.rb', line 2

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



86
87
88
# File 'lib/baza_models/model/manipulation.rb', line 86

def assign_attributes(attributes)
  @changes.merge!(real_attributes(attributes))
end

#createObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/baza_models/model/manipulation.rb', line 36

def create
  unless new_record?
    errors.add(:base, "cannot create unless new record")
    return false
  end

  fire_callbacks(:before_create)
  self.created_at ||= Time.now if has_attribute?(:created_at)

  @data[:id] = db.insert(table_name, @changes, return_id: true)

  if @autoloads
    @autoloads.each do |relation_name, collection|
      relation = self.class.relationships.fetch(relation_name)

      collection.each do |model|
        model.assign_attributes(relation.fetch(:foreign_key) => id)
        model.create! if model.new_record?
      end
    end
  end

  true
end

#create!Object



61
62
63
64
65
66
67
# File 'lib/baza_models/model/manipulation.rb', line 61

def create!
  if create
    return true
  else
    raise BazaModels::Errors::InvalidRecord, errors.full_messages.join(". ")
  end
end

#destroyObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/baza_models/model/manipulation.rb', line 90

def destroy
  if new_record?
    errors.add(:base, "cannot destroy new record")
    return false
  else
    fire_callbacks(:before_destroy)

    return false unless restrict_has_one_relations
    return false unless restrict_has_many_relations
    return false unless destroy_has_one_relations
    return false unless destroy_has_many_relations

    db.delete(table_name, id: id)
    fire_callbacks(:after_destroy)
    return true
  end
end

#destroy!Object



108
109
110
# File 'lib/baza_models/model/manipulation.rb', line 108

def destroy!
  raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless destroy
end

#save(args = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/baza_models/model/manipulation.rb', line 6

def save(args = {})
  if args[:validate] == false || valid?
    new_record = new_record?
    fire_callbacks(:before_save)
    self.updated_at = Time.now if changed? && has_attribute?(:updated_at)

    if new_record
      status = create
    else
      fire_callbacks(:before_update)
      db.update(table_name, @changes, id: id) if changed?
      fire_callbacks(:after_update)
      status = true
    end

    return false unless status

    @changes = {}
    @new_record = false
    reload

    fire_callbacks(:after_save)
    fire_callbacks(:after_create) if new_record

    return true
  else
    return false
  end
end

#save!(args = {}) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/baza_models/model/manipulation.rb', line 69

def save!(args = {})
  if save(args)
    return true
  else
    raise BazaModels::Errors::InvalidRecord, errors.full_messages.join(". ")
  end
end

#update_attributes(attributes) ⇒ Object



77
78
79
80
# File 'lib/baza_models/model/manipulation.rb', line 77

def update_attributes(attributes)
  assign_attributes(attributes)
  save
end

#update_attributes!(attributes) ⇒ Object



82
83
84
# File 'lib/baza_models/model/manipulation.rb', line 82

def update_attributes!(attributes)
  raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless update_attributes(attributes)
end