Class: Scoping::ScopeComposer

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/scoping/scope_composer.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_scope, table = nil) ⇒ ScopeComposer

Initialize new instance with given scope and criteria table.

Parameters:

  • base_scope (ActiveRecord::Base)
  • table (Hash) (defaults to: nil)


45
46
47
48
49
50
51
# 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

Instance Method Details

#allActiveRecord::Relation

Apply the scopes and return all the matching records.

Returns:

  • (ActiveRecord::Relation)


56
57
58
59
# File 'lib/scoping/scope_composer.rb', line 56

def all
  return @current_scope.all if fields.empty?
  apply_fields_on_current_scope
end

#countFixnum

Apply the scopes and return count.

Returns:

  • (Fixnum)


79
80
81
82
# File 'lib/scoping/scope_composer.rb', line 79

def count
  apply_fields_on_current_scope
  @current_scope.count
end

#decorateActiveRecord::Relation

Apply the scopes and return decorated the matching records.

Returns:

  • (ActiveRecord::Relation)


64
65
66
# File 'lib/scoping/scope_composer.rb', line 64

def decorate
  all.decorate
end

#paginate(*args) ⇒ Array

Apply the scopes and return paginated records.

Returns:

  • (Array)


71
72
73
74
# File 'lib/scoping/scope_composer.rb', line 71

def paginate(*args)
  apply_fields_on_current_scope
  @current_scope.paginate(*args)
end

#scopeActiveRecord::Relation

Apply the scopes and return the resulting scope.

Returns:

  • (ActiveRecord::Relation)


87
88
89
90
# File 'lib/scoping/scope_composer.rb', line 87

def scope
  apply_fields_on_current_scope
  @current_scope
end