class Graphiti::Adapters::ActiveRecord

Public Class Methods

sideloading_classes() click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 10
def self.sideloading_classes
  {
    has_many: Graphiti::Adapters::ActiveRecord::HasManySideload,
    has_one: Graphiti::Adapters::ActiveRecord::HasOneSideload,
    belongs_to: Graphiti::Adapters::ActiveRecord::BelongsToSideload,
    many_to_many: Graphiti::Adapters::ActiveRecord::ManyToManySideload
  }
end

Public Instance Methods

associate(parent, child, association_name, association_type) click to toggle source
Calls superclass method Graphiti::Adapters::Abstract#associate
# File lib/graphiti/adapters/active_record.rb, line 262
def associate(parent, child, association_name, association_type)
  if activerecord_associate?(parent, child, association_name)
    association = parent.association(association_name)
    association.loaded!
    association.instance_variable_set(:@target, child)
  else
    super
  end
end
associate_all(parent, children, association_name, association_type) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 240
def associate_all(parent, children, association_name, association_type)
  if activerecord_associate?(parent, children[0], association_name)
    association = parent.association(association_name)
    association.loaded!

    children.each do |child|
      if association_type == :many_to_many &&
          [:create, :update].include?(Graphiti.context[:namespace]) &&
          !parent.send(association_name).exists?(child.id) &&
          child.errors.blank?
        parent.send(association_name) << child
      else
        target = association.instance_variable_get(:@target)
        target |= [child]
        association.instance_variable_set(:@target, target)
      end
    end
  else
    super
  end
end
average(scope, attr) click to toggle source

