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
- #assign_attributes(attributes) ⇒ Object
- #create ⇒ Object
- #create! ⇒ Object
- #destroy ⇒ Object
- #destroy! ⇒ Object
- #save(args = {}) ⇒ Object
- #save!(args = {}) ⇒ Object
- #update!(attributes) ⇒ Object
- #update_attributes(attributes) ⇒ Object
- #update_attributes!(attributes) ⇒ Object
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
93 94 95 |
# File 'lib/baza_models/model/manipulation.rb', line 93 def assign_attributes(attributes) @changes.merge!(real_attributes(attributes)) end |
#create ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) 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
64 65 66 67 68 69 70 |
# File 'lib/baza_models/model/manipulation.rb', line 64 def create! if create return true else raise BazaModels::Errors::InvalidRecord, errors..join(". ") end end |
#destroy ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/baza_models/model/manipulation.rb', line 97 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
115 116 117 |
# File 'lib/baza_models/model/manipulation.rb', line 115 def destroy! raise BazaModels::Errors::InvalidRecord, @errors..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 return true else return false end end |
#save!(args = {}) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/baza_models/model/manipulation.rb', line 72 def save!(args = {}) if save(args) return true else raise BazaModels::Errors::InvalidRecord, errors..join(". ") end end |
#update!(attributes) ⇒ Object
89 90 91 |
# File 'lib/baza_models/model/manipulation.rb', line 89 def update!(attributes) raise BazaModels::Errors::InvalidRecord, @errors..join(". ") unless update_attributes(attributes) end |
#update_attributes(attributes) ⇒ Object
80 81 82 83 |
# File 'lib/baza_models/model/manipulation.rb', line 80 def update_attributes(attributes) assign_attributes(attributes) save end |
#update_attributes!(attributes) ⇒ Object
85 86 87 |
# File 'lib/baza_models/model/manipulation.rb', line 85 def update_attributes!(attributes) update!(attributes) end |