module Scopiform::AssociationScopes::ClassMethods

Public Instance Methods

reflection_added(_name, reflection) click to toggle source
# File lib/scopiform/association_scopes.rb, line 15
def reflection_added(_name, reflection)
  setup_association_auto_scopes(reflection)
end

Private Instance Methods

setup_association_auto_scopes(association) click to toggle source
# File lib/scopiform/association_scopes.rb, line 27
def setup_association_auto_scopes(association)
  auto_scope_add(
    association.name,
    proc { |value, ctx: nil|
      Utilities.association_scope(self, association, :apply_filters, value, ctx: ctx)
    },
    suffix: '_is',
    argument_type: :hash
  )

  # Sorting
  auto_scope_add(
    association.name,
    proc { |value, ctx: nil|
      Utilities.association_scope(self, association, :apply_sorts, value, ctx: ctx)
    },
    prefix: 'sort_by_',
    argument_type: :hash,
    type: :sort
  )

  # Grouping
  auto_scope_add(
    association.name,
    proc { |value, ctx: nil|
      next all unless Utilities.truthy_hash(value)

      Utilities.association_scope(self, association, :apply_groupings, value, ctx: ctx)
    },
    prefix: 'group_by_',
    argument_type: :hash,
    type: :group
  )
end
setup_associations_auto_scopes() click to toggle source
# File lib/scopiform/association_scopes.rb, line 21
def setup_associations_auto_scopes
  reflect_on_all_associations.each do |association|
    setup_association_auto_scopes(association)
  end
end