Class: Wallaby::ActiveRecord::ModelServiceProvider::Querier

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/wallaby/active_record/model_service_provider/querier.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier/escaper.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier/wrapper.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier/transformer.rb

Overview

Query builder

Defined Under Namespace

Classes: Escaper, Transformer, Wrapper

Constant Summary collapse

TEXT_FIELDS =
%w[string text citext longtext tinytext mediumtext].freeze

Instance Method Summary collapse

Constructor Details

#initialize(model_decorator) ⇒ Querier

Returns a new instance of Querier.

Parameters:

  • model_decorator (Wallaby::ModelDecorator)


11
12
13
14
# File 'lib/adapters/wallaby/active_record/model_service_provider/querier.rb', line 11

def initialize(model_decorator)
  @model_decorator = model_decorator
  @model_class = @model_decorator.model_class
end

Instance Method Details

#search(params) ⇒ ActiveRecord::Relation

Extract the filter and query information from parameters ‘filter` and `q` respectively, and execute query based on the information.

Parameters:

  • params (ActionController::Parameters)

Returns:

  • (ActiveRecord::Relation)


21
22
23
24
25
26
27
28
# File 'lib/adapters/wallaby/active_record/model_service_provider/querier.rb', line 21

def search(params)
  filter_name, keywords, field_queries = extract(params)
  scope = filtered_by(filter_name)
  scope = preload_associations_for(scope)
  arel_query = text_search(keywords)
  arel_query = field_search(field_queries, arel_query)
  scope.where(arel_query) # rubocop:disable CodeReuse/ActiveRecord
end

#sort(sort_string, scope) ⇒ ActiveRecord::Relation

Extract the sorting from parameters ‘sort`, and combine with metadata option `:nulls` to execute order for the given scope.

Parameters:

  • sort_string (String)
  • scope (ActiveRecord::Relation)

Returns:

  • (ActiveRecord::Relation)


36
37
38
39
40
41
42
# File 'lib/adapters/wallaby/active_record/model_service_provider/querier.rb', line 36

def sort(sort_string, scope)
  return scope if sort_string.blank?

  sort_hash = normalize_sort(Sorting::HashBuilder.build(sort_string))
  sorting = Sorting::HashBuilder.to_str(sort_hash)
  scope.order(Arel.sql(sorting)) # rubocop:disable CodeReuse/ActiveRecord
end