Module: Typus::Orm::Base::Search
- Included in:
- ActiveRecord::Search, Mongoid::Search
- Defined in:
- lib/typus/orm/base/search.rb
Instance Method Summary collapse
- #build_boolean_conditions(key, value) ⇒ Object
-
#build_conditions(params) ⇒ Object
To build conditions we accept only model fields and the search param.
- #build_date_conditions(key, value) ⇒ Object
- #build_datetime_conditions(key, value) ⇒ Object (also: #build_time_conditions)
- #build_filter_interval(interval, key) ⇒ Object
-
#build_has_many_conditions(key, value) ⇒ Object
TODO: Detect the primary_key for this object.
- #build_my_joins(params) ⇒ Object
- #build_search_conditions(key, value) ⇒ Object
- #build_string_conditions(key, value) ⇒ Object (also: #build_integer_conditions, #build_belongs_to_conditions)
Instance Method Details
#build_boolean_conditions(key, value) ⇒ Object
10 11 12 |
# File 'lib/typus/orm/base/search.rb', line 10 def build_boolean_conditions(key, value) { key => (value == 'true') ? true : false } end |
#build_conditions(params) ⇒ Object
To build conditions we accept only model fields and the search param.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/typus/orm/base/search.rb', line 60 def build_conditions(params) Array.new.tap do |conditions| query_params = params.dup query_params.reject! do |k, v| !model_fields.keys.include?(k.to_sym) && !model_relationships.keys.include?(k.to_sym) && !(k.to_sym == :search) end query_params.compact.each do |key, value| filter_type = model_fields[key.to_sym] || model_relationships[key.to_sym] || key conditions << send("build_#{filter_type}_conditions", key, value) end end end |
#build_date_conditions(key, value) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/typus/orm/base/search.rb', line 29 def build_date_conditions(key, value) tomorrow = 0.days.ago.tomorrow.to_date interval = case value when 'today' then 0.days.ago.to_date..tomorrow when 'last_few_days' then 3.days.ago.to_date..tomorrow when 'last_7_days' then 6.days.ago.to_date..tomorrow when 'last_30_days' then 30.days.ago.to_date..tomorrow end build_filter_interval(interval, key) end |
#build_datetime_conditions(key, value) ⇒ Object Also known as: build_time_conditions
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/typus/orm/base/search.rb', line 14 def build_datetime_conditions(key, value) tomorrow = Time.zone.now.beginning_of_day.tomorrow interval = case value when 'today' then 0.days.ago.beginning_of_day..tomorrow when 'last_few_days' then 3.days.ago.beginning_of_day..tomorrow when 'last_7_days' then 6.days.ago.beginning_of_day..tomorrow when 'last_30_days' then 30.days.ago.beginning_of_day..tomorrow end build_filter_interval(interval, key) end |
#build_filter_interval(interval, key) ⇒ Object
42 43 44 |
# File 'lib/typus/orm/base/search.rb', line 42 def build_filter_interval(interval, key) raise "Not implemented!" end |
#build_has_many_conditions(key, value) ⇒ Object
TODO: Detect the primary_key for this object.
54 55 56 |
# File 'lib/typus/orm/base/search.rb', line 54 def build_has_many_conditions(key, value) ["#{key}.id = ?", value] end |
#build_my_joins(params) ⇒ Object
77 78 79 80 81 |
# File 'lib/typus/orm/base/search.rb', line 77 def build_my_joins(params) query_params = params.dup query_params.reject! { |k, v| !model_relationships.keys.include?(k.to_sym) } query_params.compact.map { |k, v| k.to_sym } end |
#build_search_conditions(key, value) ⇒ Object
6 7 8 |
# File 'lib/typus/orm/base/search.rb', line 6 def build_search_conditions(key, value) raise "Not implemented!" end |
#build_string_conditions(key, value) ⇒ Object Also known as: build_integer_conditions, build_belongs_to_conditions
46 47 48 |
# File 'lib/typus/orm/base/search.rb', line 46 def build_string_conditions(key, value) { key => value } end |