Class: ActiveTree::ModelQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/active_tree/queries/model_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Query

#valid?

Constructor Details

#initialize(initial_scope = ::ActiveTree::Model.all) ⇒ ModelQuery

Returns a new instance of ModelQuery.



4
5
6
# File 'lib/active_tree/queries/model_query.rb', line 4

def initialize(initial_scope = ::ActiveTree::Model.all)
	@initial_scope = initial_scope
end

Instance Attribute Details

#initial_scopeObject

Returns the value of attribute initial_scope.



2
3
4
# File 'lib/active_tree/queries/model_query.rb', line 2

def initial_scope
  @initial_scope
end

Instance Method Details

#by_search(scope, search = nil) ⇒ Object

Partial search by name



21
22
23
# File 'lib/active_tree/queries/model_query.rb', line 21

def by_search(scope, search = nil)
	search ? scope.where("lower(name) like ?", "%#{search.downcase}%") : scope
end

#call(params) ⇒ Object

initialize



8
9
10
11
12
13
14
15
16
17
# File 'lib/active_tree/queries/model_query.rb', line 8

def call(params)
	scope = simple_search(initial_scope, :id, params[:id])
	[:owner_type, :owner_id, :status, :data_external_id, :data_provider, :type, :parent_entity_id, :parent_entity_type, :path_slug].each do |query|
 scope = simple_search(scope, query, params[query])
	end

	scope = by_search(scope, params[:search])

	scope
end

#simple_search(scope, attribute = nil, value = nil) ⇒ Object

Simple search by attribute and exact value



27
28
29
# File 'lib/active_tree/queries/model_query.rb', line 27

def simple_search(scope, attribute = nil, value = nil)
	valid?(attribute, value) ? scope.where(attribute => value) : scope
end