Module: FmStore::Criterion::Inclusion
- Included in:
- FmStore::Criteria
- Defined in:
- lib/fm_store/criterion/inclusion.rb
Instance Method Summary collapse
- #custom_query(params = {}) ⇒ Object
- #fm_id(record_id) ⇒ Object
- #id(record_id) ⇒ Object
-
#in(params = {}) ⇒ Object
-query Job.in(“status” => [“open”, “pending”], :category => [“Account”, “IT”]) Operator not allowed in -findquery query command, so do not write this Job.in(“status.eq” => [“closed”, “pending”]).
- #search(params = {}) ⇒ Object
-
#where(params = {}, logical_and = true) ⇒ Object
Adds a
where
criterion.
Instance Method Details
#custom_query(params = {}) ⇒ Object
120 121 122 123 |
# File 'lib/fm_store/criterion/inclusion.rb', line 120 def custom_query(params = {}) update_params(params) self end |
#fm_id(record_id) ⇒ Object
91 92 93 94 95 |
# File 'lib/fm_store/criterion/inclusion.rb', line 91 def fm_id(record_id) update_params("-recid" => record_id) self.limit(1).first end |
#id(record_id) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fm_store/criterion/inclusion.rb', line 79 def id(record_id) return nil if record_id.blank? if klass.identity == "-recid" update_params("-recid" => record_id) else update_params(klass.identity => "=#{record_id}") end self.limit(1).first end |
#in(params = {}) ⇒ Object
-query Job.in(“status” => [“open”, “pending”], :category => [“Account”, “IT”]) Operator not allowed in -findquery query command, so do not write this Job.in(“status.eq” => [“closed”, “pending”])
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fm_store/criterion/inclusion.rb', line 101 def in(params = {}) accepted_params = {} params.each do |field, value| field = field.to_s # Convert to an Array if it is String value = Array(value) if value.is_a?(String) fm_name = klass.find_fm_name(field) accepted_params[fm_name] = value if fm_name end @raw_params = accepted_params update_params(assemble_query(accepted_params)) self end |
#search(params = {}) ⇒ Object
35 36 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 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fm_store/criterion/inclusion.rb', line 35 def search(params = {}) current_page = params[:page] || 1 if @params.size.zero? if params[:q].present? query = params[:q] p = klass.searchable_fields.inject({}) { |h, name| h[name] = query; h } where(p, false).paginate(:page => current_page) # Logical OR else where.paginate(:page => current_page) end else # we have constraint, but it can be from +where+ or +in+ # current implementation only take into account +where+ if params[:q].present? c = find_query ? @raw_params : @params # we need to get the raw params here query = params[:q] p = klass.searchable_fields.inject({}) { |h, name| h[name] = query; h } accepted_params = {} p.each do |field, value| field = field.to_s fm_name = klass.find_fm_name(field) accepted_params[fm_name] = value if fm_name end self.find_query = true final = assemble_constraint_query(c, accepted_params) update_params(final) paginate(:page => current_page) else if find_query self.in(@raw_params).paginate(:page => current_page) else where(@params).paginate(:page => current_page) end end end end |
#where(params = {}, logical_and = true) ⇒ Object
Adds a where
criterion. Do not specify option here. Do it using skip, limit, order, etc Returns: self
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fm_store/criterion/inclusion.rb', line 8 def where(params = {}, logical_and = true) accepted_params = {} params.each do |field, value| field = field.to_s # just to normalize it with_operator = field.split(".") if with_operator.size == 2 fm_name = klass.find_fm_name(with_operator.first) if fm_name accepted_params[fm_name] = value accepted_params["#{fm_name}.op"] = with_operator.last end else fm_name = klass.find_fm_name(field) accepted_params[fm_name] = value if fm_name end end accepted_params["-lop"] = "or" unless logical_and update_params(accepted_params) self end |