class JsonapiCompliable::Adapters::ActiveRecord

@see Adapters::Abstract

Public Instance Methods

associate(parent, child, association_name, association_type) click to toggle source

When a has_many relationship, we need to avoid Activerecord implicitly firing a query. Otherwise, simple assignment will do @see Adapters::Abstract#associate

# File lib/jsonapi_compliable/adapters/active_record.rb, line 74
def associate(parent, child, association_name, association_type)
  if association_type == :has_many
    associate_many(parent, child, association_name)
  elsif association_type == :habtm
    if parent.send(association_name).exists?(child.id)
      associate_many(parent, child, association_name)
    else
      parent.send(association_name) << child
    end
  elsif association_type == :has_one
    parent.send("#{association_name}=", child)
  elsif
    child.send("#{association_name}=", parent)
  end
end
average(scope, attr) click to toggle source

(see Adapters::Abstract#average)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 32
def average(scope, attr)
  scope.average(attr).to_f
end
count(scope, attr) click to toggle source

(see Adapters::Abstract#count)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 23
def count(scope, attr)
  if attr.to_sym == :total
    scope.distinct.count
  else
    scope.distinct.count(attr)
  end
end
create(model_class, create_params) click to toggle source

(see Adapters::Abstract#create)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 102
def create(model_class, create_params)
  instance = model_class.new(create_params)
  instance.save
  instance
end
destroy(model_class, id) click to toggle source

(see Adapters::Abstract#destroy)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 116
def destroy(model_class, id)
  instance = model_class.find(id)
  instance.destroy
  instance
end
disassociate(parent, child, association_name, association_type) click to toggle source

When a has_and_belongs_to_many relationship, we don't have a foreign key that can be null'd. Instead, go through the ActiveRecord API. @see Adapters::Abstract#disassociate

# File lib/jsonapi_compliable/adapters/active_record.rb, line 93
def disassociate(parent, child, association_name, association_type)
  if association_type == :habtm
    parent.send(association_name).delete(child)
  else
    # Nothing to do here, happened when we merged foreign key
  end
end
filter(scope, attribute, value) click to toggle source

(see Adapters::Abstract#filter)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 8
def filter(scope, attribute, value)
  scope.where(attribute => value)
end
maximum(scope, attr) click to toggle source

(see Adapters::Abstract#maximum)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 42
def maximum(scope, attr)
  scope.maximum(attr)
end
minimum(scope, attr) click to toggle source

(see Adapters::Abstract#minimum)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 47
def minimum(scope, attr)
  scope.minimum(attr)
end
order(scope, attribute, direction) click to toggle source

(see Adapters::Abstract#order)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 13
def order(scope, attribute, direction)
  scope.order(attribute => direction)
end
paginate(scope, current_page, per_page) click to toggle source

(see Adapters::Abstract#paginate)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 18
def paginate(scope, current_page, per_page)
  scope.page(current_page).per(per_page)
end
resolve(scope) click to toggle source

(see Adapters::Abstract#resolve)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 52
def resolve(scope)
  scope.to_a
end
sideloading_module() click to toggle source

(see Adapters::Abstract#sideloading_module)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 67
def sideloading_module
  JsonapiCompliable::Adapters::ActiveRecordSideloading
end
sum(scope, attr) click to toggle source

(see Adapters::Abstract#sum)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 37
def sum(scope, attr)
  scope.sum(attr)
end
transaction(model_class) { || ... } click to toggle source

Run this write request within an ActiveRecord transaction @param [Class] model_class The ActiveRecord class we are saving @return Result of yield @see Adapters::Abstract#transaction

# File lib/jsonapi_compliable/adapters/active_record.rb, line 60
def transaction(model_class)
  model_class.transaction do
    yield
  end
end
update(model_class, update_params) click to toggle source

(see Adapters::Abstract#update)

# File lib/jsonapi_compliable/adapters/active_record.rb, line 109
def update(model_class, update_params)
  instance = model_class.find(update_params.delete(:id))
  instance.update_attributes(update_params)
  instance
end

Private Instance Methods

associate_many(parent, child, association_name) click to toggle source
# File lib/jsonapi_compliable/adapters/active_record.rb, line 124
def associate_many(parent, child, association_name)
  parent.association(association_name).loaded!
  parent.association(association_name).add_to_target(child, :skip_callbacks)
end