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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ahoy/query_methods.rb', line 72 def group_prop(*props) # like with group props.flatten! relation = self if respond_to?(:columns_hash) column_type = columns_hash["properties"].type adapter_name = connection.adapter_name.downcase else adapter_name = "mongoid" end case adapter_name when "mongoid" raise "Adapter not supported: #{adapter_name}" when /mysql/ if connection.try(:mariadb?) props.each do |prop| quoted_prop = connection.quote("$.#{prop}") relation = relation.group("JSON_UNQUOTE(JSON_EXTRACT(properties, #{quoted_prop}))") end else column = column_type == :json ? "properties" : "CAST(properties AS JSON)" props.each do |prop| quoted_prop = connection.quote("$.#{prop}") relation = relation.group("JSON_UNQUOTE(JSON_EXTRACT(#{column}, #{quoted_prop}))") end end when /postgres|postgis/ # convert to jsonb to fix # could not identify an equality operator for type json # and for text columns cast = [:jsonb, :hstore].include?(column_type) ? "" : "::jsonb" props.each do |prop| quoted_prop = connection.quote(prop) relation = relation.group("properties#{cast} -> #{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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ahoy/query_methods.rb', line 10 def where_props(properties) relation = self if respond_to?(:columns_hash) column_type = columns_hash["properties"].type adapter_name = connection.adapter_name.downcase else adapter_name = "mongoid" end case adapter_name when "mongoid" relation = where(Hash[properties.map { |k, v| ["properties.#{k}", v] }]) when /mysql/ if column_type == :json properties.each do |k, v| if v.nil? v = "null" elsif v == true v = "true" end relation = relation.where("JSON_UNQUOTE(properties -> ?) = ?", "$.#{k}", v.as_json) end else properties.each do |k, v| # TODO cast to json instead relation = relation.where("properties REGEXP ?", "[{,]#{{k.to_s => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "").gsub("+", "\\\\+")}[,}]") end end when /postgres|postgis/ if column_type == :jsonb relation = relation.where("properties @> ?", properties.to_json) elsif column_type == :json properties.each do |k, v| relation = if v.nil? relation.where("properties ->> ? IS NULL", k.to_s) else relation.where("properties ->> ? = ?", k.to_s, v.as_json.to_s) end end elsif column_type == :hstore properties.each do |k, v| relation = if v.nil? relation.where("properties -> ? IS NULL", k.to_s) else relation.where("properties -> ? = ?", k.to_s, v.to_s) end end else properties.each do |k, v| # TODO cast to jsonb instead relation = relation.where("properties SIMILAR TO ?", "%[{,]#{{k.to_s => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "").gsub("+", "\\\\+")}[,}]%") end end else raise "Adapter not supported: #{adapter_name}" end relation end |