Added where_properties method

Andrew Kane committed Jun 21, 2016
commit 80ba93ce8cdf42355692ebd2bc135634712c5271
Showing 5 changed files with 35 additions and 8 deletions
CHANGELOG.md +6 -0
@@ @@ -1,3 +1,9 @@
+ ## 1.4.1 [unreleased]
+
+ - Added `where_properties` method
+ - Added Kafka store
+ - Added `mount` option
+
## 1.4.0
- Use `ActiveRecordTokenStore` by default (integer instead of uuid for id)
README.md +2 -8
@@ @@ -489,17 +489,11 @@ The same approach also works with visitor tokens.
### Querying Properties
- Postgres `json` or `jsonb`
-
```ruby
- Ahoy::Event.where("properties ->> 'store_id' = 1").count
+ Ahoy::Event.where_properties(store_id: 1).count
```
- Text
-
- ```ruby
- Ahoy::Event.where("properties LIKE '%\"store_id\": \"1\"%'").count
- ```
+ **Note:** If you get a `NoMethodError`, upgrade Ahoy and add to your model `include Ahoy::Properties`.
## Native Apps
ahoy.rb b/lib/ahoy.rb +1 -0
@@ @@ -13,6 +13,7 @@ require "ahoy/tracker"
require "ahoy/controller"
require "ahoy/model"
require "ahoy/visit_properties"
+ require "ahoy/properties"
require "ahoy/deckhands/location_deckhand"
require "ahoy/deckhands/request_deckhand"
require "ahoy/deckhands/technology_deckhand"
ahoy/properties.rb b/lib/ahoy/properties.rb +24 -0
@@ @@ -0,0 +1,24 @@
+ module Ahoy
+ module Properties
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def where_properties(properties)
+ relation = self
+ column_type = columns_hash["properties"].type
+ case column_type
+ when :jsonb, :json
+ properties.each do |k, v|
+ relation = relation.where("properties ->> ? = ?", k, v)
+ end
+ else
+ properties.each do |k, v|
+ # not 100%, but will do
+ relation = relation.where("properties LIKE ?", "%#{{k => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "")}%")
+ end
+ end
+ relation
+ end
+ end
+ end
+ end
generators/ahoy/stores/templates/active_record_event_model.rb b/lib/generators/ahoy/stores/templates/active_record_event_model.rb +2 -0
@@ @@ -1,5 +1,7 @@
module Ahoy
class Event < ActiveRecord::Base
+ include Ahoy::Properties
+
self.table_name = "ahoy_events"
belongs_to :visit