Unverified Commit 4aea05e3 authored by azul's avatar azul
Browse files

upgrade: use namespace for notices

parent 1757adb2
......@@ -119,11 +119,11 @@ module Common::Requests
end
def create_notices
RequestNotice.create! @req if @req.persisted?
Notice::RequestNotice.create! @req if @req.persisted?
end
def dismiss_notices
RequestNotice.for_noticable(@req).dismiss_all unless @req.pending?
Notice::RequestNotice.for_noticable(@req).dismiss_all unless @req.pending?
end
def approved?
......
......@@ -25,7 +25,7 @@ class Group::InvitesController < Group::BaseController
groups = @group.network? ? recipients.groups : []
emails = recipients.emails
reqs, mailers = [], []
reqs = []
unless users.any? or emails.any? or groups.any?
raise_error('Recipient required')
end
......@@ -47,18 +47,29 @@ class Group::InvitesController < Group::BaseController
reqs << req
end
alert reqs
notify reqs
redirect_to action: :new
end
protected
def alert(reqs)
if reqs.detect { |req| !req.valid? }
reqs.each do |req|
alert_message req
RequestNotice.create! req if req.valid? && !req.is_a?(RequestToJoinUsViaEmail)
end
else
success reqs.first, count: reqs.size
params[:recipients] = ""
reqs.each { |req| RequestNotice.create! req if !req.is_a?(RequestToJoinUsViaEmail) }
end
end
redirect_to action: :new
def notify(reqs)
reqs
.select { |req| req.valid? && !req.is_a?(RequestToJoinUsViaEmail) }
.each { |req| Notice::RequestNotice.create! req }
end
end
......@@ -35,7 +35,7 @@ class Person::FriendRequestsController < Person::BaseController
private
def create_notice(request_obj)
RequestNotice.create! request_obj
Notice::RequestNotice.create! request_obj
end
end
......@@ -140,18 +140,12 @@ class Discussion < ActiveRecord::Base
def post_created(post)
self.posts_count += 1
update_attributes!(
posts_count: posts_count,
last_post: post,
replied_by_id: post.user_id,
replied_at: post.updated_at )
update_attributes_from_posts
if post.private?
PrivateMessageNotice.create! from: post.user,
post.private_message_notices.create! from: post.user,
user: post.discussion.user_talking_to(post.user),
message: post.body_html,
noticable_type: post.type,
noticable_id: post.id
message: post.body_html
end
end
......@@ -162,19 +156,14 @@ class Discussion < ActiveRecord::Base
if decrement
self.posts_count -= 1
end
update_attributes_from_posts
end
update_attributes!(
posts_count: posts_count,
def update_attributes_from_posts
update_attributes! posts_count: posts_count,
last_post: posts.visible.last,
replied_by_id: posts.visible.last.try.user_id,
replied_at: posts.visible.last.try.updated_at )
if post.private?
private_notices = PrivateMessageNotice.where(noticable_type: post.type,
noticable_id: post.id,
dismissed: false)
private_notices.each &:dismiss!
end
replied_at: posts.visible.last.try.updated_at
end
end
require 'cgi'
class PageNotice < Notice
class Notice::PageNotice < Notice
alias_attr :page, :noticable
attr_accessor :message
......
class PageUpdateNotice < PageNotice
class Notice::PageUpdateNotice < Notice::PageNotice
def display_title
I18n.t(:page_updated, data).html_safe
......
class PostStarredNotice < Notice
class Notice::PostStarredNotice < Notice
alias_attr :post, :noticable
attr_accessor :from
......
class PrivateMessageNotice < Notice
class Notice::PrivateMessageNotice < Notice
alias_attr :private_message, :noticable
attr_accessor :message
attr_accessor :from
......
class RequestNotice < Notice
class Notice::RequestNotice < Notice
alias_attr :request, :noticable
......
......@@ -76,7 +76,10 @@ class Page < ActiveRecord::Base
include Page::HistoryTracking # page <> page_history
has_many :page_notices, as: :noticable, dependent: :delete_all
has_many :page_notices,
class_name: 'Notice::PageNotice',
as: :noticable,
dependent: :delete_all
# disable timestamps, we set the updated_at field through certain Page::History subclasses
......
......@@ -193,7 +193,7 @@ class Page::Share
end
def send_notices users
PageNotice.create! recipients: users,
Notice::PageNotice.create! recipients: users,
page: page,
from: sender,
message: defaults[:send_message]
......
......@@ -21,7 +21,7 @@ class Post < ActiveRecord::Base
belongs_to :page_terms # if this is on a page we set page_terms so we can use path_finder
has_many :stars, as: :starred, dependent: :delete_all
has_many :post_starred_notices, as: :noticable, dependent: :delete_all
has_many :notices, as: :noticable, dependent: :delete_all
after_create :post_created
after_destroy :post_destroyed
......
......@@ -5,6 +5,10 @@ class PrivatePost < Post
dependent: :delete,
class_name: 'Activity::MessageSent'
has_many :private_message_notices,
class_name: 'Notice::PrivateMessageNotice',
as: :noticable
def private?
true
end
......
......@@ -48,7 +48,7 @@ class Request < ActiveRecord::Base
has_many :notices, as: :noticable,
dependent: :delete_all,
class_name: 'RequestNotice'
class_name: 'Notice::RequestNotice'
# most requests are non-vote based. they just need a single 'approve' action
# to get approved
......
......@@ -19,7 +19,7 @@ module Tracking::Action
update_user_access: ['Page::History::GrantUserAccess'],
update_title: ['Page::History::ChangeTitle'],
update_wiki: ['Page::History::UpdatedContent'],
create_star: ['PostStarredNotice']
create_star: ['Notice::PostStarredNotice']
}
def self.track(event, options = {})
......
......@@ -173,12 +173,12 @@ module User::Pages
page.updated_at = now
page.updated_by = self
page.user_participations.where(watch: true).each do |part|
notices = PageUpdateNotice.for_page(page).where(dismissed: false, user_id: part.user_id)
notices = Notice::PageUpdateNotice.for_page(page).where(dismissed: false, user_id: part.user_id)
.select { |notice| notice.data[:from] == self.name }
if notices.any?
notices.each &:touch
else
PageUpdateNotice.create!(user_id: part.user_id, page: page, from: self)
Notice::PageUpdateNotice.create!(user_id: part.user_id, page: page, from: self)
end
end
end
......
......@@ -23,7 +23,7 @@ module Crabgrass
config.autoload_paths << "#{Rails.root}/lib"
config.autoload_paths << "#{Rails.root}/app/models"
config.autoload_paths += %w(chat profile requests mailers notice).
config.autoload_paths += %w(chat profile requests mailers).
collect { |dir| "#{Rails.root}/app/models/#{dir}" }
config.autoload_paths << "#{Rails.root}/app/permissions"
config.autoload_paths << "#{Rails.root}/app/helpers/classes"
......
require File.dirname(__FILE__) + '/../../test_helper'
require 'test_helper'
class Group::InvitesControllerTest < ActionController::TestCase
......@@ -76,13 +76,9 @@ class Group::InvitesControllerTest < ActionController::TestCase
login_as @user
recipient = FactoryGirl.create(:user)
assert_difference 'RequestNotice.count' do
assert_notice_for(recipient) do
post :create, group_id: @group.to_param, recipients: recipient.name
end
notice = RequestNotice.last
assert_equal recipient.id, notice.user_id
assert_equal 'request_to_join_us', notice.data[:title]
end
def test_invite_to_join_us_notifies_all_valid_recipients
......@@ -90,16 +86,10 @@ class Group::InvitesControllerTest < ActionController::TestCase
recipient = FactoryGirl.create(:user)
# As @user already member of a group it should not be notified
assert_difference('RequestNotice.count', 2) do
assert_notice_for recipient, @user_not_in_group do
post :create, group_id: @group.to_param,
recipients: "#{recipient.name}, #{@user.name}, #{@user_not_in_group.name}"
end
notice = RequestNotice.last(2)
assert_equal recipient.id, notice.first.user_id
assert_equal @user_not_in_group.id, notice.last.user_id
assert_equal 'request_to_join_us', notice.first.data[:title]
assert_equal 'request_to_join_us', notice.last.data[:title]
end
def test_invite_group_to_network_notifies_all_group_members_if_no_council
......@@ -107,13 +97,11 @@ class Group::InvitesControllerTest < ActionController::TestCase
# @group has only two members, no council
assert_nil @group.council
assert_difference('RequestNotice.count', 2) do
assert_notice_for @user, @user_not_in_network do
post :create, group_id: @network.to_param, recipients: @group.name
end
notice = RequestNotice.last(2)
assert_equal @user.id, notice.first.user_id
assert_equal @user_not_in_network.id, notice.last.user_id
notice = Notice::RequestNotice.last(2)
assert_equal 'request_to_join_our_network', notice.first.data[:title]
assert_equal 'request_to_join_our_network', notice.last.data[:title]
end
......@@ -125,21 +113,32 @@ class Group::InvitesControllerTest < ActionController::TestCase
@group.council = @council
@group.save
assert_instance_of Group::Council, @group.council
assert_difference 'RequestNotice.count' do
assert_notice_for @user_council do
post :create, group_id: @network.to_param, recipients: @group.name
end
notice = RequestNotice.last
assert_equal @user_council.id, notice.user_id
assert_equal 'request_to_join_our_network', notice.data[:title]
end
def test_invite_by_email_does_not_notify_internally
email = "test@mail.me"
login_as @user
assert_no_difference 'RequestNotice.count' do
assert_no_notice do
post :create, group_id: @group.to_param, recipients: email
end
end
protected
def assert_no_notice(&block)
assert_notice_for &block
end
def assert_notice_for(*recipients, &block)
assert_difference 'Notice::RequestNotice.count', recipients.count, &block
notices = Notice::RequestNotice.last(recipients.count)
notices.each_with_index do |notice, i|
assert_equal recipients[i].id, notice.user_id
end
end
end
......@@ -18,7 +18,7 @@ class Me::NoticesControllerTest < ActionController::TestCase
# However the landing page is such an important entry point
# that we want to be sure to catch errors as well.
def test_index_with_invalid_notice
notice = PostStarredNotice.create user: @blue, noticable_type: 'Post',
notice = Notice::PostStarredNotice.create user: @blue, noticable_type: 'Post',
noticable_id: 987654321
get :index
assert_response :success
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment