Class: Horza::Adapters::ActiveRecord

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

Constant Summary collapse

INVALID_ANCESTRY_MSG =
'Invalid relation. Ensure that the plurality of your associations is correct.'

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

#context_for_entity, #expected_errors, #expected_horza_errors, #horza_error_from_orm_error, #not_implemented_error

Methods included from InstanceMethods

#create, #delete, #find_first, #get, #initialize, #update

Class Method Details

.entity_context_mapObject



7
8
9
10
11
# File 'lib/horza/adapters/active_record.rb', line 7

def entity_context_map
  # Rails doesn't preload classes in development mode, caching doesn't make sense
  return ::Horza.descendants_map(::ActiveRecord::Base) if ::Horza.configuration.development_mode
  @map ||= ::Horza.descendants_map(::ActiveRecord::Base)
end

.expected_errors_mapObject



13
14
15
16
17
18
# File 'lib/horza/adapters/active_record.rb', line 13

def expected_errors_map
  {
    ::ActiveRecord::RecordNotFound => Horza::Errors::RecordNotFound,
    ::ActiveRecord::RecordInvalid => Horza::Errors::RecordInvalid
  }
end

Instance Method Details

#association(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/horza/adapters/active_record.rb', line 58

def association(options = {})
  run_and_convert_exceptions do
    options = Options.new(options)

    base = @context
    base = base.includes(options.eager_args) if options.eager_load?
    base = base.find(options.id)

    result = walk_family_tree(base, options)
    return nil unless result

    collection?(result) ? entity_class(query(options, result)) : entity_class(result.attributes)
  end
end

#create!(options = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/horza/adapters/active_record.rb', line 33

def create!(options = {})
  run_and_convert_exceptions do
    record = @context.new(options)
    record.save!
    entity_class(record.attributes)
  end
end

#delete!(id) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/horza/adapters/active_record.rb', line 50

def delete!(id)
  run_and_convert_exceptions do
    record = @context.find(id)
    record.destroy
    true
  end
end

#find_all(options = {}) ⇒ Object



29
30
31
# File 'lib/horza/adapters/active_record.rb', line 29

def find_all(options = {})
  run_and_convert_exceptions { entity_class(query(options)) }
end

#find_first!(options = {}) ⇒ Object



25
26
27
# File 'lib/horza/adapters/active_record.rb', line 25

def find_first!(options = {})
  run_and_convert_exceptions { entity_class(query(options).first!.attributes) }
end

#get!(id) ⇒ Object



21
22
23
# File 'lib/horza/adapters/active_record.rb', line 21

def get!(id)
  run_and_convert_exceptions { entity_class(@context.find(id).attributes) }
end

#to_hashObject



73
74
75
76
77
# File 'lib/horza/adapters/active_record.rb', line 73

def to_hash
  raise ::Horza::Errors::CannotGetHashFromCollection.new if collection?
  raise ::Horza::Errors::QueryNotYetPerformed.new unless @context.respond_to?(:attributes)
  @context.attributes
end

#update!(id, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/horza/adapters/active_record.rb', line 41

def update!(id, options = {})
  run_and_convert_exceptions do
    record = @context.find(id)
    record.assign_attributes(options)
    record.save!
    entity_class(record.attributes)
  end
end