Skip to content
Snippets Groups Projects
Commit f04cc23c authored by Marcela Ribeiro de Oliveira's avatar Marcela Ribeiro de Oliveira
Browse files

Merge branch 'master' of gitlab.c3sl.ufpr.br:portalmec/portalmec into publishers

parents 726d4995 9ace85ee
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
# along with portalmec. If not, see <http://www.gnu.org/licenses/>. # along with portalmec. If not, see <http://www.gnu.org/licenses/>.
## This concern has +like+, +unlike+ controller actions ## 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 module SociableController
extend ActiveSupport::Concern extend ActiveSupport::Concern
...@@ -55,7 +56,8 @@ module SociableController ...@@ -55,7 +56,8 @@ module SociableController
end end
def view_object! 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
end end
...@@ -46,8 +46,8 @@ module Sociable ...@@ -46,8 +46,8 @@ module Sociable
!Share.where(user: user, shareable: self).blank? !Share.where(user: user, shareable: self).blank?
end end
def view(user) def view(user, ip)
View.create(user: user, viewable: self) View.create(user: user, ip: ip, viewable: self)
end end
def viewed?(user) def viewed?(user)
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# user_id :integer # user_id :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# ip :string
# #
class View < ApplicationRecord class View < ApplicationRecord
...@@ -34,14 +35,12 @@ class View < ApplicationRecord ...@@ -34,14 +35,12 @@ class View < ApplicationRecord
include Trackable include Trackable
belongs_to :viewable, polymorphic: true, counter_cache: true 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 before_create :current_time_greater_than_last
scope :created_last, ->(user) { where(user: user).order('created_at DESC').limit(1) }
def recipient def recipient
viewable viewable
end end
...@@ -49,7 +48,7 @@ class View < ApplicationRecord ...@@ -49,7 +48,7 @@ class View < ApplicationRecord
private private
def current_time_greater_than_last 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? unless last_view.blank?
return false if Time.current < (last_view.created_at + 1.day) return false if Time.current < (last_view.created_at + 1.day)
......
# 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment