Module: Ahoy::QueryMethods::ClassMethods
- Defined in:
- lib/ahoy/query_methods.rb
Instance Method Summary collapse
- #group_prop(*props) ⇒ Object
- #where_event(name, properties = {}) ⇒ Object
- #where_props(properties) ⇒ Object (also: #where_properties)
Instance Method Details
#group_prop(*props) ⇒ Object
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 78 79 80 81 82 |
# File 'lib/ahoy/query_methods.rb', line 48 def group_prop(*props) # like with group props.flatten! relation = all adapter_name = respond_to?(:connection_db_config) ? connection_db_config.adapter.to_s : "mongoid" case adapter_name when "mongoid" raise "Adapter not supported: #{adapter_name}" when /mysql|trilogy/i props.each do |prop| quoted_prop = connection_pool.with_connection { |c| c.quote("$.#{prop}") } relation = relation.group("JSON_UNQUOTE(JSON_EXTRACT(properties, #{quoted_prop}))") end when /postg/i # convert to jsonb to fix # could not identify an equality operator for type json # and for text columns column_type = columns_hash["properties"].type cast = [:jsonb, :hstore].include?(column_type) ? "" : "::jsonb" props.each do |prop| quoted_prop = connection_pool.with_connection { |c| c.quote(prop) } relation = relation.group("properties#{cast} -> #{quoted_prop}") end when /sqlite/i props.each do |prop| quoted_prop = connection_pool.with_connection { |c| c.quote("$.#{prop}") } relation = relation.group("JSON_EXTRACT(properties, #{quoted_prop})") end else raise "Adapter not supported: #{adapter_name}" end relation end |
#where_event(name, properties = {}) ⇒ Object
6 7 8 |
# File 'lib/ahoy/query_methods.rb', line 6 def where_event(name, properties = {}) where(name: name).where_props(properties) end |
#where_props(properties) ⇒ Object Also known as: where_properties
10 11 12 13 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 39 40 41 42 43 44 45 |
# File 'lib/ahoy/query_methods.rb', line 10 def where_props(properties) return all if properties.empty? adapter_name = respond_to?(:connection_db_config) ? connection_db_config.adapter.to_s : "mongoid" case adapter_name when "mongoid" where(properties.to_h { |k, v| ["properties.#{k}", v] }) when /mysql|trilogy/i where("JSON_CONTAINS(properties, ?, '$') = 1", properties.to_json) when /postg/i case columns_hash["properties"].type when :hstore properties.inject(all) do |relation, (k, v)| if v.nil? relation.where("properties -> ? IS NULL", k.to_s) else relation.where("properties -> ? = ?", k.to_s, v.to_s) end end when :jsonb where("properties @> ?", properties.to_json) else where("properties::jsonb @> ?", properties.to_json) end when /sqlite/i properties.inject(all) do |relation, (k, v)| if v.nil? relation.where("JSON_EXTRACT(properties, ?) IS NULL", "$.#{k}") else relation.where("JSON_EXTRACT(properties, ?) = ?", "$.#{k}", v.as_json) end end else raise "Adapter not supported: #{adapter_name}" end end |