use before_action for rails 5, maintain backwards compat.

This commit is contained in:
M. Simon Borg 2017-05-10 15:20:11 -04:00
parent 6f22014650
commit 5ed366de82
8 changed files with 28 additions and 14 deletions

View File

@ -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.

View File

@ -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={})

View File

@ -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

View File

@ -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]

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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