class Scoping::ScopeComposer

Public Class Methods

new(base_scope, table = nil) click to toggle source

Initialize new instance with given scope and criteria table.

@param [ActiveRecord::Base] base_scope @param [Hash] table

Calls superclass method
# File lib/scoping/scope_composer.rb, line 45
def initialize(base_scope, table = nil)
  @base_scope = base_scope
  @current_scope = base_scope
  @applied = false
  super({})
  @table = table if table
end

Public Instance Methods

all() click to toggle source

Apply the scopes and return all the matching records.

@return [ActiveRecord::Relation]

# File lib/scoping/scope_composer.rb, line 56
def all
  return @current_scope.all if fields.empty?
  apply_fields_on_current_scope
end
count() click to toggle source

Apply the scopes and return count.

@return [Fixnum]

# File lib/scoping/scope_composer.rb, line 79
def count
  apply_fields_on_current_scope
  @current_scope.count
end
decorate() click to toggle source

Apply the scopes and return decorated the matching records.

@return [ActiveRecord::Relation]

# File lib/scoping/scope_composer.rb, line 64
def decorate
  all.decorate
end
paginate(*args) click to toggle source

Apply the scopes and return paginated records.

@return [Array]

# File lib/scoping/scope_composer.rb, line 71
def paginate(*args)
  apply_fields_on_current_scope
  @current_scope.paginate(*args)
end
scope() click to toggle source

Apply the scopes and return the resulting scope.

@return [ActiveRecord::Relation]

# File lib/scoping/scope_composer.rb, line 87
def scope
  apply_fields_on_current_scope
  @current_scope
end

Private Instance Methods

apply_field_on_current_scope(field, value) click to toggle source

Applies given scope with given argument to current scope

@param [String] field @param [Object] value

# File lib/scoping/scope_composer.rb, line 113
def apply_field_on_current_scope(field, value)
  arity = arity_for(field)
  if arity == 0
    if value == "1" || value == true
      @current_scope = @current_scope.public_send field
    end
  else
    @current_scope = @current_scope.public_send field, value
  end
end
apply_fields_on_current_scope() click to toggle source

Applies assigned scopes and returns the result scope.

The scope is called only when the value (argument for scope) is non-empty value.

# File lib/scoping/scope_composer.rb, line 98
def apply_fields_on_current_scope
  return @current_scope if @applied
  fields.each do |field|
    value = public_send(field)
    next unless value.present?
    apply_field_on_current_scope field, value
  end
  @applied = true
  @current_scope
end
arity_for(scope) click to toggle source

Returns arity for given scope.

@return [Fixnum]

# File lib/scoping/scope_composer.rb, line 127
def arity_for(scope)
  if @base_scope.respond_to?(:klass)
    @base_scope.klass
  else
    @base_scope
  end.method(scope).arity
end
fields() click to toggle source

Return the filter fields.

@return [Array]

# File lib/scoping/scope_composer.rb, line 138
def fields
  @table.keys
end