Renamed ActiveRecordLegacyStore to ActiveRecordTokenStore
Andrew Kane
committed Jun 18, 2014
commit f6d4710e2c181e4e7a6f3deeb7d6fecb26fa50de
Showing 7
changed files with
119 additions
and 166 deletions
README.md
+3
-3
| @@ | @@ -440,7 +440,7 @@ Use an array to pass multiple events at once. |
| Add the following code to the end of `config/intializers/ahoy.rb`. | |
| ```ruby | |
| - | class Ahoy::Store < Ahoy::Stores::ActiveRecordLegacyStore |
| + | class Ahoy::Store < Ahoy::Stores::ActiveRecordTokenStore |
| uses_deprecated_subscribers | |
| end | |
| ``` | |
| @@ | @@ -479,7 +479,7 @@ Remove `uses_deprecated_subscribers` from `Ahoy::Store`. |
| If you have a custom subscriber, copy the `track` method to `track_event` in `Ahoy::Store`. | |
| ```ruby | |
| - | class Ahoy::Store < Ahoy::Stores::ActiveRecordLegacyStore |
| + | class Ahoy::Store < Ahoy::Stores::ActiveRecordTokenStore |
| def track_event(name, properties, options) | |
| # code copied from the track method in your subscriber | |
| @@ | @@ -495,7 +495,7 @@ Replace the `Ahoy.user_method` with `user` method, and replace `Ahoy.track_bots` |
| Skip this step if you do not use these options. | |
| ```ruby | |
| - | class Ahoy::Store < Ahoy::Stores::ActiveRecordLegacyStore |
| + | class Ahoy::Store < Ahoy::Stores::ActiveRecordTokenStore |
| def user | |
| # logic from Ahoy.user_method goes here | |
ahoy.rb b/lib/ahoy.rb
+1
-1
| @@ | @@ -17,8 +17,8 @@ require "ahoy/deckhands/technology_deckhand" |
| require "ahoy/deckhands/traffic_source_deckhand" | |
| require "ahoy/deckhands/utm_parameter_deckhand" | |
| require "ahoy/stores/base_store" | |
| - | require "ahoy/stores/active_record_legacy_store" |
| require "ahoy/stores/active_record_store" | |
| + | require "ahoy/stores/active_record_token_store" |
| require "ahoy/stores/log_store" | |
| require "ahoy/stores/mongoid_store" | |
| require "ahoy/engine" | |
ahoy/stores/active_record_legacy_store.rb b/lib/ahoy/stores/active_record_legacy_store.rb
+0
-113
| @@ | @@ -1,113 +0,0 @@ |
| - | module Ahoy |
| - | module Stores |
| - | class ActiveRecordLegacyStore < BaseStore |
| - | |
| - | def track_visit(options, &block) |
| - | visit = |
| - | visit_model.new do |v| |
| - | v.visit_token = ahoy.visit_token |
| - | v.visitor_token = ahoy.visitor_token |
| - | v.user = user if v.respond_to?(:user=) |
| - | v.created_at = options[:started_at] |
| - | end |
| - | |
| - | visit_properties.keys.each do |key| |
| - | visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") |
| - | end |
| - | |
| - | yield(visit) if block_given? |
| - | |
| - | begin |
| - | visit.save! |
| - | rescue ActiveRecord::RecordNotUnique |
| - | # do nothing |
| - | end |
| - | end |
| - | |
| - | def track_event(name, properties, options, &block) |
| - | if self.class.uses_deprecated_subscribers? |
| - | options[:controller] ||= controller |
| - | options[:user] ||= user |
| - | options[:visit] ||= visit |
| - | options[:visit_token] ||= ahoy.visit_token |
| - | options[:visitor_token] ||= ahoy.visitor_token |
| - | |
| - | subscribers = Ahoy.subscribers |
| - | if subscribers.any? |
| - | subscribers.each do |subscriber| |
| - | subscriber.track(name, properties, options.dup) |
| - | end |
| - | else |
| - | $stderr.puts "No subscribers" |
| - | end |
| - | else |
| - | event = |
| - | event_model.new do |e| |
| - | e.visit_id = visit.try(:id) |
| - | e.user = user |
| - | e.name = name |
| - | e.properties = properties |
| - | e.time = options[:time] |
| - | end |
| - | |
| - | yield(event) if block_given? |
| - | |
| - | event.save! |
| - | end |
| - | end |
| - | |
| - | def visit |
| - | @visit ||= visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token |
| - | end |
| - | |
| - | def exclude? |
| - | (!Ahoy.track_bots && bot?) || |
| - | ( |
| - | if Ahoy.exclude_method |
| - | warn "[DEPRECATION] Ahoy.exclude_method is deprecated - use exclude? instead" |
| - | if Ahoy.exclude_method.arity == 1 |
| - | Ahoy.exclude_method.call(controller) |
| - | else |
| - | Ahoy.exclude_method.call(controller, request) |
| - | end |
| - | else |
| - | false |
| - | end |
| - | ) |
| - | end |
| - | |
| - | def user |
| - | user_method = Ahoy.user_method |
| - | if user_method.respond_to?(:call) |
| - | user_method.call(controller) |
| - | else |
| - | controller.send(user_method) |
| - | end |
| - | end |
| - | |
| - | class << self |
| - | |
| - | def uses_deprecated_subscribers |
| - | warn "[DEPRECATION] Ahoy subscribers are deprecated" |
| - | @uses_deprecated_subscribers = true |
| - | end |
| - | |
| - | def uses_deprecated_subscribers? |
| - | @uses_deprecated_subscribers || false |
| - | end |
| - | |
| - | end |
| - | |
| - | protected |
| - | |
| - | def visit_model |
| - | Ahoy.visit_model || ::Visit |
| - | end |
| - | |
| - | def event_model |
| - | ::Ahoy::Event |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
ahoy/stores/active_record_token_store.rb b/lib/ahoy/stores/active_record_token_store.rb
+113
-0
| @@ | @@ -0,0 +1,113 @@ |
| + | module Ahoy |
| + | module Stores |
| + | class ActiveRecordTokenStore < BaseStore |
| + | |
| + | def track_visit(options, &block) |
| + | visit = |
| + | visit_model.new do |v| |
| + | v.visit_token = ahoy.visit_token |
| + | v.visitor_token = ahoy.visitor_token |
| + | v.user = user if v.respond_to?(:user=) |
| + | v.created_at = options[:started_at] |
| + | end |
| + | |
| + | visit_properties.keys.each do |key| |
| + | visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") |
| + | end |
| + | |
| + | yield(visit) if block_given? |
| + | |
| + | begin |
| + | visit.save! |
| + | rescue ActiveRecord::RecordNotUnique |
| + | # do nothing |
| + | end |
| + | end |
| + | |
| + | def track_event(name, properties, options, &block) |
| + | if self.class.uses_deprecated_subscribers? |
| + | options[:controller] ||= controller |
| + | options[:user] ||= user |
| + | options[:visit] ||= visit |
| + | options[:visit_token] ||= ahoy.visit_token |
| + | options[:visitor_token] ||= ahoy.visitor_token |
| + | |
| + | subscribers = Ahoy.subscribers |
| + | if subscribers.any? |
| + | subscribers.each do |subscriber| |
| + | subscriber.track(name, properties, options.dup) |
| + | end |
| + | else |
| + | $stderr.puts "No subscribers" |
| + | end |
| + | else |
| + | event = |
| + | event_model.new do |e| |
| + | e.visit_id = visit.try(:id) |
| + | e.user = user |
| + | e.name = name |
| + | e.properties = properties |
| + | e.time = options[:time] |
| + | end |
| + | |
| + | yield(event) if block_given? |
| + | |
| + | event.save! |
| + | end |
| + | end |
| + | |
| + | def visit |
| + | @visit ||= visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token |
| + | end |
| + | |
| + | def exclude? |
| + | (!Ahoy.track_bots && bot?) || |
| + | ( |
| + | if Ahoy.exclude_method |
| + | warn "[DEPRECATION] Ahoy.exclude_method is deprecated - use exclude? instead" |
| + | if Ahoy.exclude_method.arity == 1 |
| + | Ahoy.exclude_method.call(controller) |
| + | else |
| + | Ahoy.exclude_method.call(controller, request) |
| + | end |
| + | else |
| + | false |
| + | end |
| + | ) |
| + | end |
| + | |
| + | def user |
| + | user_method = Ahoy.user_method |
| + | if user_method.respond_to?(:call) |
| + | user_method.call(controller) |
| + | else |
| + | controller.send(user_method) |
| + | end |
| + | end |
| + | |
| + | class << self |
| + | |
| + | def uses_deprecated_subscribers |
| + | warn "[DEPRECATION] Ahoy subscribers are deprecated" |
| + | @uses_deprecated_subscribers = true |
| + | end |
| + | |
| + | def uses_deprecated_subscribers? |
| + | @uses_deprecated_subscribers || false |
| + | end |
| + | |
| + | end |
| + | |
| + | protected |
| + | |
| + | def visit_model |
| + | Ahoy.visit_model || ::Visit |
| + | end |
| + | |
| + | def event_model |
| + | ::Ahoy::Event |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
ahoy/tracker.rb b/lib/ahoy/tracker.rb
+2
-2
| @@ | @@ -84,12 +84,12 @@ module Ahoy |
| @visit_properties ||= Ahoy::VisitProperties.new(request, @options.slice(:api)) | |
| end | |
| - | # for ActiveRecordLegacyStore only - do not use |
| + | # for ActiveRecordTokenStore only - do not use |
| def visit_token | |
| @visit_token ||= existing_visit_id || generate_id | |
| end | |
| - | # for ActiveRecordLegacyStore only - do not use |
| + | # for ActiveRecordTokenStore only - do not use |
| def visitor_token | |
| @visitor_token ||= existing_visitor_id || generate_id | |
| end | |
generators/ahoy/stores/active_record_legacy_generator.rb b/lib/generators/ahoy/stores/active_record_legacy_generator.rb
+0
-16
| @@ | @@ -1,16 +0,0 @@ |
| - | require "rails/generators" |
| - | |
| - | module Ahoy |
| - | module Stores |
| - | module Generators |
| - | class ActiveRecordLegacyGenerator < Rails::Generators::Base |
| - | source_root File.expand_path("../templates", __FILE__) |
| - | |
| - | def create_initializer |
| - | template "active_record_legacy_initializer.rb", "config/initializers/ahoy.rb" |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| - | end |
generators/ahoy/stores/templates/active_record_legacy_initializer.rb b/lib/generators/ahoy/stores/templates/active_record_legacy_initializer.rb
+0
-31
| @@ | @@ -1,31 +0,0 @@ |
| - | class Ahoy::Store < Ahoy::Stores::ActiveRecordLegacyStore |
| - | # code generated to assist with the migration |
| - | # if you do not use a method, delete it |
| - | |
| - | # Ahoy.user_method replacement |
| - | # def user |
| - | # controller.true_user |
| - | # end |
| - | |
| - | # Ahoy.track_bots and Ahoy.exclude_method replacement |
| - | # def exclude? |
| - | # bot? || request.ip == "1.1.1.1" |
| - | # end |
| - | |
| - | # Ahoy.visit_model replacement |
| - | # def visit_model |
| - | # CustomVisit |
| - | # end |
| - | |
| - | # Custom subscriber replacement |
| - | # Not needed if ActiveRecord subscriber used |
| - | # def track_event(name, properties, options) |
| - | # # track method goes here |
| - | # end |
| - | |
| - | # Ahoy::Subscribers::ActiveRecord.new(model: CustomEvent) replacement |
| - | # def event_model |
| - | # CustomEvent |
| - | # end |
| - | |
| - | end |