2013-02-06 03:16:51 +08:00
|
|
|
require 'ipaddr'
|
|
|
|
|
|
|
|
class View < ActiveRecord::Base
|
2013-02-07 23:45:24 +08:00
|
|
|
belongs_to :parent, polymorphic: true
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :user
|
2013-06-25 12:18:54 +08:00
|
|
|
validates_presence_of :parent_type, :parent_id, :ip_address, :viewed_at
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# TODO: This could happen asyncronously
|
|
|
|
def self.create_for(parent, ip, user=nil)
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Only store a view once per day per thing per user per ip
|
|
|
|
redis_key = "view:#{parent.class.name}:#{parent.id}:#{Date.today.to_s}"
|
|
|
|
if user.present?
|
|
|
|
redis_key << ":user-#{user.id}"
|
|
|
|
else
|
|
|
|
redis_key << ":ip-#{ip}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if $redis.setnx(redis_key, "1")
|
|
|
|
$redis.expire(redis_key, 1.day.to_i)
|
|
|
|
|
|
|
|
View.transaction do
|
2013-06-25 12:18:54 +08:00
|
|
|
View.create(parent: parent, ip_address: ip, viewed_at: Date.today, user: user)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# Update the views count in the parent, if it exists.
|
|
|
|
if parent.respond_to?(:views)
|
2013-03-01 02:54:12 +08:00
|
|
|
parent.class.update_all 'views = views + 1', id: parent.id
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: views
|
|
|
|
#
|
|
|
|
# parent_id :integer not null
|
|
|
|
# parent_type :string(50) not null
|
|
|
|
# viewed_at :date not null
|
|
|
|
# user_id :integer
|
2013-06-25 12:18:54 +08:00
|
|
|
# ip_address :string not null
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_views_on_parent_id_and_parent_type (parent_id,parent_type)
|
|
|
|
#
|
|
|
|
|