diff --git a/app/controllers/forum/forum_topics_controller.rb b/app/controllers/forum/forum_topics_controller.rb index 8429d86..a4606ee 100644 --- a/app/controllers/forum/forum_topics_controller.rb +++ b/app/controllers/forum/forum_topics_controller.rb @@ -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 diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index fcab7eb..5ab5eff 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -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 diff --git a/app/models/forum_user.rb b/app/models/forum_user.rb index e2c7df8..7a32fe9 100644 --- a/app/models/forum_user.rb +++ b/app/models/forum_user.rb @@ -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 diff --git a/app/models/forum_user_topic_viewed.rb b/app/models/forum_user_topic_viewed.rb new file mode 100644 index 0000000..521a057 --- /dev/null +++ b/app/models/forum_user_topic_viewed.rb @@ -0,0 +1,4 @@ +class ForumUserTopicViewed < ActiveRecord::Base + belongs_to :forum_topic + belongs_to :forum_user +end diff --git a/app/views/forum/forum_topics/_forum_topic.haml b/app/views/forum/forum_topics/_forum_topic.haml index bb5ae82..485bf59 100644 --- a/app/views/forum/forum_topics/_forum_topic.haml +++ b/app/views/forum/forum_topics/_forum_topic.haml @@ -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 diff --git a/app/views/forum/forum_topics/show.haml b/app/views/forum/forum_topics/show.haml index 7559b1a..20c0169 100644 --- a/app/views/forum/forum_topics/show.haml +++ b/app/views/forum/forum_topics/show.haml @@ -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 diff --git a/db/migrate/20150121145821_create_forum_user_topic_vieweds.rb b/db/migrate/20150121145821_create_forum_user_topic_vieweds.rb new file mode 100644 index 0000000..8ed38ad --- /dev/null +++ b/db/migrate/20150121145821_create_forum_user_topic_vieweds.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f48d234..4b689fa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/test/fixtures/forum_user_topic_vieweds.yml b/test/fixtures/forum_user_topic_vieweds.yml new file mode 100644 index 0000000..4a2bb3f --- /dev/null +++ b/test/fixtures/forum_user_topic_vieweds.yml @@ -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: diff --git a/test/models/forum_user_topic_viewed_test.rb b/test/models/forum_user_topic_viewed_test.rb new file mode 100644 index 0000000..1a82d7e --- /dev/null +++ b/test/models/forum_user_topic_viewed_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ForumUserTopicViewedTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end