Class: Horza::Adapters::ActiveRecord

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

Constant Summary collapse

INVALID_ANCESTRY_MSG =
'Invalid relation. Ensure that the plurality of your associations is correct.'
CONTEXT_NAMESPACE =
::ActiveRecord::Base

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

#collection_entity_klass, #context_for_entity, #expected_errors, #expected_horza_errors, #horza_error_from_orm_error, #lazy_load_model, #not_implemented_error, #single_entity_klass

Methods included from InstanceMethods

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

Class Method Details

.entity_context_mapObject



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

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

.expected_errors_mapObject



18
19
20
21
22
23
24
# File 'lib/horza/adapters/active_record/active_record.rb', line 18

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

.single_entity_klassObject



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

def single_entity_klass
  ::Horza::Entities::SingleWithActiveModel
end

Instance Method Details

#association(options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/horza/adapters/active_record/active_record.rb', line 78

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

    options.target.to_s.plural? ? entity(query(options, result)) : entity(result.attributes)
  end
end

#create!(options = {}) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/horza/adapters/active_record/active_record.rb', line 46

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

#create_as_child!(parent_args, options = {}) ⇒ Object



54
55
56
57
58
59
# File 'lib/horza/adapters/active_record/active_record.rb', line 54

def create_as_child!(parent_args, options = {})
  run_and_convert_exceptions do
    parent = Horza.adapter.context_for_entity(parent_args[:klass]).find(parent_args[:id])
    create!(options.merge(parent_args[:klass] => parent))
  end
end

#delete!(id) ⇒ Object



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

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

#find_all(options = {}) ⇒ Object



35
36
37
# File 'lib/horza/adapters/active_record/active_record.rb', line 35

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

#find_first!(options = {}) ⇒ Object



31
32
33
# File 'lib/horza/adapters/active_record/active_record.rb', line 31

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

#get!(id) ⇒ Object



27
28
29
# File 'lib/horza/adapters/active_record/active_record.rb', line 27

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

#join(options = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/horza/adapters/active_record/active_record.rb', line 39

def join(options = {})
  run_and_convert_exceptions do
    sql = ArelJoin.sql(self.context, options)
    entity(::ActiveRecord::Base.connection.exec_query(sql).to_a)
  end
end

#to_hashObject



93
94
95
96
97
# File 'lib/horza/adapters/active_record/active_record.rb', line 93

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



61
62
63
64
65
66
67
68
# File 'lib/horza/adapters/active_record/active_record.rb', line 61

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