Merge pull request #241 from msimonborg/patch-1

before_filter -> before_action
This commit is contained in:
Jonas Grau 2017-05-10 22:20:28 +02:00 committed by GitHub
commit 529f9d494d
6 changed files with 26 additions and 12 deletions

View File

@ -139,7 +139,7 @@ Usage
@widget.impressionist_count(:message=>"pageview", :filter=>:all) @widget.impressionist_count(:message=>"pageview", :filter=>:all)
Logging impressions for authenticated users happens automatically. If you have 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 authenticated user, current_user.id will be written to the user_id field in the
impressions table. impressions table.

View File

@ -3,13 +3,21 @@ require 'digest/sha2'
module ImpressionistController module ImpressionistController
module ClassMethods module ClassMethods
def impressionist(opts={}) 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
end end
module InstanceMethods module InstanceMethods
def self.included(base) 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 end
def impressionist(obj,message=nil,opts={}) def impressionist(obj,message=nil,opts={})
@ -103,7 +111,7 @@ module ImpressionistController
request_param = params_hash request_param = params_hash
impressions.detect{|impression| impression.params == request_param }.nil? impressions.detect{|impression| impression.params == request_param }.nil?
end end
# creates the query to check for uniqueness # creates the query to check for uniqueness
def unique_query(unique_opts,impressionable=nil) def unique_query(unique_opts,impressionable=nil)
full_statement = direct_create_statement({},impressionable) full_statement = direct_create_statement({},impressionable)

View File

@ -1,8 +1,12 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery 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 def secondary_before_action
@test_secondary_before_filter = "this is a test" @test_secondary_before_action = "this is a test"
end end
end end

View File

@ -1,5 +1,9 @@
class ArticlesController < ApplicationController 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 def test_current_user_var
if session[:user_id] if session[:user_id]

View File

@ -26,7 +26,7 @@ describe ArticlesController do
Article.first.impressions.last.action_name.should eq "show" Article.first.impressions.last.action_name.should eq "show"
end 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 session[:user_id] = 123
get "show", :id=> 1 get "show", :id=> 1
Article.first.impressions.last.user_id.should eq 123 Article.first.impressions.last.user_id.should eq 123
@ -72,5 +72,3 @@ describe ArticlesController do
Impression.last.referrer.should eq nil Impression.last.referrer.should eq nil
end end
end end

View File

@ -13,7 +13,7 @@ describe Impressionist do
expect(ApplicationController).to respond_to(method) expect(ApplicationController).to respond_to(method)
end 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 = ApplicationController._process_action_callbacks.select { |c| c.kind == :before }
filters.collect{|filter|filter.filter}.include?(:impressionist_app_filter).should be_true filters.collect{|filter|filter.filter}.include?(:impressionist_app_filter).should be_true
end end