From 0eb0eb971393b9d1ef8d1e3a15320fb2faf566d6 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Thu, 26 Jan 2012 21:04:58 +0400 Subject: [PATCH] [refs #123] Activity Feed logic global refactoring. --- app/helpers/activity_feeds_helper.rb | 3 ++ app/models/activity_feed.rb | 6 +++ app/models/activity_feed_observer.rb | 40 ++++++++++++++----- app/views/activity_feeds/index.html.haml | 2 +- .../partials/issue_assign_notification.haml | 8 +--- .../partials/new_comment_notification.haml | 10 ++--- .../new_comment_reply_notification.haml | 10 ++--- .../partials/new_issue_notification.haml | 8 +--- .../partials/new_user_notification.haml | 10 ++--- config/locales/ru.yml | 20 ++++++++++ .../20120124065207_create_activity_feeds.rb | 3 +- db/schema.rb | 3 +- 12 files changed, 78 insertions(+), 45 deletions(-) diff --git a/app/helpers/activity_feeds_helper.rb b/app/helpers/activity_feeds_helper.rb index 54a624bad..f3facbf94 100644 --- a/app/helpers/activity_feeds_helper.rb +++ b/app/helpers/activity_feeds_helper.rb @@ -1,2 +1,5 @@ module ActivityFeedsHelper + def render_activity_feed(activity_feed) + render :partial => activity_feed.partial, :locals => activity_feed.data + end end diff --git a/app/models/activity_feed.rb b/app/models/activity_feed.rb index 0708e6df6..934ef56ea 100644 --- a/app/models/activity_feed.rb +++ b/app/models/activity_feed.rb @@ -1,3 +1,9 @@ class ActivityFeed < ActiveRecord::Base belongs_to :user + + serialize :data + + def partial + 'activity_feeds/partials/' + self.kind + end end diff --git a/app/models/activity_feed_observer.rb b/app/models/activity_feed_observer.rb index 89eff315b..eaf3c6772 100644 --- a/app/models/activity_feed_observer.rb +++ b/app/models/activity_feed_observer.rb @@ -4,18 +4,26 @@ class ActivityFeedObserver < ActiveRecord::Observer def after_create(record) case record.class.to_s when 'User' - ActivityFeed.create(:user => record, :body => render_body('new_user_notification')) + ActivityFeed.create(:user => record, :kind => 'new_user_notification', :date => {:user_name => record.name, :user_email => record.email, :password => user.password}) when 'Issue' recipients = record.collect_recipient_ids recipients.each do |recipient_id| recipient = User.find(recipient_id) UserMailer.delay.new_issue_notification(record, recipient) if User.find(recipient).notifier.can_notify && User.find(recipient).notifier.new_issue - ActivityFeed.create(:user => recipient, :body => render_body('new_issue_notification', {:user => recipient, :issue => record})) + ActivityFeed.create( + :user => recipient, + :kind => 'new_issue_notification', + :data => {:user_name => recipient.name, :issue_id => record.id, :issue_title => record.title, :project_id => record.project.id, :project_name => record.project.name} + ) end UserMailer.delay.issue_assign_notification(record, record.user) if record.user_id_was != record.user_id && record.user.notifier.issue_assign && record.user.notifier.can_notify - ActivityFeed.create(:user => record.user, :body => render_body('issue_assign_notification', {:user => record.user, :issue => record})) + ActivityFeed.create( + :user_name => record.user.name, + :kind => 'issue_assign_notification', + :data => {:user_name => record.user.name, :issue_id => record.id, :project_id => record.project.id, :issue_title => record.title} + ) when 'Comment' subscribes = record.commentable.subscribes @@ -23,10 +31,18 @@ class ActivityFeedObserver < ActiveRecord::Observer if record.user_id != subscribe.user_id if record.reply? subscribe UserMailer.delay.new_comment_reply_notification(record, subscribe.user) if record.can_notify_on_reply?(subscribe) - ActivityFeed.create(:user => subscribe.user, :body => render_body('new_comment_reply_notification', {:user => subscribe.user, :comment => record})) + ActivityFeed.create( + :user => subscribe.user, + :kind => 'new_comment_reply_notification', + :data => {:user_name => subscribe.user.name, :comment_body => record.body, :issue_title => record.commentable.title, :issue_id => record.commentable.id, :project_id => record.commentable.project.id} + ) else UserMailer.delay.new_comment_notification(record, subscribe.user) if record.can_notify_on_new_comment?(subscribe) - ActivityFeed.create(:user => subscribe.user, :body => render_body('new_comment_notification', {:user => subscribe.user, :comment => record})) + ActivityFeed.create( + :user => subscribe.user, + :kind => 'new_comment_notification', + :data => {:user_name => subscribe.user.name, :comment_body => record.body, :issue_title => record.commentable.title, :issue_id => record.commentable.id, :project_id => record.commentable.project.id} + ) end end end @@ -37,13 +53,17 @@ class ActivityFeedObserver < ActiveRecord::Observer case record.class.to_s when 'Issue' UserMailer.delay.issue_assign_notification(record, record.user) if record.user_id_was != record.user_id && record.user.notifier.issue_assign && record.user.notifier.can_notify - ActivityFeed.create(:user => record.user, :body => render_body('issue_assign_notification', {:user => record.user, :issue => record})) + ActivityFeed.create( + :user_name => record.user.name, + :kind => 'issue_assign_notification', + :data => {:user_name => record.user.name, :issue_id => record.id, :project_id => record.project.id, :issue_title => record.title} + ) end end - def render_body(partial_name, locals={}) - @@stub_controller ||= StubController.new - @@stub_controller.partial_to_string('activity_feeds/partials/' + partial_name, locals) - end + #def (partial_name, locals={}) + # @@stub_controller ||= StubController.new + # @@stub_controller.partial_to_string('activity_feeds/partials/' + partial_name, locals) + #end end diff --git a/app/views/activity_feeds/index.html.haml b/app/views/activity_feeds/index.html.haml index 85f1ba5c6..838d9a62a 100644 --- a/app/views/activity_feeds/index.html.haml +++ b/app/views/activity_feeds/index.html.haml @@ -15,7 +15,7 @@ %br %br .item - = raw activity_feed.body + = render_activity_feed(activity_feed) %br %br %br diff --git a/app/views/activity_feeds/partials/issue_assign_notification.haml b/app/views/activity_feeds/partials/issue_assign_notification.haml index 414f8e6c3..1dc74a671 100644 --- a/app/views/activity_feeds/partials/issue_assign_notification.haml +++ b/app/views/activity_feeds/partials/issue_assign_notification.haml @@ -1,7 +1,3 @@ -%p== Здравствуйте, #{user.name}. +%p== #{ t("notifications.bodies.issue_assign_notification.title", :user_name => user_name) } - -%p Вам была назначена задача #{ link_to issue.title, [issue.project, issue] } - - -%p== Команда поддержки «ROSA Build System» +%p #{ t("notifications.bodies.issue_assign_notification.content", :issue_link => link_to(issue_title, [project_id, issue_id])) } diff --git a/app/views/activity_feeds/partials/new_comment_notification.haml b/app/views/activity_feeds/partials/new_comment_notification.haml index dd09ab701..bbd60a77b 100644 --- a/app/views/activity_feeds/partials/new_comment_notification.haml +++ b/app/views/activity_feeds/partials/new_comment_notification.haml @@ -1,9 +1,5 @@ -%p== Здравствуйте, #{user.name}. +%p== #{ t("notifications.bodies.new_comment_notification.title", :user_name => user_name) } +%p #{ t("notifications.bodies.new_comment_notification.content", :issue_link => link_to(issue_title, [project_id, issue_id])) } -%p К задаче #{ link_to comment.commentable.title, [comment.commentable.project, comment.commentable] } был добавлен новый комментарий. - -%p "#{ comment.body }" - - -%p== Команда поддержки «ROSA Build System» +%p "#{ comment_body }" diff --git a/app/views/activity_feeds/partials/new_comment_reply_notification.haml b/app/views/activity_feeds/partials/new_comment_reply_notification.haml index 9aa223deb..a0367f840 100644 --- a/app/views/activity_feeds/partials/new_comment_reply_notification.haml +++ b/app/views/activity_feeds/partials/new_comment_reply_notification.haml @@ -1,9 +1,5 @@ -%p== Здравствуйте, #{user.name}. +%p== #{ t("notifications.bodies.new_comment_reply_notification.title", :user_name => user_name) } +%p #{ t("notifications.bodies.new_comment_reply_notification.content", :issue_link => link_to(issue_title, [project_id, issue_id])) } -%p На Ваш комментарий в задаче #{ link_to comment.commentable.title, [comment.commentable.project, comment.commentable] } был дан ответ. - -%p "#{ comment.body }" - - -%p== Команда поддержки «ROSA Build System» +%p "#{ comment_body }" diff --git a/app/views/activity_feeds/partials/new_issue_notification.haml b/app/views/activity_feeds/partials/new_issue_notification.haml index a5a139fd6..75d2269a7 100644 --- a/app/views/activity_feeds/partials/new_issue_notification.haml +++ b/app/views/activity_feeds/partials/new_issue_notification.haml @@ -1,7 +1,3 @@ -%p== Здравствуйте, #{user.name}. +%p== #{ t("notifications.bodies.new_issue_notification.title", :user_name => user_name) } - -%p К проекту #{ link_to issue.project.name, project_path(issue.project) } была добавлена задача #{ link_to issue.title, [issue.project, issue] } - - -%p== Команда поддержки «ROSA Build System» +%p #{ t("notifications.bodies.new_issue_notification.content", :issue_link => link_to(issue_title, [project_id, issue_id]), :project_link => link_to(project_name, project_path(project_id)) } diff --git a/app/views/activity_feeds/partials/new_user_notification.haml b/app/views/activity_feeds/partials/new_user_notification.haml index 191e2ad5f..13339f00d 100644 --- a/app/views/activity_feeds/partials/new_user_notification.haml +++ b/app/views/activity_feeds/partials/new_user_notification.haml @@ -1,12 +1,10 @@ -%p== Здравствуйте, #{user.name}. +%p== #{ t("notifications.bodies.new_user_notification.title", :user_name => user_name) } -%p Вы зарегистрированы на проекте «ROSA Build System» и теперь можете войти в систему. +%p #{ t("notifications.bodies.new_user_notification.content") } %p - ==Ваш email : #{user.email} + #{ t("notifications.bodies.new_user_notification.email"), :user_email => user_email } %br/ - ==Ваш пароль: #{user.password} - -%p== Команда поддержки «ROSA Build System» + #{ t("notifications.bodies.new_user_notification.password"), :user_password => user_email } diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 6ba1c7ea6..58ab6c0fa 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -692,6 +692,26 @@ ru: notifications: subjects: new_comment_notification: Новый комментарий к Вашей задаче + new_comment_reply_notification: Новый комментарий к Вашей задаче new_issue_notification: Новая задача добавлена к проекту new_user_notification: Регистрация на проекте «%{ project_name }» issue_assign_notification: Вам назначили задачу + + bodies: + new_comment_notification: + title: Здравствуйте, %{user_name}. + content: К задаче %{ issue_link } был добавлен новый комментарий. + new_comment_reply_notification: + title: Здравствуйте, %{user_name}. + content: На Ваш комментарий в задаче %{ issue_link } был дан ответ. + new_issue_notification: + title: Здравствуйте, %{user_name}. + content: К проекту %{ project_link } была добавлена задача %{ issue_link } + new_user_notification: + title: Здравствуйте, %{user_name}. + content: Вы зарегистрированы на проекте «ROSA Build System» и теперь можете войти в систему. + email: ==Ваш email : %{user_email} + password: ==Ваш пароль: %{user_password} + issue_assign_notification: + title: Здравствуйте, %{user_name}. + content: Вам была назначена задача %{ issue_link } diff --git a/db/migrate/20120124065207_create_activity_feeds.rb b/db/migrate/20120124065207_create_activity_feeds.rb index 6321f4b58..d5b13fc3a 100644 --- a/db/migrate/20120124065207_create_activity_feeds.rb +++ b/db/migrate/20120124065207_create_activity_feeds.rb @@ -2,7 +2,8 @@ class CreateActivityFeeds < ActiveRecord::Migration def self.up create_table :activity_feeds do |t| t.integer :user_id, :null => false - t.text :body + t.string :kind + t.text :data t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index 038fc0423..c97d7408f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -14,7 +14,8 @@ ActiveRecord::Schema.define(:version => 20120124065207) do create_table "activity_feeds", :force => true do |t| t.integer "user_id", :null => false - t.text "body" + t.string "kind" + t.text "data" t.datetime "created_at" t.datetime "updated_at" end