From 6f22014650d0c58421767c67d44bd8da4589c020 Mon Sep 17 00:00:00 2001 From: "M. Simon Borg" Date: Wed, 10 May 2017 08:49:15 -0400 Subject: [PATCH 1/5] splat args in SetupAssociation test --- tests/spec/setup_association_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spec/setup_association_spec.rb b/tests/spec/setup_association_spec.rb index 644ddc3..93870da 100644 --- a/tests/spec/setup_association_spec.rb +++ b/tests/spec/setup_association_spec.rb @@ -11,7 +11,7 @@ module Impressionist # expects attr_accessible to return true # and pass 12 arguments mock. - expect(:attr_accessible, true) do |args| + expect(:attr_accessible, true) do |*args| args.size == 12 end From 51c054e20031ba77ae285c38020fbddb15d19f55 Mon Sep 17 00:00:00 2001 From: "m. simon borg" Date: Wed, 10 May 2017 09:52:26 -0400 Subject: [PATCH 2/5] before_filter -> before_action `before_filter` is deprecated in favor of `before_action` and removed in Rails 5.1 --- app/controllers/impressionist_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index b92fcbf..4e21621 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -3,13 +3,13 @@ require 'digest/sha2' module ImpressionistController module ClassMethods def impressionist(opts={}) - before_filter { |c| c.impressionist_subapp_filter(opts) } + before_action { |c| c.impressionist_subapp_filter(opts) } end end module InstanceMethods def self.included(base) - base.before_filter :impressionist_app_filter + base.before_action :impressionist_app_filter end def impressionist(obj,message=nil,opts={}) From 6cd542df7a006dfbd232db14e2d96daf29d10d0f Mon Sep 17 00:00:00 2001 From: "m. simon borg" Date: Wed, 10 May 2017 14:59:11 -0400 Subject: [PATCH 3/5] use before_action only for Rails >= 5 conditional branch on `Rails::VERSION::MAJOR` for backwards compatibility --- app/controllers/impressionist_controller.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index 4e21621..2fee16a 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -3,13 +3,21 @@ require 'digest/sha2' module ImpressionistController module ClassMethods def impressionist(opts={}) - before_action { |c| c.impressionist_subapp_filter(opts) } + if Rails::VERSION::MAJOR >= 5 + before_action { |c| c.impressionist_subapp_filter(opts) } + else + before_filter { |c| c.impressionist_subapp_filter(opts) } + end end end module InstanceMethods def self.included(base) - base.before_action :impressionist_app_filter + if Rails::VERSION::MAJOR >= 5 + base.before_action :impressionist_app_filter + else + base.before_filter :impressionist_app_filter + end end def impressionist(obj,message=nil,opts={}) From 5ed366de820f6e1cfa9179d90b117f40ad8f60b6 Mon Sep 17 00:00:00 2001 From: "M. Simon Borg" Date: Wed, 10 May 2017 15:20:11 -0400 Subject: [PATCH 4/5] use before_action for rails 5, maintain backwards compat. --- README.md | 2 +- app/controllers/impressionist_controller.rb | 14 +++++++++++--- .../app/controllers/application_controller.rb | 10 +++++++--- .../app/controllers/articles_controller.rb | 6 +++++- tests/test_app/config/environments/development.rb | 2 +- tests/test_app/config/environments/test.rb | 2 +- .../spec/controllers/articles_controller_spec.rb | 4 +--- .../spec/initializers/initializers_spec.rb | 2 +- 8 files changed, 28 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 198dac2..21ffd0b 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ Usage @widget.impressionist_count(:message=>"pageview", :filter=>:all) Logging impressions for authenticated users happens automatically. If you have -a current_user helper or use @current_user in your before_filter to set your +a current_user helper or use @current_user in your before_filter (or before_action in Rails >= 5.0) to set your authenticated user, current_user.id will be written to the user_id field in the impressions table. diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index b92fcbf..f23a9fc 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -3,13 +3,21 @@ require 'digest/sha2' module ImpressionistController module ClassMethods def impressionist(opts={}) - before_filter { |c| c.impressionist_subapp_filter(opts) } + if Rails::VERSION::MAJOR >= 5 + before_action { |c| c.impressionist_subapp_filter(opts) } + else + before_filter { |c| c.impressionist_subapp_filter(opts) } + end end end module InstanceMethods def self.included(base) - base.before_filter :impressionist_app_filter + if Rails::VERSION::MAJOR >= 5 + base.before_action :impressionist_app_filter + else + base.before_filter :impressionist_app_filter + end end def impressionist(obj,message=nil,opts={}) @@ -103,7 +111,7 @@ module ImpressionistController request_param = params_hash impressions.detect{|impression| impression.params == request_param }.nil? end - + # creates the query to check for uniqueness def unique_query(unique_opts,impressionable=nil) full_statement = direct_create_statement({},impressionable) diff --git a/tests/test_app/app/controllers/application_controller.rb b/tests/test_app/app/controllers/application_controller.rb index 1ce61fa..077d62d 100644 --- a/tests/test_app/app/controllers/application_controller.rb +++ b/tests/test_app/app/controllers/application_controller.rb @@ -1,8 +1,12 @@ class ApplicationController < ActionController::Base protect_from_forgery - before_filter :secondary_before_filter + if Rails::VERSION::MAJOR >= 5 + before_action :secondary_before_action + else + before_filter :secondary_before_action + end - def secondary_before_filter - @test_secondary_before_filter = "this is a test" + def secondary_before_action + @test_secondary_before_action = "this is a test" end end diff --git a/tests/test_app/app/controllers/articles_controller.rb b/tests/test_app/app/controllers/articles_controller.rb index 367b460..559c9e0 100644 --- a/tests/test_app/app/controllers/articles_controller.rb +++ b/tests/test_app/app/controllers/articles_controller.rb @@ -1,5 +1,9 @@ class ArticlesController < ApplicationController - before_filter :test_current_user_var + if Rails::VERSION::MAJOR >= 5 + before_action :test_current_user_var + else + before_filter :test_current_user_var + end def test_current_user_var if session[:user_id] diff --git a/tests/test_app/config/environments/development.rb b/tests/test_app/config/environments/development.rb index b5306b3..c69665e 100644 --- a/tests/test_app/config/environments/development.rb +++ b/tests/test_app/config/environments/development.rb @@ -23,7 +23,7 @@ TestApp::Application.configure do config.action_dispatch.best_standards_support = :builtin # Raise exception on mass assignment protection for Active Record models - config.active_record.mass_assignment_sanitizer = :strict + # config.active_record.mass_assignment_sanitizer = :strict # Log the query plan for queries taking more than this (works # with SQLite, MySQL, and PostgreSQL) diff --git a/tests/test_app/config/environments/test.rb b/tests/test_app/config/environments/test.rb index 21ec8e4..6eb922c 100644 --- a/tests/test_app/config/environments/test.rb +++ b/tests/test_app/config/environments/test.rb @@ -30,7 +30,7 @@ TestApp::Application.configure do config.action_mailer.delivery_method = :test # Raise exception on mass assignment protection for Active Record models - config.active_record.mass_assignment_sanitizer = :strict + # config.active_record.mass_assignment_sanitizer = :strict # Print deprecation notices to the stderr config.active_support.deprecation = :stderr diff --git a/tests/test_app/spec/controllers/articles_controller_spec.rb b/tests/test_app/spec/controllers/articles_controller_spec.rb index 15454cd..ab35124 100644 --- a/tests/test_app/spec/controllers/articles_controller_spec.rb +++ b/tests/test_app/spec/controllers/articles_controller_spec.rb @@ -26,7 +26,7 @@ describe ArticlesController do Article.first.impressions.last.action_name.should eq "show" end - it "should log the user_id if user is authenticated (@current_user before_filter method)" do + it "should log the user_id if user is authenticated (@current_user before_action method)" do session[:user_id] = 123 get "show", :id=> 1 Article.first.impressions.last.user_id.should eq 123 @@ -72,5 +72,3 @@ describe ArticlesController do Impression.last.referrer.should eq nil end end - - diff --git a/tests/test_app/spec/initializers/initializers_spec.rb b/tests/test_app/spec/initializers/initializers_spec.rb index 398f49b..44d06ca 100644 --- a/tests/test_app/spec/initializers/initializers_spec.rb +++ b/tests/test_app/spec/initializers/initializers_spec.rb @@ -13,7 +13,7 @@ describe Impressionist do expect(ApplicationController).to respond_to(method) end - it "should include the before_filter method in ApplicationController" do + it "should include the before_action method in ApplicationController" do filters = ApplicationController._process_action_callbacks.select { |c| c.kind == :before } filters.collect{|filter|filter.filter}.include?(:impressionist_app_filter).should be_true end From c9cb792592d9cd919e948af489eba43e4cbd9c97 Mon Sep 17 00:00:00 2001 From: "M. Simon Borg" Date: Wed, 10 May 2017 15:27:44 -0400 Subject: [PATCH 5/5] undo changes in test env that break travis builds --- tests/spec/setup_association_spec.rb | 2 +- tests/test_app/config/environments/development.rb | 2 +- tests/test_app/config/environments/test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/spec/setup_association_spec.rb b/tests/spec/setup_association_spec.rb index 93870da..644ddc3 100644 --- a/tests/spec/setup_association_spec.rb +++ b/tests/spec/setup_association_spec.rb @@ -11,7 +11,7 @@ module Impressionist # expects attr_accessible to return true # and pass 12 arguments mock. - expect(:attr_accessible, true) do |*args| + expect(:attr_accessible, true) do |args| args.size == 12 end diff --git a/tests/test_app/config/environments/development.rb b/tests/test_app/config/environments/development.rb index c69665e..b5306b3 100644 --- a/tests/test_app/config/environments/development.rb +++ b/tests/test_app/config/environments/development.rb @@ -23,7 +23,7 @@ TestApp::Application.configure do config.action_dispatch.best_standards_support = :builtin # Raise exception on mass assignment protection for Active Record models - # config.active_record.mass_assignment_sanitizer = :strict + config.active_record.mass_assignment_sanitizer = :strict # Log the query plan for queries taking more than this (works # with SQLite, MySQL, and PostgreSQL) diff --git a/tests/test_app/config/environments/test.rb b/tests/test_app/config/environments/test.rb index 6eb922c..21ec8e4 100644 --- a/tests/test_app/config/environments/test.rb +++ b/tests/test_app/config/environments/test.rb @@ -30,7 +30,7 @@ TestApp::Application.configure do config.action_mailer.delivery_method = :test # Raise exception on mass assignment protection for Active Record models - # config.active_record.mass_assignment_sanitizer = :strict + config.active_record.mass_assignment_sanitizer = :strict # Print deprecation notices to the stderr config.active_support.deprecation = :stderr