added draft for impressionist(...) uniqueness

This commit is contained in:
mio 2011-11-09 18:10:35 +01:00
parent 09bfa2f27e
commit e63e835936
2 changed files with 41 additions and 40 deletions

View File

@ -12,11 +12,14 @@ module ImpressionistController
base.before_filter :impressionist_app_filter base.before_filter :impressionist_app_filter
end end
def impressionist(obj,message=nil) def impressionist(obj,message=nil,opts={})
unless bypass unless bypass
if obj.respond_to?("impressionable?") if obj.respond_to?("impressionable?")
obj.impressions.create(create_statement({:message => message})) if unique_instance?(obj, opts[:unique])
obj.impressions.create(associative_create_statement({:message => message}))
end
else else
# we could create an impression anyway. for classes, too. why not?
raise "#{obj.class.to_s} is not impressionable!" raise "#{obj.class.to_s} is not impressionable!"
end end
end end
@ -29,14 +32,8 @@ module ImpressionistController
def impressionist_subapp_filter(actions=nil,unique_opts=nil) def impressionist_subapp_filter(actions=nil,unique_opts=nil)
unless bypass unless bypass
actions.collect!{|a|a.to_s} unless actions.blank? actions.collect!{|a|a.to_s} unless actions.blank?
if (actions.blank? || actions.include?(action_name)) && (unique_opts.blank? || is_unique(unique_opts)) if (actions.blank? || actions.include?(action_name)) && unique?(unique_opts)
if (!actions.blank? && !unique_opts.blank?) Impression.create(direct_create_statement)
logger.info "Restricted to actions #{actions.inspect} and uniqueness for #{unique_opts.inspect}"
end
Impression.create(create_statement(
:impressionable_type => controller_name.singularize.camelize,
:impressionable_id=> params[:id]
))
end end
end end
end end
@ -50,50 +47,53 @@ module ImpressionistController
Impressionist::Bots::LIST.include? request.user_agent Impressionist::Bots::LIST.include? request.user_agent
end end
def is_unique(unique_opts) def unique_instance?(impressionable, unique_opts)
# FIXME think about uniqueness in relation to impressionable_id, impressionable_type and controller_name return unique_opts.blank? || impressionable.impressions.where(unique_query(unique_ops)).size == 0
# is controller name redundant? does the controller name always have to match?
default_statement = create_statement(
:impressionable_type => controller_name.singularize.camelize,
:impressionable_id=> params[:id]
)
statement = unique_opts.reduce({}) do |query, param|
query[param] = default_statement[param]
query
end
#logger.debug "Statement params: #{statement.inspect}."
# always use impressionable type?
statement[:impressionable_type] = controller_name.singularize.camelize
#statement[:impressionable_id] = params[:id]
return Impression.where(statement).size == 0
end end
# creates a statment hash that contains default values for creating an impression (without def unique?(unique_opts)
# :impressionable_type and impressionable_id as they are not needed for creating via association). return unique_opts.blank? || Impression.where(unique_query(unique_opts)).size == 0
def create_statement(query_params={}) end
# 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]
query
end
end
# 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!( query_params.reverse_merge!(
:controller_name => controller_name, :controller_name => controller_name,
:action_name => action_name, :action_name => action_name,
:user_id => user_id, :user_id => user_id,
:request_hash => @impressionist_hash, :request_hash => @impressionist_hash,
:session_hash => session_hash, :session_hash => session_hash,
:ip_address => remote_ip, :ip_address => request.remote_ip,
:referrer => request.referer :referrer => request.referer
) )
end end
def session_hash # creates a statment hash that contains default values for creating an impression.
# # careful: request.session_options[:id] encoding in rspec test was ASCII-8BIT def direct_create_statement(query_params={})
# # that broke the database query for uniqueness. not sure how to solve this issue query_params.reverse_merge!(
# # seems to depend on app setup/config :impressionable_type => controller_name.singularize.camelize,
# str = request.session_options[:id] :impressionable_id=> params[:id]
# # probably this isn't a fix: request.session_options[:id].encode("ISO-8859-1") )
# logger.debug "Encoding: #{str.encoding.inspect}" associative_create_statement(query_params)
request.session_options[:id]
end end
def remote_ip def session_hash
request.remote_ip # # careful: request.session_options[:id] encoding in rspec test was ASCII-8BIT
# # that broke the database query for uniqueness. not sure if this is a testing only issue.
# str = request.session_options[:id]
# logger.debug "Encoding: #{str.encoding.inspect}"
# # request.session_options[:id].encode("ISO-8859-1")
request.session_options[:id]
end end
#use both @current_user and current_user helper #use both @current_user and current_user helper

View File

@ -57,7 +57,8 @@ describe PostsController do
end end
it "should recognize referrer uniqueness" do it "should recognize referrer uniqueness" do
controller.stub!(:referrer).and_return("http://somehost.someurl.somdomain/some/path") @request.env['HTTP_REFERER'] = 'http://somehost.someurl.somdomain/some/path'
#controller.stub!(:referer).and_return("http://somehost.someurl.somdomain/some/path")
controller.impressionist_subapp_filter(nil, [:referrer]) controller.impressionist_subapp_filter(nil, [:referrer])
controller.impressionist_subapp_filter(nil, [:referrer]) controller.impressionist_subapp_filter(nil, [:referrer])
Impression.should have(@impression_count + 1).records Impression.should have(@impression_count + 1).records