notifications forum

This commit is contained in:
Nicolas Bally 2014-11-22 00:11:40 +01:00
parent 81fc99d138
commit 6770841072
13 changed files with 200 additions and 3 deletions

View File

@ -8,6 +8,22 @@ class ForumMails < ActionMailer::Base
@options = options
mail(:to => forum_user.email, :subject => "Réinitialisation de votre mot de passe.")
end
def topic_notification(forum_topic, forum_user, options = {})
@forum_user = forum_user
@forum_topic = forum_topic
@options = options
mail(:to => forum_user.email, :subject => "Forum Pic Vert - Nouveau fil de discussion")
end
def message_notification(forum_message, forum_user, options = {})
@forum_user = forum_user
@forum_message = forum_message
@options = options
mail(:to => forum_user.email, :subject => "Forum Pic Vert - Nouveau message")
end

View File

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

View File

@ -6,8 +6,43 @@ class ForumMessage < ActiveRecord::Base
validates :description, :presence => true
belongs_to :topic, :class_name => "ForumTopic"
belongs_to :topic, :class_name => "ForumTopic", :foreign_key => :forum_topic_id
belongs_to :forum_user
after_create do
topic = ForumTopic.find(self.forum_topic_id)
user_to_notify = []
ForumUser.where(:new_messages_notifications => true).each do |forum_user|
user_to_notify << forum_user
end
FollowedTopic.where(:forum_topic_id => self.forum_topic_id).each do |followed_topic|
user_to_notify << followed_topic.forum_user
end
if self.forum_user.follow_participated_topics
folowed_topic = forum_user.followed_topics.find_or_initialize_by_forum_topic_id(topic.id)
folowed_topic.save
end
user_to_notify.uniq!
user_to_notify.each do |forum_user|
ForumMails.message_notification(self, forum_user).deliver
end
end
def username
if self.forum_user
self.forum_user.firstname.to_s+" "+

View File

@ -4,8 +4,14 @@ class ForumTopic < ActiveRecord::Base
belongs_to :forum_user
#attr_accessible :description, :title, :forum_messages_attributes, :category_id
has_many :followed_topics
has_many :forum_users, :through => :followed_topics do
def enabled
where(":followed_topic.enabled = ?", true)
end
end
has_many :active_followers
has_many :forum_messages
accepts_nested_attributes_for :forum_messages
@ -18,6 +24,49 @@ class ForumTopic < ActiveRecord::Base
default_scope :include => :forum_messages, :order => "forum_messages.created_at DESC"
after_create do
topic = self
user_to_notify = []
ForumUser.where(:new_messages_notifications => true).each do |forum_user|
user_to_notify << forum_user
end
ForumUser.where(:new_topic_notifications => true).each do |forum_user|
user_to_notify << forum_user
end
ForumUser.where(:follow_new_topics => true).each do |forum_user|
folowed_topic = forum_user.followed_topics.find_or_initialize_by_forum_topic_id(topic.id)
folowed_topic.save
end
FollowedTopic.where(:forum_topic_id => topic.id).each do |followed_topic|
user_to_notify << followed_topic.forum_user
end
if self.forum_user.follow_participated_topics
folowed_topic = forum_user.followed_topics.find_or_initialize_by_forum_topic_id(topic.id)
folowed_topic.save
end
user_to_notify.uniq!
user_to_notify.each do |forum_user|
ForumMails.topic_notification(topic, forum_user).deliver
end
end
def username
if self.forum_user

View File

@ -1,6 +1,14 @@
class ForumUser < ActiveRecord::Base
#attr_accessible :bio, :email, :firstname, :name, :password, :password_confirmation, :avatar, :moderator
has_many :followed_topics
has_many :forum_topics, :through => :followed_topics do
def enabled
where(":followed_topic.enabled = ?", true)
end
end
has_secure_password
validates_presence_of :password, :on => :create
validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

