class Horza::Adapters::ActiveRecord
Constants
- CONTEXT_NAMESPACE
- INVALID_ANCESTRY_MSG
Public Class Methods
expected_errors_map()
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 14 def expected_errors_map { ::ActiveRecord::RecordNotFound => Horza::Errors::RecordNotFound, ::ActiveRecord::RecordInvalid => Horza::Errors::RecordInvalid, ::ActiveRecord::UnknownAttributeError => Horza::Errors::UnknownAttributeError } end
single_entity_klass()
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 10 def single_entity_klass ::Horza::Entities::SingleWithActiveModel end
Public Instance Methods
association(options = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 74 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 if result.nil? options.target.to_s.plural? ? entity(query(options, result)) : entity(result.attributes) end end
create!(options = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 42 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 = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 50 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)
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 66 def delete!(id) run_and_convert_exceptions do record = @context.find(id) record.destroy true end end
find_all(options = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 31 def find_all(options = {}) run_and_convert_exceptions { entity(query(options)) } end
find_first!(options = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 27 def find_first!(options = {}) run_and_convert_exceptions { entity(query(options).first!.attributes) } end
get!(id)
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 23 def get!(id) run_and_convert_exceptions { entity(@context.find(id).attributes) } end
join(options = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 35 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_hash()
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 89 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 = {})
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 57 def update!(id, options = {}) run_and_convert_exceptions do record = @context.find(id) record.assign_attributes(options) record.save! entity(record.attributes) end end
Private Instance Methods
collection?(subject = @context)
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 108 def collection?(subject = @context) subject.is_a?(::ActiveRecord::Relation) || subject.is_a?(Array) end
query(options, base = @context)
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 97 def query(options, base = @context) options = options.is_a?(Options) ? options : Options.new(options) result = base result = base.where(options.conditions) if options.conditions result = result.order(base.arel_table[options.order_field].send(options.order_direction)) result = result.limit(options.limit) if options.limit result = result.offset(options.offset) if options.offset result end
walk_family_tree(object, options)
click to toggle source
# File lib/horza/adapters/active_record/active_record.rb, line 112 def walk_family_tree(object, options) via = options.via || [] via.push(options.target).reduce(object) do |object, relation| return nil if object.nil? raise ::Horza::Errors::InvalidAncestry.new(INVALID_ANCESTRY_MSG) unless object.respond_to? relation object.send(relation) end end