(see Adapters::Abstract#average)

# File lib/graphiti/adapters/active_record.rb, line 206
def average(scope, attr)
  scope.average(attr).to_f
end
base_scope(model) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 179
def base_scope(model)
  model.all
end
can_group?() click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 314
def can_group?
  true
end
close() click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 306
def close
  if ::ActiveRecord.version > Gem::Version.new("7.2")
    ::ActiveRecord::Base.connection_handler.clear_active_connections!
  else
    ::ActiveRecord::Base.clear_active_connections!
  end
end
count(scope, attr) click to toggle source

(see Adapters::Abstract#count)

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

(see Adapters::Abstract#create)

# File lib/graphiti/adapters/active_record.rb, line 283
def create(model_class, create_params)
  instance = model_class.new(create_params)
  instance.save
  instance
end
destroy(model_instance) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 301
def destroy(model_instance)
  model_instance.destroy
  model_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/graphiti/adapters/active_record.rb, line 275
def disassociate(parent, child, association_name, association_type)
  if association_type == :many_to_many
    parent.send(association_name).delete(child)
  end
  # Nothing to do in the else case, happened when we merged foreign key
end
filter_big_decimal_eq(scope, attribute, value)
Alias for: filter_eq
filter_big_decimal_gt(scope, attribute, value)
Alias for: filter_gt
filter_big_decimal_gte(scope, attribute, value)
Alias for: filter_gte
filter_big_decimal_lt(scope, attribute, value)
Alias for: filter_lt
filter_big_decimal_lte(scope, attribute, value)
Alias for: filter_lte
filter_big_decimal_not_eq(scope, attribute, value)
Alias for: filter_not_eq
filter_boolean_eq(scope, attribute, value)
Alias for: filter_eq
filter_boolean_not_eq(scope, attribute, value)
Alias for: filter_not_eq
filter_date_eq(scope, attribute, value)
Alias for: filter_eq
filter_date_gt(scope, attribute, value)
Alias for: filter_gt
filter_date_gte(scope, attribute, value)
Alias for: filter_gte
filter_date_lt(scope, attribute, value)
Alias for: filter_lt
filter_date_lte(scope, attribute, value)
Alias for: filter_lte
filter_date_not_eq(scope, attribute, value)
Alias for: filter_not_eq
filter_datetime_eq(scope, attribute, value, is_not: false) click to toggle source

Ensure fractional seconds don’t matter

# File lib/graphiti/adapters/active_record.rb, line 163
def filter_datetime_eq(scope, attribute, value, is_not: false)
  ranges = value.map { |v| (v..v + 1.second - 0.00000001) unless v.nil? }
  clause = {attribute => ranges}
  is_not ? scope.where.not(clause) : scope.where(clause)
end
filter_datetime_gt(scope, attribute, value)
Alias for: filter_gt
filter_datetime_gte(scope, attribute, value)
Alias for: filter_gte
filter_datetime_lt(scope, attribute, value)
Alias for: filter_lt
filter_datetime_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 173
def filter_datetime_lte(scope, attribute, value)
  value = value.map { |v| v + 1.second - 0.00000001 }
  column = scope.klass.arel_table[attribute]
  scope.where(column.lteq_any(value))
end
filter_datetime_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 169
def filter_datetime_not_eq(scope, attribute, value)
  filter_datetime_eq(scope, attribute, value, is_not: true)
end
filter_enum_eq(scope, attribute, value)
Alias for: filter_eq
filter_enum_eql(scope, attribute, value)
Alias for: filter_eq
filter_enum_not_eq(scope, attribute, value)
Alias for: filter_not_eq
filter_enum_not_eql(scope, attribute, value)
Alias for: filter_not_eq
filter_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 19
def filter_eq(scope, attribute, value)
  scope.where(attribute => value)
end
filter_float_eq(scope, attribute, value)
Alias for: filter_eq
filter_float_gt(scope, attribute, value)
Alias for: filter_gt
filter_float_gte(scope, attribute, value)
Alias for: filter_gte
filter_float_lt(scope, attribute, value)
Alias for: filter_lt
filter_float_lte(scope, attribute, value)
Alias for: filter_lte
filter_float_not_eq(scope, attribute, value)
Alias for: filter_not_eq
filter_gt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 123
def filter_gt(scope, attribute, value)
  column = column_for(scope, attribute)
  scope.where(column.gt_any(value))
end
filter_gte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 133
def filter_gte(scope, attribute, value)
  column = column_for(scope, attribute)
  scope.where(column.gteq_any(value))
end
filter_integer_eq(scope, attribute, value)
Alias for: filter_eq
filter_integer_gt(scope, attribute, value)
Alias for: filter_gt
filter_integer_gte(scope, attribute, value)
Alias for: filter_gte
filter_integer_lt(scope, attribute, value)
Alias for: filter_lt
filter_integer_lte(scope, attribute, value)
Alias for: filter_lte
filter_integer_not_eq(scope, attribute, value)
Alias for: filter_not_eq
filter_lt(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 143
def filter_lt(scope, attribute, value)
  column = column_for(scope, attribute)
  scope.where(column.lt_any(value))
end
filter_lte(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 153
def filter_lte(scope, attribute, value)
  column = column_for(scope, attribute)
  scope.where(column.lteq_any(value))
end
filter_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 31
def filter_not_eq(scope, attribute, value)
  scope.where.not(attribute => value)
end
filter_string_eq(scope, attribute, value, is_not: false) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 43
def filter_string_eq(scope, attribute, value, is_not: false)
  column = column_for(scope, attribute)
  clause = column.lower.eq_any(value.map(&:downcase))
  is_not ? scope.where.not(clause) : scope.where(clause)
end
filter_string_eql(scope, attribute, value, is_not: false) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 49
def filter_string_eql(scope, attribute, value, is_not: false)
  clause = {attribute => value}
  is_not ? scope.where.not(clause) : scope.where(clause)
end
filter_string_match(scope, attribute, value, is_not: false) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 66
def filter_string_match(scope, attribute, value, is_not: false)
  clause = sanitized_like_for(scope, attribute, value) { |v|
    "%#{v}%"
  }
  is_not ? scope.where.not(clause) : scope.where(clause)
end
filter_string_not_eq(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 54
def filter_string_not_eq(scope, attribute, value)
  filter_string_eq(scope, attribute, value, is_not: true)
end
filter_string_not_eql(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 58
def filter_string_not_eql(scope, attribute, value)
  filter_string_eql(scope, attribute, value, is_not: true)
end
filter_string_not_match(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 119
def filter_string_not_match(scope, attribute, value)
  filter_string_match(scope, attribute, value, is_not: true)
end
filter_string_not_prefix(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 111
def filter_string_not_prefix(scope, attribute, value)
  filter_string_prefix(scope, attribute, value, is_not: true)
end
filter_string_not_suffix(scope, attribute, value) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 115
def filter_string_not_suffix(scope, attribute, value)
  filter_string_suffix(scope, attribute, value, is_not: true)
end
filter_string_prefix(scope, attribute, value, is_not: false) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 73
def filter_string_prefix(scope, attribute, value, is_not: false)
  clause = sanitized_like_for(scope, attribute, value) { |v|
    "#{v}%"
  }
  is_not ? scope.where.not(clause) : scope.where(clause)
end
filter_string_suffix(scope, attribute, value, is_not: false) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 80
def filter_string_suffix(scope, attribute, value, is_not: false)
  clause = sanitized_like_for(scope, attribute, value) { |v|
    "%#{v}"
  }
  is_not ? scope.where.not(clause) : scope.where(clause)
end
filter_uuid_eq(scope, attribute, value)
Alias for: filter_eq
filter_uuid_not_eq(scope, attribute, value)
Alias for: filter_not_eq
group(scope, attribute) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 318
def group(scope, attribute)
  scope.group(attribute)
end
maximum(scope, attr) click to toggle source

(see Adapters::Abstract#maximum)

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

(see Adapters::Abstract#minimum)

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

(see Adapters::Abstract#order)

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

(see Adapters::Abstract#paginate)

# File lib/graphiti/adapters/active_record.rb, line 189
def paginate(scope, current_page, per_page, offset)
  scope = scope.page(current_page) if current_page
  scope = scope.per(per_page) if per_page
  scope = scope.padding(offset) if offset
  scope
end
resolve(scope) click to toggle source

(see Adapters::Abstract#resolve)

# File lib/graphiti/adapters/active_record.rb, line 226
def resolve(scope)
  scope.to_a
end
save(model_instance) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 296
def save(model_instance)
  model_instance.save
  model_instance
end
sum(scope, attr) click to toggle source

(see Adapters::Abstract#sum)

# File lib/graphiti/adapters/active_record.rb, line 211
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/graphiti/adapters/active_record.rb, line 234
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/graphiti/adapters/active_record.rb, line 290
def update(model_class, update_params)
  instance = model_class.find(update_params.only(:id))
  instance.update_attributes(update_params.except(:id))
  instance
end

Private Instance Methods

column_for(scope, name) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 324
def column_for(scope, name)
  table = scope.klass.arel_table
  if (other = scope.attribute_alias(name))
    table[other]
  else
    table[name]
  end
end
sanitized_like_for(scope, attribute, value, &block) click to toggle source
# File lib/graphiti/adapters/active_record.rb, line 333
def sanitized_like_for(scope, attribute, value, &block)
  escape_char = "\\"
  column = column_for(scope, attribute)
  map = value.map { |v|
    v = v.downcase
    v = Sanitizer.sanitize_like(v, escape_char)
    block.call v
  }

  column.lower.matches_any(map, escape_char, true)
end