From d1d6779ad7b1e244be20126fa050d18a2f924568 Mon Sep 17 00:00:00 2001 From: Gabriel Bellon Date: Wed, 25 Oct 2017 16:34:30 -0200 Subject: [PATCH 1/2] Change depency strategy for Mongoid --- .../mongoid/impressionist/impressionable.rb | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/impressionist/models/mongoid/impressionist/impressionable.rb b/lib/impressionist/models/mongoid/impressionist/impressionable.rb index 4311d3b0..abe8631c 100644 --- a/lib/impressionist/models/mongoid/impressionist/impressionable.rb +++ b/lib/impressionist/models/mongoid/impressionist/impressionable.rb @@ -1,26 +1,35 @@ module Impressionist module Impressionable + extend ActiveSupport::Concern - # extends AS::Concern - include Impressionist::IsImpressionable + module InstanceMethods + # Overides impressionist_count in order to provide mongoid compability + def impressionist_count(options={}) - # Overides impressionist_count in order to provide mongoid compability - def impressionist_count(options={}) + # Uses these options as defaults unless overridden in options hash + options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now) - # Uses these options as defaults unless overridden in options hash - options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now) + # If a start_date is provided, finds impressions between then and the end_date. + # Otherwise returns all impressions + imps = options[:start_date].blank? ? impressions : + impressions.between(created_at: options[:start_date]..options[:end_date]) - # If a start_date is provided, finds impressions between then and the end_date. - # Otherwise returns all impressions - imps = options[:start_date].blank? ? impressions : - impressions.between(created_at: options[:start_date]..options[:end_date]) - - # Count all distinct impressions unless the :all filter is provided - distinct = options[:filter] != :all - distinct ? imps.where(options[:filter].ne => nil).distinct(options[:filter]).count : imps.count + # Count all distinct impressions unless the :all filter is provided + distinct = options[:filter] != :all + distinct ? imps.where(options[:filter].ne => nil).distinct(options[:filter]).count : imps.count + end end + module ClassMethods + def is_impressionable(options={}) + has_many :impressions, + as: :impressionable, + dependent: :delete + + @impressionist_cache_options = options + end + end end end From 5869bf162996f36eca5f6481a5a94eb8ca6b7a9b Mon Sep 17 00:00:00 2001 From: Gabriel Bellon Date: Wed, 25 Oct 2017 17:20:06 -0200 Subject: [PATCH 2/2] Refactor impression model for Mongoid --- .../models/mongoid/impression.rb | 2 +- .../mongoid/impressionist/impressionable.rb | 28 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/impressionist/models/mongoid/impression.rb b/lib/impressionist/models/mongoid/impression.rb index 5e10e337..73de29f9 100644 --- a/lib/impressionist/models/mongoid/impression.rb +++ b/lib/impressionist/models/mongoid/impression.rb @@ -6,7 +6,6 @@ class Impression include Mongoid::Timestamps include Impressionist::CounterCache - Impressionist::SetupAssociation.new(self).set field :impressionable_id, type: BSON::ObjectId field :impressionable_type @@ -23,4 +22,5 @@ class Impression after_save :impressionable_counter_cache_updatable? + belongs_to :impressionable, polymorphic: true end diff --git a/lib/impressionist/models/mongoid/impressionist/impressionable.rb b/lib/impressionist/models/mongoid/impressionist/impressionable.rb index abe8631c..77ab63cf 100644 --- a/lib/impressionist/models/mongoid/impressionist/impressionable.rb +++ b/lib/impressionist/models/mongoid/impressionist/impressionable.rb @@ -2,24 +2,22 @@ module Impressionist module Impressionable extend ActiveSupport::Concern - module InstanceMethods - # Overides impressionist_count in order to provide mongoid compability - def impressionist_count(options={}) + # Overides impressionist_count in order to provide mongoid compability + def impressionist_count(options={}) - # Uses these options as defaults unless overridden in options hash - options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now) + # Uses these options as defaults unless overridden in options hash + options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now) - # If a start_date is provided, finds impressions between then and the end_date. - # Otherwise returns all impressions - imps = options[:start_date].blank? ? impressions : - impressions.between(created_at: options[:start_date]..options[:end_date]) + # If a start_date is provided, finds impressions between then and the end_date. + # Otherwise returns all impressions + imps = options[:start_date].blank? ? impressions : + impressions.between(created_at: options[:start_date]..options[:end_date]) - # Count all distinct impressions unless the :all filter is provided - distinct = options[:filter] != :all - distinct ? imps.where(options[:filter].ne => nil).distinct(options[:filter]).count : imps.count - end - end + # Count all distinct impressions unless the :all filter is provided + distinct = options[:filter] != :all + distinct ? imps.where(options[:filter].ne => nil).distinct(options[:filter]).count : imps.count + end module ClassMethods def is_impressionable(options={}) @@ -28,6 +26,8 @@ def is_impressionable(options={}) dependent: :delete @impressionist_cache_options = options + + true end end end