Class: Get::Adapters::ActiveRecord

Inherits:
AbstractAdapter show all
Defined in:
lib/get/adapters/active_record.rb

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

descendants, #eager_load, #initialize, not_implemented_error

Constructor Details

This class inherits a constructor from Get::Adapters::AbstractAdapter

Class Method Details

.context_for_entity(entity) ⇒ Object



9
10
11
# File 'lib/get/adapters/active_record.rb', line 9

def context_for_entity(entity)
  entity_context_map[entity]
end

.entity_context_mapObject



13
14
15
# File 'lib/get/adapters/active_record.rb', line 13

def entity_context_map
  @map ||= ::ActiveRecord::Base.descendants.reduce({}) { |hash, (klass)| hash.merge(klass.name.split('::').last.underscore.to_sym => klass) }
end

.expected_errorsObject



5
6
7
# File 'lib/get/adapters/active_record.rb', line 5

def expected_errors
  [::ActiveRecord::RecordNotFound]
end

Instance Method Details

#ancestors(options = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/get/adapters/active_record.rb', line 30

def ancestors(options = {})
  get!(options)
  walk_family_tree(options)
  rescue NoMethodError
    raise ::Get::Errors::InvalidAncestry.new('Invalid relation. Ensure that the plurality of your associations is correct.')
end

#find_all(options = {}) ⇒ Object



26
27
28
# File 'lib/get/adapters/active_record.rb', line 26

def find_all(options = {})
  @context = @context.where(options[:conditions]).order('ID DESC')
end

#find_first(options = {}) ⇒ Object



22
23
24
# File 'lib/get/adapters/active_record.rb', line 22

def find_first(options = {})
  @context = find_all(options).limit(1).first!
end

#get!(options = {}) ⇒ Object



18
19
20
# File 'lib/get/adapters/active_record.rb', line 18

def get!(options = {})
  @context = @context.find(options[:id])
end

#to_hashObject



37
38
39
# File 'lib/get/adapters/active_record.rb', line 37

def to_hash
  @context.attributes
end