Class: MnoEnterprise::BaseResource

Inherits:
Object
  • Object
show all
Includes:
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.



60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/mno_enterprise/base_resource.rb', line 60

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

.find_by(hash) ⇒ Object

Find first record using a hash of attributes



47
48
49
# File 'app/models/mno_enterprise/base_resource.rb', line 47

def find_by(hash)
  self.where(hash).limit(1).first
end

.first(n = 1) ⇒ Object

ActiveRecord Compatibility for Her



33
34
35
36
37
# File 'app/models/mno_enterprise/base_resource.rb', line 33

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



40
41
42
43
44
# File 'app/models/mno_enterprise/base_resource.rb', line 40

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.



180
181
182
183
184
185
# File 'app/models/mno_enterprise/base_resource.rb', line 180

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/mno_enterprise/base_resource.rb', line 88

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



112
113
114
115
116
# File 'app/models/mno_enterprise/base_resource.rb', line 112

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)



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

def clear_attribute_changes!
  self.instance_variable_set(:@changed_attributes, {})
end

#max_updated_column_timestamp(timestamp_names = [:updated_at]) ⇒ Object



104
105
106
107
108
109
110
# File 'app/models/mno_enterprise/base_resource.rb', line 104

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



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

def read_attribute(attr_name)
  get_attribute(attr_name)
end

#reload(options = nil) ⇒ Object

ActiveRecord Compatibility for Her



152
153
154
155
156
# File 'app/models/mno_enterprise/base_resource.rb', line 152

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



130
131
132
133
134
135
136
137
138
# File 'app/models/mno_enterprise/base_resource.rb', line 130

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

#save!(options = {}) ⇒ Object

ActiveRecord Compatibility for Her



141
142
143
144
145
146
147
148
149
# File 'app/models/mno_enterprise/base_resource.rb', line 141

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

#update(attributes) ⇒ Object

ActiveRecord Compatibility for Her



159
160
161
162
# File 'app/models/mno_enterprise/base_resource.rb', line 159

def update(attributes)
  assign_attributes(attributes)
  save
end

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

ActiveRecord Compatibility for Her



124
125
126
# File 'app/models/mno_enterprise/base_resource.rb', line 124

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