class Graphiti::Adapters::Abstract

Attributes

resource[R]

Public Class Methods

default_operators() click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 27
def self.default_operators
  {
    string: [
      :eq,
      :not_eq,
      :eql,
      :not_eql,
      :prefix,
      :not_prefix,
      :suffix,
      :not_suffix,
      :match,
      :not_match
    ],
    uuid: [:eq, :not_eq],
    enum: [:eq, :not_eq, :eql, :not_eql],
    integer_id: numerical_operators,
    integer: numerical_operators,
    big_decimal: numerical_operators,
    float: numerical_operators,
    boolean: [:eq],
    date: numerical_operators,
    datetime: numerical_operators,
    hash: [:eq],
    array: [:eq]
  }
end
new(resource) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 9
def initialize(resource)
  @resource = resource
end
numerical_operators() click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 414
def self.numerical_operators
  [:eq, :not_eq, :gt, :gte, :lt, :lte].freeze
end
sideloading_classes() click to toggle source

You want to override this! Map of association_type => sideload_class e.g. { has_many: Adapters::ActiveRecord::HasManySideload }

# File lib/graphiti/adapters/abstract.rb, line 17
def self.sideloading_classes
  {
    has_many: ::Graphiti::Sideload::HasMany,
    belongs_to: ::Graphiti::Sideload::BelongsTo,
    has_one: ::Graphiti::Sideload::HasOne,
    many_to_many: ::Graphiti::Sideload::ManyToMany,
    polymorphic_belongs_to: ::Graphiti::Sideload::PolymorphicBelongsTo
  }
end

Public Instance Methods

assign_attributes(model_instance, attributes) click to toggle source

TODO respond to and specific error

# File lib/graphiti/adapters/abstract.rb, line 393
def assign_attributes(model_instance, attributes)
  attributes.each_pair do |k, v|
    model_instance.send(:"#{k}=", v)
  end
end
associate(parent, child, association_name, association_type) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 369
def associate(parent, child, association_name, association_type)
  if activerecord_associate?(parent, child, association_name)
    activerecord_adapter.associate \
      parent, child, association_name, association_type
  elsif [:has_many, :many_to_many].include?(association_type)
    if parent.send(:"#{association_name}").nil?
      parent.send(:"#{association_name}=", [child])
    else
      parent.send(:"#{association_name}") << child
    end
  else
    parent.send(:"#{association_name}=", child)
  end
end
associate_all(parent, children, association_name, association_type) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 358
def associate_all(parent, children, association_name, association_type)
  if activerecord_associate?(parent, children[0], association_name)
    activerecord_adapter.associate_all parent,
      children, association_name, association_type
  else
    children.each do |c|
      associate(parent, c, association_name, association_type)
    end
  end
end
average(scope, attr) click to toggle source

@param scope the scope object we are chaining @param [Symbol] attr corresponding stat attribute name @return [Float] the average of the scope @example ActiveRecord default

def average(scope, attr)
  scope.average(attr).to_f
end
# File lib/graphiti/adapters/abstract.rb, line 278
def average(scope, attr)
  raise "you must override #average in an adapter subclass"
end
base_scope(model) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 227
def base_scope(model)
  raise "you must override #base_scope in an adapter subclass"
