Class: JsonapiCompliable::Adapters::ActiveRecord

Inherits:
Abstract
  • Object
show all
Defined in:
lib/jsonapi_compliable/adapters/active_record.rb

Overview

See Also:

Instance Method Summary collapse

Instance Method Details

#associate(parent, child, association_name, association_type) ⇒ Object

When a has_many relationship, we need to avoid Activerecord implicitly firing a query. Otherwise, simple assignment will do



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Float

Returns the average of the scope

Examples:

ActiveRecord default

def average(scope, attr)
  scope.average(attr).to_f
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Float)

    the average of the scope



32
33
34
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 32

def average(scope, attr)
  scope.average(attr).to_f
end

#count(scope, attr) ⇒ Numeric

Returns the count of the scope

Examples:

ActiveRecord default

def count(scope, attr)
  column = attr == :total ? :all : attr
  scope.uniq.count(column)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the count of the scope



23
24
25
26
27
28
29
# 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) ⇒ Object

Returns the model instance just created

Examples:

ActiveRecord default

def create(model_class, create_params)
  instance = model_class.new(create_params)
  instance.save
  instance
end

Parameters:

  • model_class (Class)

    The configured model class (see Resource.model)

  • create_params (Hash)

    Attributes + id

Returns:

  • the model instance just created

See Also:



102
103
104
105
106
# 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) ⇒ Object

Returns the model instance just destroyed

Examples:

ActiveRecord default

def destroy(model_class, id)
  instance = model_class.find(id)
  instance.destroy
  instance
end

Parameters:

  • model_class (Class)

    The configured model class (see Resource.model)

  • id (Integer)

    the id for this model

Returns:

  • the model instance just destroyed

See Also:



116
117
118
119
120
# 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) ⇒ Object

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.



93
94
95
96
97
98
99
# 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) ⇒ Object

Returns the scope

Examples:

ActiveRecord default

def filter(scope, attribute, value)
  scope.where(attribute => value)
end

Parameters:

  • scope

    The scope object we are chaining

  • attribute (Symbol)

    The attribute name we are filtering

  • value

    The corresponding query parameter value

Returns:

  • the scope



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

def filter(scope, attribute, value)
  scope.where(attribute => value)
end

#maximum(scope, attr) ⇒ Numeric

Returns the maximum value of the scope

Examples:

ActiveRecord default

def maximum(scope, attr)
  scope.maximum(attr)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the maximum value of the scope



42
43
44
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 42

def maximum(scope, attr)
  scope.maximum(attr)
end

#minimum(scope, attr) ⇒ Numeric

Returns the maximum value of the scope

Examples:

ActiveRecord default

def maximum(scope, attr)
  scope.maximum(attr)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the maximum value of the scope



47
48
49
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 47

def minimum(scope, attr)
  scope.minimum(attr)
end

#order(scope, attribute, direction) ⇒ Object

Returns the scope

Examples:

ActiveRecord default

def order(scope, attribute, direction)
  scope.order(attribute => direction)
end

Parameters:

  • scope

    The scope object we are chaining

  • attribute (Symbol)

    The attribute name we are sorting

  • direction (Symbol)

    The direction we are sorting (asc/desc)

Returns:

  • the scope



13
14
15
# 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) ⇒ Object

Returns the scope

Examples:

ActiveRecord default

# via kaminari gem
def paginate(scope, current_page, per_page)
  scope.page(current_page).per(per_page)
end

Parameters:

  • scope

    The scope object we are chaining

  • current_page (Integer)

    The current page number

  • per_page (Integer)

    The number of results per page

Returns:

  • the scope



18
19
20
# 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) ⇒ Object

Resolve the scope. This is where you'd actually fire SQL, actually make an HTTP call, etc.

Examples:

ActiveRecordDefault

def resolve(scope)
  scope.to_a
end

Suggested Customization

# When making a service call, we suggest this abstraction
# 'scope' here is a hash
def resolve(scope)
  # The implementation of .where can be whatever you want
  SomeModelClass.where(scope)
end

Parameters:

  • scope

    The scope object to resolve

Returns:

  • an array of Model instances

See Also:



52
53
54
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 52

def resolve(scope)
  scope.to_a
end

#sideloading_moduleObject

This module gets mixed in to Sideload classes This is where you define methods like has_many, belongs_to etc that wrap the lower-level Sideload#allow_sideload

Returns:

  • the module to mix in

See Also:



67
68
69
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 67

def sideloading_module
  JsonapiCompliable::Adapters::ActiveRecordSideloading
end

#sum(scope, attr) ⇒ Numeric

Returns the sum of the scope

Examples:

ActiveRecord default

def sum(scope, attr)
  scope.sum(attr)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the sum of the scope



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

def sum(scope, attr)
  scope.sum(attr)
end

#transaction(model_class) ⇒ Object

Run this write request within an ActiveRecord transaction

Parameters:

  • model_class (Class)

    The ActiveRecord class we are saving

Returns:

  • Result of yield

See Also:



60
61
62
63
64
# 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) ⇒ Object

Returns the model instance just created

Examples:

ActiveRecord default

def update(model_class, update_params)
  instance = model_class.find(update_params.delete(:id))
  instance.update_attributes(update_params)
  instance
end

Parameters:

  • model_class (Class)

    The configured model class (see Resource.model)

  • update_params (Hash)

    Attributes + id

Returns:

  • the model instance just created

See Also:



109
110
111
112
113
# 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