ajout fonction nombre de vues

This commit is contained in:
Nicolas Bally 2015-01-21 16:19:30 +01:00
parent 4ada778208
commit 6febd47f2c
10 changed files with 75 additions and 6 deletions

View File

@ -28,7 +28,17 @@ class Forum::ForumTopicsController < ApplicationController
def show
@topic = ForumTopic.find(params[:id])
session[:topics] = session[:topics] || []
if session[:topics].include?(@topic.id)
topic_viewed = current_fuser.forum_user_topic_vieweds.where(:forum_topic_id => @topic.id).order("created_at DESC").first
topic_viewed.updated_at = Time.now
topic_viewed.save
else
current_fuser.topic_vieweds << @topic
session[:topics] << @topic.id
end
end

View File

@ -3,6 +3,7 @@
class ForumTopic < ActiveRecord::Base
belongs_to :forum_user
#attr_accessible :description, :title, :forum_messages_attributes, :category_id
has_many :forum_user_topic_vieweds
has_many :followed_topics
has_many :forum_users, :through => :followed_topics do

View File

@ -1,6 +1,12 @@
class ForumUser < ActiveRecord::Base
#attr_accessible :bio, :email, :firstname, :name, :password, :password_confirmation, :avatar, :moderator
has_many :topic_vieweds, :through => :forum_user_topic_vieweds, :source => :forum_topic
has_many :forum_user_topic_vieweds
has_many :followed_topics
has_many :forum_topics, :through => :followed_topics do

View File

@ -0,0 +1,4 @@
class ForumUserTopicViewed < ActiveRecord::Base
belongs_to :forum_topic
belongs_to :forum_user
end

View File

@ -1,7 +1,14 @@
%tr.topic#topic{:id => forum_topic.id}
%td=link_to forum_topic.title, [:forum, forum_topic]
%td{:style => "width:160px;"}=forum_topic.category.title if forum_topic.category
%td{:style => "width:100px;"}=forum_topic.forum_messages.count - 1
%td{:style => "width:100px;"}
=forum_topic.forum_messages.count - 1
%br
vu
= forum_topic.forum_user_topic_vieweds.count
fois
-#views = forum_topic.forum_user_topic_vieweds.select("DISTINCT forum_user_id").count
%td{:style => "width:160px;"}
= forum_topic.username
%br

View File

@ -1,4 +1,3 @@
=link_to "ajouter un message", new_forum_forum_message_path(:topic_id => @topic.id), :class => "btn btn-primary", :style => "margin:1em 0em;float:right"
%h1

View File

@ -0,0 +1,10 @@
class CreateForumUserTopicVieweds < ActiveRecord::Migration
def change
create_table :forum_user_topic_vieweds do |t|
t.references :forum_topic, index: true
t.references :forum_user, index: true
t.timestamps
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141209215455) do
ActiveRecord::Schema.define(version: 20150121145821) do
create_table "admins", force: true do |t|
t.string "email", default: "", null: false
@ -24,14 +24,20 @@ ActiveRecord::Schema.define(version: 20141209215455) do
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "name"
t.string "firstname"
t.string "file"
t.string "avatar"
t.string "password_digest", default: "", null: false
t.string "remember_token"
end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree
create_table "albums", force: true do |t|
t.string "name"
t.boolean "super_admin"
@ -270,6 +276,16 @@ ActiveRecord::Schema.define(version: 20141209215455) do
add_index "forum_user_images", ["album_id_id"], name: "index_forum_user_images_on_album_id_id", using: :btree
add_index "forum_user_images", ["forum_user_id"], name: "index_forum_user_images_on_forum_user_id", using: :btree
create_table "forum_user_topic_vieweds", force: true do |t|
t.integer "forum_topic_id"
t.integer "forum_user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "forum_user_topic_vieweds", ["forum_topic_id"], name: "index_forum_user_topic_vieweds_on_forum_topic_id", using: :btree
add_index "forum_user_topic_vieweds", ["forum_user_id"], name: "index_forum_user_topic_vieweds_on_forum_user_id", using: :btree
create_table "forum_users", force: true do |t|
t.string "name"
t.string "firstname"

View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
forum_topic_id:
forum_user_id:
two:
forum_topic_id:
forum_user_id:

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ForumUserTopicViewedTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end