51 lines
1.2 KiB
Ruby
51 lines
1.2 KiB
Ruby
class DelayedImpressionist
|
|
def self.before_filter(*args)
|
|
return
|
|
end
|
|
|
|
include ImpressionistController::InstanceMethods
|
|
@queue = :low
|
|
|
|
def initialize(request,controller_name,action_name,user,obj)
|
|
@controller_name = controller_name
|
|
@action_name = action_name
|
|
@user = User.find user unless user.nil?
|
|
@request = request
|
|
end
|
|
|
|
def self.perform(*args)
|
|
arg = args[0]
|
|
@request= DelayImpressionistRequest.new
|
|
@request.restore(arg["request"])
|
|
obj = eval("#{arg["class"]}.find '#{arg['obj']['_id']}'")
|
|
# imp = new(@request,arg["controller_name"],arg["action_name"],arg["user"],obj)
|
|
new_impression = obj.impressions.build(:user_id=>arg["user"],:controller_name=>arg["controller_name"],:action_name=>arg["action_name"],:ip_address=>@request.remote_ip,:referrer=>@request.referer)
|
|
@try = 1
|
|
loop do
|
|
result = new_impression.save rescue false
|
|
break if (result || @try >= 10)
|
|
@try = @try + 1
|
|
puts "trying:#{@try}"
|
|
end
|
|
new_impression.save
|
|
obj.update_attribute(:view_count,obj.impression_count)
|
|
end
|
|
|
|
private
|
|
def user
|
|
@user
|
|
end
|
|
|
|
def action_name
|
|
@action_name
|
|
end
|
|
|
|
def controller_name
|
|
@controller_name
|
|
end
|
|
|
|
def request
|
|
@request
|
|
end
|
|
|
|
end |