From 39c217a976de8184405b4bfe2bd9b03f27b08b58 Mon Sep 17 00:00:00 2001 From: Ty Rauber Date: Sat, 30 Mar 2013 13:00:46 -0700 Subject: [PATCH 1/2] Non-restful controllers - controllers without models / resources - would cause impressionist to throw an error on impression create, because it was unable to update the model counter cache due to lack of an impressionable_type and impressionable_id. An if statement around the counter-cache method on impressionable_type and impressionable_id solves this. --- lib/impressionist/models/active_record/impression.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/impressionist/models/active_record/impression.rb b/lib/impressionist/models/active_record/impression.rb index 710c446..f211b98 100644 --- a/lib/impressionist/models/active_record/impression.rb +++ b/lib/impressionist/models/active_record/impression.rb @@ -10,11 +10,12 @@ class Impression < ActiveRecord::Base private def update_impressions_counter_cache - impressionable_class = self.impressionable_type.constantize - - if impressionable_class.impressionist_counter_cache_options - resouce = impressionable_class.find(self.impressionable_id) - resouce.try(:update_impressionist_counter_cache) + if self.impressionable_type && self.impressionable_id + impressionable_class = self.impressionable_type.constantize + if impressionable_class.impressionist_counter_cache_options + resouce = impressionable_class.find(self.impressionable_id) + resouce.try(:update_impressionist_counter_cache) + end end end end From 495e0a42731beef4cdcc6c7606fc30cee677c4e5 Mon Sep 17 00:00:00 2001 From: Ty Rauber Date: Sat, 30 Mar 2013 13:30:56 -0700 Subject: [PATCH 2/2] Test coverage around impressions for non-restful controller actions. Added dummy route, index method, view and dummy controller spec. --- test_app/app/controllers/dummy_controller.rb | 2 ++ test_app/app/views/dummy/index.html.erb | 0 test_app/config/routes.rb | 2 +- test_app/spec/controllers/controller_spec.rb | 9 +++++++++ 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test_app/app/views/dummy/index.html.erb diff --git a/test_app/app/controllers/dummy_controller.rb b/test_app/app/controllers/dummy_controller.rb index 86664a6..c90cd9c 100644 --- a/test_app/app/controllers/dummy_controller.rb +++ b/test_app/app/controllers/dummy_controller.rb @@ -3,4 +3,6 @@ class DummyController < ActionController::Base impressionist + def index + end end diff --git a/test_app/app/views/dummy/index.html.erb b/test_app/app/views/dummy/index.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/test_app/config/routes.rb b/test_app/config/routes.rb index 7bcb78e..76cb5b5 100644 --- a/test_app/config/routes.rb +++ b/test_app/config/routes.rb @@ -1,3 +1,3 @@ TestApp::Application.routes.draw do - resources :articles, :posts, :widgets + resources :articles, :posts, :widgets, :dummy end diff --git a/test_app/spec/controllers/controller_spec.rb b/test_app/spec/controllers/controller_spec.rb index a48b3a0..7f1307a 100644 --- a/test_app/spec/controllers/controller_spec.rb +++ b/test_app/spec/controllers/controller_spec.rb @@ -123,3 +123,12 @@ describe WidgetsController do end end +describe DummyController do + fixtures :impressions + render_views + + it "should log impression at the per action level on non-restful controller" do + get "index" + Impression.all.size.should eq 12 + end +end \ No newline at end of file