end
belongs_to_many_filter(sideload, scope, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 354
def belongs_to_many_filter(sideload, scope, value)
  raise "You must implement #belongs_to_many_filter in an adapter subclass"
end
build(model_class) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 388
def build(model_class)
  model_class.new
end
can_group?() click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 418
def can_group?
  false
end
close() click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 407
def close
end
count(scope, attr) click to toggle source

@param scope the scope object we are chaining @param [Symbol] attr corresponding stat attribute name @return [Numeric] the count of the scope @example ActiveRecord default

def count(scope, attr)
  column = attr == :total ? :all : attr
  scope.uniq.count(column)
end
# File lib/graphiti/adapters/abstract.rb, line 267
def count(scope, attr)
  raise "you must override #count in an adapter subclass"
end
destroy(model_instance) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 403
def destroy(model_instance)
  raise "you must override #destroy in an adapter subclass"
end
disassociate(parent, child, association_name, association_type) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 384
def disassociate(parent, child, association_name, association_type)
  raise "you must override #disassociate in an adapter subclass"
end
filter_big_decimal_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 151
def filter_big_decimal_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_eq)
end
filter_big_decimal_gt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 159
def filter_big_decimal_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_gt)
end
filter_big_decimal_gte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 163
def filter_big_decimal_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_gte)
end
filter_big_decimal_lt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 167
def filter_big_decimal_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_lt)
end
filter_big_decimal_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 171
def filter_big_decimal_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_lte)
end
filter_big_decimal_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 155
def filter_big_decimal_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_not_eq)
end
filter_boolean_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 223
def filter_boolean_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_boolean_eq)
end
filter_date_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 199
def filter_date_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_eq)
end
filter_date_gt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 207
def filter_date_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_gt)
end
filter_date_gte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 211
def filter_date_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_gte)
end
filter_date_lt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 215
def filter_date_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_lt)
end
filter_date_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 219
def filter_date_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_lte)
end
filter_date_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 203
def filter_date_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_not_eq)
end
filter_datetime_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 175
def filter_datetime_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_eq)
end
filter_datetime_gt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 183
def filter_datetime_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_gt)
end
filter_datetime_gte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 187
def filter_datetime_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_gte)
end
filter_datetime_lt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 191
def filter_datetime_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_lt)
end
filter_datetime_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 195
def filter_datetime_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_lte)
end
filter_datetime_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 179
def filter_datetime_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_not_eq)
end
filter_float_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 127
def filter_float_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_eq)
end
filter_float_gt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 135
def filter_float_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_gt)
end
filter_float_gte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 139
def filter_float_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_gte)
end
filter_float_lt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 143
def filter_float_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_lt)
end
filter_float_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 147
def filter_float_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_lte)
end
filter_float_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 131
def filter_float_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_not_eq)
end
filter_integer_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 103
def filter_integer_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_eq)
end
filter_integer_gt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 111
def filter_integer_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_gt)
end
filter_integer_gte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 115
def filter_integer_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_gte)
end
filter_integer_lt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 119
def filter_integer_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_lt)
end
filter_integer_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 123
def filter_integer_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_lte)
end
filter_integer_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 107
def filter_integer_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_not_eq)
end
filter_string_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 55
def filter_string_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_eq)
end
filter_string_eql(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 59
def filter_string_eql(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_eql)
end
filter_string_match(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 87
def filter_string_match(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_match)
end
filter_string_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 63
def filter_string_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_eq)
end
filter_string_not_eql(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 67
def filter_string_not_eql(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_eql)
end
filter_string_not_match(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 91
def filter_string_not_match(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_match)
end
filter_string_not_prefix(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 75
def filter_string_not_prefix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_prefix)
end
filter_string_not_suffix(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 83
def filter_string_not_suffix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_suffix)
end
filter_string_prefix(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 71
def filter_string_prefix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_prefix)
end
filter_string_suffix(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 79
def filter_string_suffix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_suffix)
end
filter_uuid_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 95
def filter_uuid_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_uuid_eq)
end
filter_uuid_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 99
def filter_uuid_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_uuid_not_eq)
end
maximum(scope, attr) click to toggle source

@param scope the scope object we are chaining @param [Symbol] attr corresponding stat attribute name @return [Numeric] the maximum value of the scope @example ActiveRecord default

def maximum(scope, attr)
  scope.maximum(attr)
end
# File lib/graphiti/adapters/abstract.rb, line 300
def maximum(scope, attr)
  raise "you must override #maximum in an adapter subclass"
end
minimum(scope, attr) click to toggle source

@param scope the scope object we are chaining @param [Symbol] attr corresponding stat attribute name @return [Numeric] the maximum value of the scope @example ActiveRecord default

def maximum(scope, attr)
  scope.maximum(attr)
end
# File lib/graphiti/adapters/abstract.rb, line 311
def minimum(scope, attr)
  raise "you must override #maximum in an adapter subclass"
end
order(scope, attribute, direction) click to toggle source

@param scope The scope object we are chaining @param [Symbol] attribute The attribute name we are sorting @param [Symbol] direction The direction we are sorting (asc/desc) @return the scope

@example ActiveRecord default

def order(scope, attribute, direction)
  scope.order(attribute => direction)
end
# File lib/graphiti/adapters/abstract.rb, line 240
def order(scope, attribute, direction)
  raise "you must override #order in an adapter subclass"
end
paginate(scope, current_page, per_page, offset) click to toggle source

@param scope The scope object we are chaining @param [Integer] current_page The current page number @param [Integer] per_page The number of results per page @param [Integer] offset The offset to start from @return the scope

@example ActiveRecord default

# via kaminari gem
def paginate(scope, current_page, per_page, offset)
  scope.page(current_page).per(per_page)
end
# File lib/graphiti/adapters/abstract.rb, line 255
def paginate(scope, current_page, per_page, offset)
  raise "you must override #paginate in an adapter subclass"
end
persistence_attributes(persistance, attributes) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 410
def persistence_attributes(persistance, attributes)
  attributes
end
resolve(scope) click to toggle source

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

@example ActiveRecordDefault

def resolve(scope)
  scope.to_a
end

@example 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

@see Adapters::ActiveRecord#resolve @param scope The scope object to resolve @return an array of Model instances

# File lib/graphiti/adapters/abstract.rb, line 350
def resolve(scope)
  scope
end
save(model_instance) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 399
def save(model_instance)
  raise "you must override #save in an adapter subclass"
end
sum(scope, attr) click to toggle source

@param scope the scope object we are chaining @param [Symbol] attr corresponding stat attribute name @return [Numeric] the sum of the scope @example ActiveRecord default

def sum(scope, attr)
  scope.sum(attr)
end
# File lib/graphiti/adapters/abstract.rb, line 289
def sum(scope, attr)
  raise "you must override #sum in an adapter subclass"
end
transaction(model_class) click to toggle source

This method must yield the code to run within the transaction. This method should roll back the transaction if an error is raised.

@param [Class] model_class The class we’re operating on @example ActiveRecord default

def transaction(model_class)
  model_class.transaction do
    yield
  end
end

@see Resource.model

# File lib/graphiti/adapters/abstract.rb, line 327
def transaction(model_class)
  raise "you must override #transaction in an adapter subclass, it must yield"
end

Private Instance Methods

activerecord_adapter() click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 424
def activerecord_adapter
  @activerecord_adapter ||=
    ::Graphiti::Adapters::ActiveRecord.new(resource)
end
activerecord_associate?(parent, child, association_name) click to toggle source
# File lib/graphiti/adapters/abstract.rb, line 429
def activerecord_associate?(parent, child, association_name)
  defined?(::ActiveRecord) &&
    parent.is_a?(::ActiveRecord::Base) &&
    parent.class.reflect_on_association(association_name)
end