diff --git a/app/controllers/concerns/sociable_controller.rb b/app/controllers/concerns/sociable_controller.rb index 82a96c83234c9582d091923146c53e115c78fef1..022ff389096935fb798386a6b05c267d39cc1288 100644 --- a/app/controllers/concerns/sociable_controller.rb +++ b/app/controllers/concerns/sociable_controller.rb @@ -18,7 +18,8 @@ # along with portalmec. If not, see <http://www.gnu.org/licenses/>. ## This concern has +like+, +unlike+ controller actions -## When you use it, be sure that +set_sociable+ method is implemented in your controller, otherwise an fatal error will raise. +## When you use it, be sure that +set_sociable+ method is implemented in your controller, otherwise a fatal error will raise. + module SociableController extend ActiveSupport::Concern @@ -55,7 +56,8 @@ module SociableController end def view_object! - sociable.view current_user if user_signed_in? + # Change request.remote_ip to req.env["HTTP_X_REAL_IP"] in production + sociable.view(current_user, request.remote_ip) end end diff --git a/app/models/concerns/sociable.rb b/app/models/concerns/sociable.rb index e199ac7bc1bcb1d3b457ab938478469665f14f79..3dfaaebf7aaec6ded03626a3e074f6eaaedf1b33 100644 --- a/app/models/concerns/sociable.rb +++ b/app/models/concerns/sociable.rb @@ -46,8 +46,8 @@ module Sociable !Share.where(user: user, shareable: self).blank? end - def view(user) - View.create(user: user, viewable: self) + def view(user, ip) + View.create(user: user, ip: ip, viewable: self) end def viewed?(user) diff --git a/app/models/view.rb b/app/models/view.rb index 2e2d995e58d8e375c5126767510c1eef16feae3d..c6e4f9f1fc93726f5d0c2723080ace7bed326853 100644 --- a/app/models/view.rb +++ b/app/models/view.rb @@ -27,6 +27,7 @@ # user_id :integer # created_at :datetime not null # updated_at :datetime not null +# ip :string # class View < ApplicationRecord @@ -34,14 +35,12 @@ class View < ApplicationRecord include Trackable belongs_to :viewable, polymorphic: true, counter_cache: true - belongs_to :user + belongs_to :user, optional: true - validates_presence_of :user, :viewable + validates_presence_of :viewable, :ip before_create :current_time_greater_than_last - scope :created_last, ->(user) { where(user: user).order('created_at DESC').limit(1) } - def recipient viewable end @@ -49,7 +48,7 @@ class View < ApplicationRecord private def current_time_greater_than_last - last_view = viewable.views.created_last(user).first + last_view = viewable.views.where(user: user, ip: ip).order('created_at DESC').limit(1).first unless last_view.blank? return false if Time.current < (last_view.created_at + 1.day) diff --git a/db/migrate/20180406125006_add_ip_to_views.rb b/db/migrate/20180406125006_add_ip_to_views.rb new file mode 100644 index 0000000000000000000000000000000000000000..1af87bf47696a4ad206b7bd98d071d2106f52639 --- /dev/null +++ b/db/migrate/20180406125006_add_ip_to_views.rb @@ -0,0 +1,24 @@ + +# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre +# Departamento de Informatica - Universidade Federal do Parana +# +# This file is part of portalmec. +# +# portalmec is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# portalmec is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with portalmec. If not, see <http://www.gnu.org/licenses/>. + +class AddIpToViews < ActiveRecord::Migration[5.0] + def change + add_column :views, :ip, :string + end +end