2011-02-13 19:15:31 +00:00
|
|
|
require 'digest/sha2'
|
|
|
|
|
|
|
|
module ImpressionistController
|
|
|
|
module ClassMethods
|
|
|
|
def impressionist(opts={})
|
2011-11-07 19:41:51 +00:00
|
|
|
before_filter { |c| c.impressionist_subapp_filter(opts[:actions], opts[:unique])}
|
2011-02-13 19:15:31 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-07 19:41:51 +00:00
|
|
|
|
2011-02-13 19:15:31 +00:00
|
|
|
module InstanceMethods
|
|
|
|
def self.included(base)
|
|
|
|
base.before_filter :impressionist_app_filter
|
|
|
|
end
|
|
|
|
|
2011-11-09 17:10:35 +00:00
|
|
|
def impressionist(obj,message=nil,opts={})
|
2011-02-13 19:15:31 +00:00
|
|
|
unless bypass
|
|
|
|
if obj.respond_to?("impressionable?")
|
2011-11-09 17:10:35 +00:00
|
|
|
if unique_instance?(obj, opts[:unique])
|
|
|
|
obj.impressions.create(associative_create_statement({:message => message}))
|
|
|
|
end
|
2011-02-13 19:15:31 +00:00
|
|
|
else
|
2011-11-09 17:10:35 +00:00
|
|
|
# we could create an impression anyway. for classes, too. why not?
|
2011-02-13 19:15:31 +00:00
|
|
|
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
|
|
|
|
|
2011-11-09 17:10:35 +00:00
|
|
|
def impressionist_subapp_filter(actions=nil,unique_opts=nil)
|
2011-02-13 19:15:31 +00:00
|
|
|
unless bypass
|
|
|
|
actions.collect!{|a|a.to_s} unless actions.blank?
|
2011-11-09 17:10:35 +00:00
|
|
|
if (actions.blank? || actions.include?(action_name)) && unique?(unique_opts)
|
|
|
|
Impression.create(direct_create_statement)
|
2011-02-13 19:15:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-22 18:43:59 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# creates a statment hash that contains default values for creating an impression via an AR relation.
|
|
|
|
def associative_create_statement(query_params={})
|
|
|
|
query_params.reverse_merge!(
|
|
|
|
:controller_name => controller_name,
|
|
|
|
:action_name => action_name,
|
|
|
|
:user_id => user_id,
|
|
|
|
:request_hash => @impressionist_hash,
|
|
|
|
:session_hash => session_hash,
|
|
|
|
:ip_address => request.remote_ip,
|
|
|
|
:referrer => request.referer
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-02-13 19:15:31 +00:00
|
|
|
private
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-02-13 19:15:31 +00:00
|
|
|
def bypass
|
2012-03-20 20:57:20 +00:00
|
|
|
Impressionist::Bots.bot?(request.user_agent)
|
2011-02-13 19:15:31 +00:00
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-11-09 17:10:35 +00:00
|
|
|
def unique_instance?(impressionable, unique_opts)
|
2012-01-19 20:27:36 +00:00
|
|
|
return unique_opts.blank? || !impressionable.impressions.where(unique_query(unique_opts)).exists?
|
2011-11-09 17:10:35 +00:00
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-11-09 17:10:35 +00:00
|
|
|
def unique?(unique_opts)
|
2012-01-19 20:27:36 +00:00
|
|
|
return unique_opts.blank? || !Impression.where(unique_query(unique_opts)).exists?
|
2011-11-09 17:10:35 +00:00
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-11-09 17:10:35 +00:00
|
|
|
# creates the query to check for uniqueness
|
|
|
|
def unique_query(unique_opts)
|
|
|
|
full_statement = direct_create_statement
|
|
|
|
# reduce the full statement to the params we need for the specified unique options
|
|
|
|
unique_opts.reduce({}) do |query, param|
|
|
|
|
query[param] = full_statement[param]
|
2011-11-07 19:41:51 +00:00
|
|
|
query
|
|
|
|
end
|
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-11-09 17:10:35 +00:00
|
|
|
# creates a statment hash that contains default values for creating an impression.
|
|
|
|
def direct_create_statement(query_params={})
|
|
|
|
query_params.reverse_merge!(
|
2013-05-08 00:40:59 +00:00
|
|
|
:impressionable_type => controller_name.singularize.camelize,
|
2011-11-09 17:10:35 +00:00
|
|
|
:impressionable_id=> params[:id]
|
|
|
|
)
|
|
|
|
associative_create_statement(query_params)
|
2011-11-07 19:41:51 +00:00
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-11-07 19:41:51 +00:00
|
|
|
def session_hash
|
|
|
|
# # careful: request.session_options[:id] encoding in rspec test was ASCII-8BIT
|
2011-11-09 17:10:35 +00:00
|
|
|
# # that broke the database query for uniqueness. not sure if this is a testing only issue.
|
2011-11-07 19:41:51 +00:00
|
|
|
# str = request.session_options[:id]
|
|
|
|
# logger.debug "Encoding: #{str.encoding.inspect}"
|
2011-11-09 17:10:35 +00:00
|
|
|
# # request.session_options[:id].encode("ISO-8859-1")
|
2011-11-07 19:41:51 +00:00
|
|
|
request.session_options[:id]
|
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
|
2011-02-13 19:15:31 +00:00
|
|
|
#use both @current_user and current_user helper
|
|
|
|
def user_id
|
|
|
|
user_id = @current_user ? @current_user.id : nil rescue nil
|
|
|
|
user_id = current_user ? current_user.id : nil rescue nil if user_id.blank?
|
|
|
|
user_id
|
|
|
|
end
|
|
|
|
end
|
2012-03-06 21:42:28 +00:00
|
|
|
end
|