Class: ScopedSearch::QueryConditionsBuilder
- Inherits:
-
Object
- Object
- ScopedSearch::QueryConditionsBuilder
- Defined in:
- lib/scoped_search/query_conditions_builder.rb
Class Method Summary collapse
-
.build_query(search_conditions, query_fields) ⇒ Object
Builds the query string by calling the build method on a new instances of QueryConditionsBuilder.
Instance Method Summary collapse
-
#build(search_conditions, query_fields) ⇒ Object
Build the query based on the search conditions and the fields to query.
-
#initialize ⇒ QueryConditionsBuilder
constructor
Initializes the default class variables.
Constructor Details
#initialize ⇒ QueryConditionsBuilder
Initializes the default class variables.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/scoped_search/query_conditions_builder.rb', line 11 def initialize @query_fields = nil @query_params = {} @sql_like = 'LIKE' if ActiveRecord::Base.connected? and ActiveRecord::Base.connection.adapter_name.downcase == 'postgresql' @sql_like = 'ILIKE' end end |
Class Method Details
.build_query(search_conditions, query_fields) ⇒ Object
Builds the query string by calling the build method on a new instances of QueryConditionsBuilder.
6 7 8 |
# File 'lib/scoped_search/query_conditions_builder.rb', line 6 def self.build_query(search_conditions, query_fields) self.new.build(search_conditions, query_fields) end |
Instance Method Details
#build(search_conditions, query_fields) ⇒ Object
Build the query based on the search conditions and the fields to query.
Hash query_options : A hash of fields and field types.
Example:
search_conditions = [["Wes", :like], ["Hays", :not], ["Hello World", :like], ["Goodnight Moon", :not],
["Bob OR Wes", :or], ["Happy cow OR Sad Frog", :or], ["Man made OR Dogs", :or],
["Cows OR Frog Toys", :or], ['9/28/1980, :datetime]]
query_fields = {:first_name => :string, :created_at => :datetime}
Exceptons :
1) If search_conditions is not an array
2) If query_fields is not a Hash
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/scoped_search/query_conditions_builder.rb', line 37 def build(search_conditions, query_fields) raise 'search_conditions must be a hash' unless search_conditions.class.to_s == 'Array' raise 'query_fields must be a hash' unless query_fields.class.to_s == 'Hash' @query_fields = query_fields conditions = [] search_conditions.each_with_index do |search_condition, index| keyword_name = "keyword_#{index}".to_sym conditions << case search_condition.last # :like also handles integers when :like then like_condition(keyword_name, search_condition.first) when :not then not_like_condition(keyword_name, search_condition.first) when :or then or_condition(keyword_name, search_condition.first) when :less_than_date then less_than_date(keyword_name, search_condition.first) when :less_than_or_equal_to_date then less_than_or_equal_to_date(keyword_name, search_condition.first) when :as_of_date then as_of_date(keyword_name, search_condition.first) when :greater_than_date then greater_than_date(keyword_name, search_condition.first) when :greater_than_or_equal_to_date then greater_than_or_equal_to_date(keyword_name, search_condition.first) when :between_dates then between_dates(keyword_name, search_condition.first) end end [conditions.compact.join(' AND '), @query_params] end |