Module: HasHelpers::Search::Provider
- Included in:
- Elasticsearch, SqlSearch
- Defined in:
- app/lib/has_helpers/search/provider.rb
Overview
Examples
HasHelpers::Search::Provider.new(controller_instance).results
Constant Summary collapse
- PAGINATION_ATTRS =
[:current_page, :per_page, :total_entries]
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
Class Method Summary collapse
-
.owner_relation_from_scope(scope) ⇒ Object
Returns a triple of scope relation name, owner relation name, and an ActiveRecord::Relation object built from the given scope (Relation or model class) which includes the joined owner view, as well as the relation names related to the owner view.
Instance Method Summary collapse
- #initialize(controller = nil) ⇒ Object
- #results ⇒ Object
- #search_options(prefix, _screen = prefix, _query) ⇒ Object
-
#search_prefix ⇒ Object
Returns.
-
#search_screen(prefix = search_prefix) ⇒ Object
Returns.
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
8 9 10 |
# File 'app/lib/has_helpers/search/provider.rb', line 8 def controller @controller end |
Class Method Details
.owner_relation_from_scope(scope) ⇒ Object
Returns a triple of scope relation name, owner relation name, and
an ActiveRecord::Relation object built from the given scope (Relation
or model class) which includes the joined owner view, as well as the
relation names related to the owner view. If no owner view is detected
then nil is returned as the relation.
Examples
scope_with_owner_view(Advisor) # => ["advisors", "owner_advisors_view", <#ActiveRecord::Relation>]
# Hierarchy does _not_ have an owner view.
scope_with_owner_view(Hierarchy) # => ["hierarchies", "owner_hierarchies_view", nil]
74 75 76 77 78 79 80 81 82 83 |
# File 'app/lib/has_helpers/search/provider.rb', line 74 def self.owner_relation_from_scope(scope) scope_relation_name = scope.table_name owner_relation_name = "owner_#{ scope_relation_name }_view" if (::ActiveRecord::Base.connection.execute("SELECT 1 FROM #{ owner_relation_name } LIMIT 1") rescue false) # Check if the owner view is present. owner_relation = scope.joins("LEFT OUTER JOIN #{ owner_relation_name } ON #{ owner_relation_name }.id = #{ scope_relation_name }.id") owner_relation.select_values = ["#{ scope_relation_name }.*"] end [scope_relation_name, owner_relation_name, owner_relation] end |
Instance Method Details
#initialize(controller = nil) ⇒ Object
10 11 12 |
# File 'app/lib/has_helpers/search/provider.rb', line 10 def initialize(controller = nil) @controller = controller end |
#results ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/lib/has_helpers/search/provider.rb', line 14 def results # Prefix prefix = search_prefix if prefix # Screen screen = search_screen(prefix) # Sanitize prefix for use as an instance variable name (:: is not valid) ivar_prefix = prefix.gsub("::", "") # Search setup controller.instance_variable_set("@#{ ivar_prefix }_query", params["search_#{ prefix }_query"][0..255]) controller.instance_variable_set("@#{ ivar_prefix }_screen", screen) # Query sanitized_query = sanitize_query(controller.instance_variable_get("@#{ ivar_prefix }_query")) # Options = (prefix, screen, sanitized_query) # Perform search query search(prefix, screen, sanitized_query, ) end end |
#search_options(prefix, _screen = prefix, _query) ⇒ Object
54 55 56 57 58 59 |
# File 'app/lib/has_helpers/search/provider.rb', line 54 def (prefix, _screen = prefix, _query) { page: (params[:page] || 1).to_i, per_page: (params[:per_page] || 15).to_i } end |
#search_prefix ⇒ Object
Returns
42 43 44 45 46 |
# File 'app/lib/has_helpers/search/provider.rb', line 42 def search_prefix # Fallback on getting the prefix from the first parameter key which begins wih "search_". key, _ = params.to_h.detect { |k, v| v == "Search" && k.start_with?("search_") } key[/\Asearch_(.*)/, 1] if key end |
#search_screen(prefix = search_prefix) ⇒ Object
Returns
50 51 52 |
# File 'app/lib/has_helpers/search/provider.rb', line 50 def search_screen(prefix = search_prefix) params["search_#{ prefix }_screen"] || prefix # Get screen from params if block is missing or returned nil. end |