Class: MnoEnterprise::BaseResource
- Inherits:
-
Object
- Object
- MnoEnterprise::BaseResource
- Includes:
- GlobalID::Identification, Her::Model, HerExtension::Validations::RemoteUniquenessValidation
- Defined in:
- app/models/mno_enterprise/base_resource.rb
Direct Known Subclasses
App, AppInstance, AppInstancesSync, AppReview, ArrearsSituation, AuditEvent, CreditCard, DeletionRequest, Identity, Invoice, OrgInvite, Organization, Team, Tenant, TenantInvoice, User
Class Method Summary collapse
-
.base_class ⇒ Object
ActiveRecord Compatibility for Her Returns the class descending directly from MnoEnterprise::BaseResource, or an abstract class, if any, in the inheritance hierarchy.
-
.exists?(hash) ⇒ Boolean
ActiveRecord Compatibility for Her.
-
.find_by(hash) ⇒ Object
Find first record using a hash of attributes.
-
.first(n = 1) ⇒ Object
ActiveRecord Compatibility for Her.
-
.last(n = 1) ⇒ Object
ActiveRecord Compatibility for Her.
Instance Method Summary collapse
-
#==(comparison_object) ⇒ Object
(also: #eql?)
Returns true if
comparison_objectis the same exact object, orcomparison_objectis of the same type andselfhas an ID and it is equal tocomparison_object.id. -
#cache_key(*timestamp_names) ⇒ Object
Instance methods ====================================================================== Returns a cache key that can be used to identify this record.
- #clear_association_cache ⇒ Object
-
#clear_attribute_changes! ⇒ Object
Reset the ActiveModel hash containing all attribute changes Useful when initializing a existing resource using a hash fetched via http call (e.g.: MnoEnterprise::User.authenticate).
- #max_updated_column_timestamp(timestamp_names = [:updated_at]) ⇒ Object
-
#read_attribute(attr_name) ⇒ Object
ActiveRecord Compatibility for Her.
-
#reload(options = nil) ⇒ Object
ActiveRecord Compatibility for Her.
-
#save(options = {}) ⇒ Object
ActiveRecord Compatibility for Her.
-
#save!(options = {}) ⇒ Object
ActiveRecord Compatibility for Her.
-
#update(attributes) ⇒ Object
ActiveRecord Compatibility for Her.
-
#write_attribute(attr_name, value) ⇒ Object
(also: #[]=)
ActiveRecord Compatibility for Her.
Methods included from HerExtension::Validations::RemoteUniquenessValidation
Class Method Details
.base_class ⇒ Object
ActiveRecord Compatibility for Her Returns the class descending directly from MnoEnterprise::BaseResource, or an abstract class, if any, in the inheritance hierarchy.
If A extends MnoEnterprise::BaseResource, A.base_class will return A. If B descends from A through some arbitrarily deep hierarchy, B.base_class will return A.
If B < A and C < B and if A is an abstract_class then both B.base_class and C.base_class would return B as the answer since A is an abstract_class.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/mno_enterprise/base_resource.rb', line 66 def base_class unless self < BaseResource raise Error, "#{name} doesn't belong in a hierarchy descending from BaseResource" end if superclass == BaseResource || superclass.abstract_class? self else superclass.base_class end end |
.exists?(hash) ⇒ Boolean
ActiveRecord Compatibility for Her
53 54 55 |
# File 'app/models/mno_enterprise/base_resource.rb', line 53 def exists?(hash) find_by(hash).present? end |
.find_by(hash) ⇒ Object
Find first record using a hash of attributes
48 49 50 |
# File 'app/models/mno_enterprise/base_resource.rb', line 48 def find_by(hash) self.where(hash).limit(1).first end |
.first(n = 1) ⇒ Object
ActiveRecord Compatibility for Her
34 35 36 37 38 |
# File 'app/models/mno_enterprise/base_resource.rb', line 34 def first(n = 1) return [] unless n > 0 q = self.order_by('id.asc').limit(n) n == 1 ? q.to_a.first : q.to_a end |
.last(n = 1) ⇒ Object
ActiveRecord Compatibility for Her
41 42 43 44 45 |
# File 'app/models/mno_enterprise/base_resource.rb', line 41 def last(n = 1) return [] unless n > 0 q = self.order_by('id.desc').limit(n) n == 1 ? q.to_a.first : q.to_a end |
Instance Method Details
#==(comparison_object) ⇒ Object Also known as: eql?
Returns true if comparison_object is the same exact object, or comparison_object is of the same type and self has an ID and it is equal to comparison_object.id.
Note that new records are different from any other record by definition, unless the other record is the receiver itself. Besides, if you fetch existing records with select and leave the ID out, you’re on your own, this predicate will return false.
Note also that destroying a record preserves its ID in the model instance, so deleted models are still comparable.
187 188 189 190 191 192 |
# File 'app/models/mno_enterprise/base_resource.rb', line 187 def ==(comparison_object) super || comparison_object.instance_of?(self.class) && !id.nil? && comparison_object.id == id end |
#cache_key(*timestamp_names) ⇒ Object
Instance methods
Returns a cache key that can be used to identify this record.
Product.new.cache_key # => "products/new"
Product.find(5).cache_key # => "products/5" (updated_at not available)
Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available)
You can also pass a list of named timestamps, and the newest in the list will be used to generate the key:
Person.find(5).cache_key(:updated_at, :last_reviewed_at)
Notes: copied from ActiveRecord
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/mno_enterprise/base_resource.rb', line 94 def cache_key(*) case when new? "#{model_name.cache_key}/new" when .any? = () = .utc.to_s(:nsec) "#{model_name.cache_key}/#{id}-#{timestamp}" when = = .utc.to_s(:nsec) "#{model_name.cache_key}/#{id}-#{timestamp}" else "#{model_name.cache_key}/#{id}" end end |
#clear_association_cache ⇒ Object
118 119 120 121 122 |
# File 'app/models/mno_enterprise/base_resource.rb', line 118 def clear_association_cache self.class.associations[:has_many].each do |assoc| instance_variable_set(:"@_her_association_#{assoc[:name]}", nil) end end |
#clear_attribute_changes! ⇒ Object
Reset the ActiveModel hash containing all attribute changes Useful when initializing a existing resource using a hash fetched via http call (e.g.: MnoEnterprise::User.authenticate)
174 175 176 |
# File 'app/models/mno_enterprise/base_resource.rb', line 174 def clear_attribute_changes! self.instance_variable_set(:@changed_attributes, {}) end |
#max_updated_column_timestamp(timestamp_names = [:updated_at]) ⇒ Object
110 111 112 113 114 115 116 |
# File 'app/models/mno_enterprise/base_resource.rb', line 110 def ( = [:updated_at]) .map { |attr| self[attr] } .compact .map(&:to_time) .max end |
#read_attribute(attr_name) ⇒ Object
ActiveRecord Compatibility for Her
125 126 127 |
# File 'app/models/mno_enterprise/base_resource.rb', line 125 def read_attribute(attr_name) get_attribute(attr_name) end |
#reload(options = nil) ⇒ Object
ActiveRecord Compatibility for Her
159 160 161 162 163 |
# File 'app/models/mno_enterprise/base_resource.rb', line 159 def reload( = nil) @attributes.update(self.class.find(self.id).attributes) self.run_callbacks :find self end |
#save(options = {}) ⇒ Object
ActiveRecord Compatibility for Her
136 137 138 139 140 141 142 143 144 |
# File 'app/models/mno_enterprise/base_resource.rb', line 136 def save(={}) if perform_validations() ret = super() process_response_errors ret else false end end |
#save!(options = {}) ⇒ Object
ActiveRecord Compatibility for Her
147 148 149 150 151 152 153 154 155 156 |
# File 'app/models/mno_enterprise/base_resource.rb', line 147 def save!(={}) if perform_validations() ret = super() process_response_errors raise_record_invalid if self.errors.any? ret else raise_record_invalid end end |
#update(attributes) ⇒ Object
ActiveRecord Compatibility for Her
166 167 168 169 |
# File 'app/models/mno_enterprise/base_resource.rb', line 166 def update(attributes) assign_attributes(attributes) save end |
#write_attribute(attr_name, value) ⇒ Object Also known as: []=
ActiveRecord Compatibility for Her
130 131 132 |
# File 'app/models/mno_enterprise/base_resource.rb', line 130 def write_attribute(attr_name, value) assign_attributes(attr_name => value) end |