Module: ActiveRecordExtension
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_record_extension.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #delete ⇒ Object
- #destroy ⇒ Object
- #destroy! ⇒ Object
-
#reload(options = nil) ⇒ Object
If using default_scope to restrict fields returned (with select) Change reload so it still uses the default scope rather than unscoped.
-
#reload_after_odata(options = nil) ⇒ Object
Created a method here because could not override ActiveRecord Persistance module because 3 other modules override the reload method.
- #save ⇒ Object
- #save! ⇒ Object
- #update(attributes) ⇒ Object
- #update_attribute(name, value) ⇒ Object
Instance Method Details
#delete ⇒ Object
8 9 10 |
# File 'lib/active_record_extension.rb', line 8 def delete ::OData::Model.destroy(self) end |
#destroy ⇒ Object
12 13 14 15 16 |
# File 'lib/active_record_extension.rb', line 12 def destroy destroy! rescue false end |
#destroy! ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_record_extension.rb', line 18 def destroy! run_callbacks :destroy do ::OData::Model.destroy(self) end has_errors = errors.present? if has_errors _raise_record_not_destroyed end !has_errors end |
#reload(options = nil) ⇒ Object
If using default_scope to restrict fields returned (with select) Change reload so it still uses the default scope rather than unscoped
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_record_extension.rb', line 102 def reload( = nil) self.class.connection.clear_query_cache fresh_object = if && [:lock] self.class.unscoped { self.class.lock([:lock]).find(id) } else self.class.find(id) end @attributes = fresh_object.instance_variable_get("@attributes") @new_record = false self end |
#reload_after_odata(options = nil) ⇒ Object
Created a method here because could not override ActiveRecord Persistance module because 3 other modules override the reload method. I’m not really that thrilled with what I did here with this method for reload. Basically got parts from various ActiveRecord modules and combined them in here. I followed the reload through byebug to see how “reload” was being called
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_record_extension.rb', line 61 def reload_after_odata( = nil) # aggregations.rb clear_aggregation_cache # autosave_association.rb @marked_for_destruction = false @destroyed_by_association = nil # associations.rb clear_association_cache # persistence.rb (only 1 line of code chaned in here) self.class.connection.clear_query_cache fresh_object = if && [:lock] self.class.unscoped { self.class.lock([:lock]).find(id) } else # Remove unscope here self.class.find(id) end @attributes = fresh_object.instance_variable_get("@attributes") @new_record = false # attribute_methods/dirty.rb @previous_mutation_tracker = nil clear_mutation_trackers @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new self end |
#save ⇒ Object
29 30 31 32 33 |
# File 'lib/active_record_extension.rb', line 29 def save(*) save! rescue false end |
#save! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_record_extension.rb', line 35 def save!(*) validate! run_callbacks :save do ::OData::Model.save(self) end has_errors = errors.present? if has_errors raise_validation_error else reload_after_odata unless id.nil? end !has_errors end |
#update(attributes) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/active_record_extension.rb', line 49 def update(attributes) attributes.each do |k,v| write_attribute(k,v) end save end |
#update_attribute(name, value) ⇒ Object
95 96 97 98 |
# File 'lib/active_record_extension.rb', line 95 def update_attribute(name, value) write_attribute(name,value) save end |