simply reopen ApplicationController instead of using include/extend. As long as the file is not named "application_controller.rb", then it will load. Rails does not load files from engine /app that have the same name as files in container application /app dir

This commit is contained in:
cowboycoded 2011-02-04 10:13:27 -05:00
parent 4ec37f86e0
commit ec1b40487d
3 changed files with 55 additions and 69 deletions

View File

@ -0,0 +1,54 @@
require 'digest/sha2'
class ApplicationController < ActionController::Base
before_filter :impressionist_app_filter
def self.impressionist(opts={})
before_filter { |c| c.impressionist_subapp_filter opts[:actions] }
end
def impressionist(obj,message=nil)
unless bypass
if obj.respond_to?("impressionable?")
obj.impressions.create(message: message,
request_hash: @impressionist_hash,
ip_address: request.remote_ip,
user_id: user_id)
else
raise "#{obj.class.to_s} is not impressionable!"
end
end
end
def impressionist_app_filter
@impressionist_hash = Digest::SHA2.hexdigest(Time.now.to_f.to_s+rand(10000).to_s)
end
def impressionist_subapp_filter(actions=nil)
unless bypass
actions.collect!{|a|a.to_s} unless actions.blank?
if actions.blank? or actions.include?(action_name)
Impression.create(controller_name: controller_name,
action_name: action_name,
user_id: user_id,
request_hash: @request_hash,
request_hash: @impressionist_hash,
ip_address: request.remote_ip,
impressionable_type: controller_name.singularize.camelize,
impressionable_id: params[:id])
end
end
end
private
def bypass
Impressionist::Bots::WILD_CARDS.each do |wild_card|
return true if request.user_agent.include? wild_card
end
Impressionist::Bots::LIST.include? request.user_agent
end
def user_id
@current_user ? @current_user.id : nil
end
end

View File

@ -1,60 +0,0 @@
require 'digest/sha2'
module ImpressionistController
module ClassMethods
def impressionist(opts={})
before_filter { |c| c.impressionist_subapp_filter opts[:actions] }
end
end
module InstanceMethods
def self.included(base)
base.before_filter :impressionist_app_filter
end
def impressionist(obj,message=nil)
unless bypass
if obj.respond_to?("impressionable?")
obj.impressions.create(message: message,
request_hash: @impressionist_hash,
ip_address: request.remote_ip,
user_id: user_id)
else
raise "#{obj.class.to_s} is not impressionable!"
end
end
end
def impressionist_app_filter
@impressionist_hash = Digest::SHA2.hexdigest(Time.now.to_f.to_s+rand(10000).to_s)
end
def impressionist_subapp_filter(actions=nil)
unless bypass
actions.collect!{|a|a.to_s} unless actions.blank?
if actions.blank? or actions.include?(action_name)
Impression.create(controller_name: controller_name,
action_name: action_name,
user_id: user_id,
request_hash: @request_hash,
request_hash: @impressionist_hash,
ip_address: request.remote_ip,
impressionable_type: controller_name.singularize.camelize,
impressionable_id: params[:id])
end
end
end
private
def bypass
Impressionist::Bots::WILD_CARDS.each do |wild_card|
return true if request.user_agent.include? wild_card
end
Impressionist::Bots::LIST.include? request.user_agent
end
def user_id
@current_user ? @current_user.id : nil
end
end
end

View File

@ -2,15 +2,7 @@ require "impressionist"
require "rails"
module Impressionist
class Engine < Rails::Engine
initializer 'impressionist.controller' do
ActiveSupport.on_load(:action_controller) do
include ImpressionistController::InstanceMethods
extend ImpressionistController::ClassMethods
end
end
class Engine < Rails::Engine
initializer 'impressionist.extend_ar' do
ActiveRecord::Base.extend Impressionist::Impressionable
end