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



95
96
97
# File 'lib/baza_models/model/manipulation.rb', line 95

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

#createObject



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

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)

  @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

  true
end

#create!Object



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

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

#destroyObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/baza_models/model/manipulation.rb', line 99

def destroy
  if new_record?
    errors.add(:base, "cannot destroy new record")
    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)
    true
  end
end

#destroy!Object



117
118
119
# File 'lib/baza_models/model/manipulation.rb', line 117

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
35
36
37
# 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
      @before_last_save = @data
      status = create
    else
      fire_callbacks(:before_update)
      @before_last_save = @data
      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

    true
  else
    false
  end
end

#save!(args = {}) ⇒ Object



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

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

#update(attributes) ⇒ Object



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

def update(attributes)
  assign_attributes(attributes)
  save
end

#update!(attributes) ⇒ Object



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

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

#update_attributes(attributes) ⇒ Object



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

def update_attributes(attributes)
  update(attributes)
end

#update_attributes!(attributes) ⇒ Object



91
92
93
# File 'lib/baza_models/model/manipulation.rb', line 91

def update_attributes!(attributes)
  update!(attributes)
end