Class: DataTables::Modules::Search
- Inherits:
-
Object
- Object
- DataTables::Modules::Search
- Defined in:
- lib/data_tables/modules/search.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #build_search(model, in_hash, join_hash = nil) ⇒ Object
-
#initialize(scope, request_parameters) ⇒ Search
constructor
A new instance of Search.
- #search ⇒ Object
Constructor Details
#initialize(scope, request_parameters) ⇒ Search
Returns a new instance of Search.
7 8 9 10 |
# File 'lib/data_tables/modules/search.rb', line 7 def initialize(scope, request_parameters) @scope = scope @request_parameters = request_parameters end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
5 6 7 |
# File 'lib/data_tables/modules/search.rb', line 5 def context @context end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
5 6 7 |
# File 'lib/data_tables/modules/search.rb', line 5 def scope @scope end |
Instance Method Details
#build_search(model, in_hash, join_hash = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/data_tables/modules/search.rb', line 27 def build_search(model, in_hash, join_hash = nil) # Tuple! return in_hash.inject([]) { |queries, (column, query)| case query when Hash if (assoc = model.reflect_on_association(column)) new_queries, join_class = build_search(assoc.klass, query) join_hash = join_class.nil? ? [column] : {column => join_class} queries << new_queries.reduce(:and) else warn("trying to reflect on #{column} but #{model.class.name} has no such association.") end else search_by_type(model, column, query) do |result| queries << result end end queries }, join_hash end |
#search ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/data_tables/modules/search.rb', line 12 def search default_search = @request_parameters.dig(:search, :value) model = @scope.try(:model) || @scope columns = searchable_columns(default_search) searches = DataTables::Responder.flat_keys_to_nested columns search_by, join_hash = build_search(model, searches) @scope = @scope.joins(join_hash) @scope.where(search_by.reduce(:and)) end |