Class: MnoEnterprise::BaseResource

Inherits:
Object
  • Object
show all
Includes:
GlobalID::Identification, Her::Model, HerExtension::Validations::RemoteUniquenessValidation
Defined in:
app/models/mno_enterprise/base_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HerExtension::Validations::RemoteUniquenessValidation

included

Class Method Details

.base_classObject

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

Returns:

  • (Boolean)


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.



189
190
191
192
193
194
# File 'app/models/mno_enterprise/base_resource.rb', line 189

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(*timestamp_names)
  case
    when new?
      "#{model_name.cache_key}/new"
    when timestamp_names.any?
      timestamp = max_updated_column_timestamp(timestamp_names)
      timestamp = timestamp.utc.to_s(:nsec)
      "#{model_name.cache_key}/#{id}-#{timestamp}"
    when timestamp = max_updated_column_timestamp
      timestamp = timestamp.utc.to_s(:nsec)
      "#{model_name.cache_key}/#{id}-#{timestamp}"
    else
      "#{model_name.cache_key}/#{id}"
  end
end

#clear_association_cacheObject

Clear the record association cache



119
120
121
122
123
124
# File 'app/models/mno_enterprise/base_resource.rb', line 119

def clear_association_cache
  self.class.associations[:has_many].each do |assoc|
    instance_variable_set(:"@_her_association_#{assoc[:name]}", nil)
    attributes.delete(assoc[:name].to_s)
  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)



176
177
178
# File 'app/models/mno_enterprise/base_resource.rb', line 176

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 max_updated_column_timestamp(timestamp_names = [:updated_at])
  timestamp_names
      .map { |attr| self[attr] }
      .compact
      .map(&:to_time)
      .max
end

#read_attribute(attr_name) ⇒ Object

ActiveRecord Compatibility for Her



127
128
129
# File 'app/models/mno_enterprise/base_resource.rb', line 127

def read_attribute(attr_name)
  get_attribute(attr_name)
end

#reload(options = nil) ⇒ Object

ActiveRecord Compatibility for Her



161
162
163
164
165
# File 'app/models/mno_enterprise/base_resource.rb', line 161

def reload(options = nil)
  @attributes.update(self.class.find(self.id).attributes)
  self.run_callbacks :find
  self
end

#save(options = {}) ⇒ Object

ActiveRecord Compatibility for Her



138
139
140
141
142
143
144
145
146
# File 'app/models/mno_enterprise/base_resource.rb', line 138

def save(options={})
  if perform_validations(options)
    ret = super()
    process_response_errors
    ret
  else
    false
  end
end

#save!(options = {}) ⇒ Object

ActiveRecord Compatibility for Her



149
150
151
152
153
154
155
156
157
158
# File 'app/models/mno_enterprise/base_resource.rb', line 149

def save!(options={})
  if perform_validations(options)
    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



168
169
170
171
# File 'app/models/mno_enterprise/base_resource.rb', line 168

def update(attributes)
  assign_attributes(attributes)
  save
end

#write_attribute(attr_name, value) ⇒ Object Also known as: []=

ActiveRecord Compatibility for Her



132
133
134
# File 'app/models/mno_enterprise/base_resource.rb', line 132

def write_attribute(attr_name, value)
  assign_attributes(attr_name => value)
end