View File

@ -10,6 +10,24 @@
= f.input :avatar, :label => "Image de profil"
%h4 Notifications
%p
Les notifications vous sont envoyées par mail, vous pouvez décider ci-dessous de les recevoir ou non :
%br
%br
= f.input :new_messages_notifications, :label => "recevoir une notification pour tout les nouveaux messages"
= f.input :new_topic_notifications, :label => "recevoir une notification lorsqu'un nouveau fil de discussion est créé"
%h4 Suivi des fils de discussions
%p
Lorsque vous suivez un fil de discussion vous recevez un mail à chaque nouveau message.
%br
%br
=f.input :follow_new_topics, :label => "suivre automatiquement tout les nouveaux fils de discussions"
=f.input :follow_participated_topics, :label => "suivre automatiquement tout les fils de discussions auxquels je participe"
%br
=f.submit "Sauvegarder", :class => "btn btn-primary"

View File

@ -0,0 +1,6 @@
%h2 Nouveau message sur le fil de discussion :
%h1= @forum_message.topic.title
%p=link_to forum_forum_topic_url(:id => @forum_message.topic.id, :anchor => "forum_message_#{@forum_message.id}"), forum_forum_topic_url(:id => @forum_message.topic.id, :anchor => "forum_message_#{@forum_message.id}")
%h2 Contenu :
=sanitize @forum_message.description

View File

@ -0,0 +1,6 @@
%h2 Nouveau fil de discussion :
%h1= @forum_topic.title
%p=link_to forum_forum_topic_url(:id => @forum_topic.id), forum_forum_topic_url(:id => @forum_topic.id)
%h2 Contenu :
=sanitize @forum_topic.forum_messages.order("created_at ASC").first.description

View File

@ -0,0 +1,11 @@
class AddNotificationToForumUsers < ActiveRecord::Migration
def change
add_column :forum_users, :new_messages_notifications, :boolean #recevoir une notification pour tout les nouveaux messages
add_column :forum_users, :new_topic_notifications, :boolean #recevoir une notification lorsqu'un nouveau fil de discussion est lancé
#suivi sur les topics
add_column :forum_users, :follow_new_topics, :boolean #suivre automatiquement tout les nouveaux fils de discussions
add_column :forum_users, :follow_participated_topics, :boolean #suivre automatiquement tout les fils de discussions auxquels je participe
end
end

View File

@ -0,0 +1,11 @@
class CreateFollowedTopics < ActiveRecord::Migration
def change
create_table :followed_topics do |t|
t.references :forum_user, index: true
t.references :forum_topic, index: true
t.boolean :enabled
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: 20141030173048) do
ActiveRecord::Schema.define(version: 20141121211542) do
create_table "admins", force: true do |t|
t.string "email", default: "", null: false
@ -71,6 +71,17 @@ ActiveRecord::Schema.define(version: 20141030173048) do
t.datetime "updated_at"
end
create_table "followed_topics", force: true do |t|
t.integer "forum_user_id"
t.integer "forum_topic_id"
t.boolean "enabled"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "followed_topics", ["forum_topic_id"], name: "index_followed_topics_on_forum_topic_id", using: :btree
add_index "followed_topics", ["forum_user_id"], name: "index_followed_topics_on_forum_user_id", using: :btree
create_table "forum_categories", force: true do |t|
t.string "title"
t.text "description"
@ -143,6 +154,10 @@ ActiveRecord::Schema.define(version: 20141030173048) do
t.boolean "new_message_on_my_topics_notification"
t.datetime "last_new_message_on_my_topics_notification"
t.string "auth_token"
t.boolean "new_messages_notifications"
t.boolean "new_topic_notifications"
t.boolean "follow_new_topics"
t.boolean "follow_participated_topics"
end
create_table "forums", force: true do |t|

11
test/fixtures/followed_topics.yml vendored Normal file
View File

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

View File

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