Commit 83ae408b authored by azul's avatar azul
Browse files

handle request notices from controller not observer

remove RequestObserver
parent ec6ce585
......@@ -13,7 +13,9 @@ module Common::Requests
helper_method :request_path
helper_method :requests_path
before_filter :fetch_request, only: [:update, :destroy, :show]
after_filter :track_activity, if: :approved?
after_filter :track_activity, if: :approved?, only: :update
after_filter :create_notices, only: :create
after_filter :dismiss_notices, only: :update
end
end
......@@ -32,8 +34,8 @@ module Common::Requests
#
def update
if mark
@request.mark!(mark, current_user)
success I18n.t(@request.name), success_message
@req.mark!(mark, current_user)
success I18n.t(@req.name), success_message
end
render template: 'common/requests/update'
end
......@@ -43,7 +45,7 @@ module Common::Requests
# uses model permissions.
#
def destroy
@request.destroy_by!(current_user)
@req.destroy_by!(current_user)
notice request_destroyed_message, :later
render(:update) {|page| page.redirect_to requests_path}
end
......@@ -59,7 +61,7 @@ module Common::Requests
end
def request_destroyed_message
:thing_destroyed.tcap thing: I18n.t(@request.name, count: 1)
:thing_destroyed.tcap thing: I18n.t(@req.name, count: 1)
end
#def left_id(request)
......@@ -79,9 +81,9 @@ module Common::Requests
end
def fetch_request
@request = request_context.find(params[:id])
if params[:code] && @request.recipient != current_user
@request.try.redeem_code!(current_user)
@req = request_context.find(params[:id])
if params[:code] && @req.recipient != current_user
@req.try.redeem_code!(current_user)
end
end
......@@ -109,7 +111,15 @@ module Common::Requests
end
def track_activity
super @request.event, @request.event_attrs
super @req.event, @req.event_attrs
end
def create_notices
RequestNotice.create! @req
end
def dismiss_notices
RequestNotice.for_noticable(@req).dismiss_all unless @req.pending?
end
def approved?
......
......@@ -64,8 +64,8 @@ class Groups::MembershipRequestsController < Groups::BaseController
def create_join_request
if !params[:cancel]
req = RequestToJoinYou.create recipient: @group, created_by: current_user
alert_message req
@req = RequestToJoinYou.create recipient: @group, created_by: current_user
alert_message @req
end
redirect_to entity_url(@group)
end
......@@ -73,18 +73,20 @@ class Groups::MembershipRequestsController < Groups::BaseController
def create_destroy_request
@entity = Entity.find_by_name!(params[:entity])
if @entity.is_a? User
req = RequestToRemoveUser.create! user: @entity, group: @group, created_by: current_user
@req = RequestToRemoveUser.create! user: @entity, group: @group, created_by: current_user
membership = @group.memberships.find_by_user_id(@entity.id)
elsif @entity.is_a? Group
req = RequestToRemoveGroup.create! group: @entity, network: @group, created_by: current_user
@req = RequestToRemoveGroup.create! group: @entity, network: @group, created_by: current_user
membership = @group.federatings.find_by_group_id(@entity.id)
else
raise_error
end
success req
success @req
render :update do |page|
standard_update(page)
page.replace(dom_id(membership), partial: "groups/memberships/membership", locals: {membership: membership})
page.replace dom_id(membership),
partial: "groups/memberships/membership",
locals: {membership: membership}
end
end
......
......@@ -29,11 +29,11 @@ class Groups::RequestsController < Groups::BaseController
# RequestToCreateCouncil
#
def create
req = requested_class.create! recipient: @group,
@req = requested_class.create! recipient: @group,
requestable: @group,
created_by: current_user
success req
redirect_to request_path(req)
success @req
redirect_to request_path(@req)
end
protected
......
......@@ -2,7 +2,7 @@ require_relative 'comment_proxy_helper'
module RequestsHelper
def posts_for_request(request = @request)
def posts_for_request(request = @req)
[request_display_post(request), request_actions_post(request)].compact
end
......
......@@ -45,7 +45,7 @@ module Mailers::Request
# :group_type => @group.group_type.downcase,
# :user => @created_by.display_name)
# @request_link = url_for(:controller => 'me/requests/', :id => request.id)
# @req_link = url_for(:controller => 'me/requests/', :id => request.id)
# @body[:user] = @created_by
# @body[:group] = @group
end
......
......@@ -30,14 +30,8 @@ class Notice < ActiveRecord::Base
destroy_all(noticable_id: noticable.id, noticable_type: type_field(noticable))
end
#
# marks all notices associated with the noticable as dismissed.
#
def self.dismiss_all(noticable)
connection.execute(
"UPDATE notices SET dismissed = 1 WHERE noticable_type = '%s' AND noticable_id = %s" %
[type_field(noticable), noticable.id]
)
def self.dismiss_all
where(dismissed: false).update_all dismissed: true, dismissed_at: Time.now
end
##
......
#
# manage notices about requests
#
class RequestObserver < ActiveRecord::Observer
def after_create(request)
RequestNotice.create! request
end
def after_update(request)
# if the request is not pending, get rid of all related notices
unless request.pending?
RequestNotice.dismiss_all(request)
end
end
end
......@@ -46,7 +46,9 @@ class Request < ActiveRecord::Base
belongs_to :shared_discussion, class_name: 'Discussion', dependent: :destroy
belongs_to :private_discussion, class_name: 'Discussion', dependent: :destroy
has_one :notice, as: :noticable, dependent: :delete, class_name: 'RequestNotice'
has_many :notices, as: :noticable,
dependent: :delete_all,
class_name: 'RequestNotice'
# most requests are non-vote based. they just need a single 'approve' action
# to get approved
......
......@@ -5,7 +5,7 @@
-# display a fancy clickable sliding list
-#
-# requires:
-# items -- e.g. '@requests'
-# items -- e.g. '@reqs'
-# path_method -- e.g. 'request_path'
-#
-# optional:
......@@ -27,4 +27,4 @@
.p.last= local_assigns[:footer]
.right-panel
#sliding-item
= big_spinner
\ No newline at end of file
= big_spinner
-#
-# required local assigns:
-#
-# :items -- the name of a variable, like @requests, to list in the split panel.
-#
-# :items -- the name of a variable, like @reqs, to list in the split panel.
-# it must NOT be empty.
-# :left_panel --
-# :right_panel --
-# :right_url --
-#
-# :left_panel --
-# :right_panel --
-# :right_url --
-#
- items_name = items.sub('@','')
- item_name = items_name.singularize.to_sym
......
# standard_update(page)
# page.call 'activatePanelRow'
# page << "if ($('%s')) {" % left_id(@request)
# page.hide left_id(@request)
# page.hide right_id(@request)
# page << "if ($('%s')) {" % left_id(@req)
# page.hide left_id(@req)
# page.hide right_id(@req)
# page << "} else {"
# page.redirect_to requests_path
# page << "}"
- content_for :title do
%h2= I18n.t(@request.name, count: 1)
%h2= I18n.t(@req.name, count: 1)
.p.first
- if referrer =~ /\/requests[\/\?]/ # hackish
......
......@@ -4,6 +4,6 @@ page.replace('request-comments', :partial => 'common/requests/comments')
#
# request were once a split panel. the code looked like this:
# page.call 'activatePanelRow'
# page.hide left_id(@request)
# page.hide right_id(@request)
# page.hide left_id(@req)
# page.hide right_id(@req)
#
<%=I18n.t(:group_invite_email,
from_user: @user.display_name,
group: @group.display_name,
accept_invite_link: me_request_url(@request, code: @request.code),
accept_invite_link: me_request_url(@req, code: @req.code),
group_home_link: group_url(@group)
)
%>
......@@ -2,4 +2,4 @@
<%= :request_to_destroy_our_group_short.t(:user => @user.display_name, :group => @group.full_name, :group_type => @group.group_type) %>
<%= @request_link %>
<%= @req_link %>
......@@ -50,8 +50,7 @@ module Crabgrass
config.active_record.observers = :user_observer,
:relationship_observer, :request_to_destroy_our_group_observer,
:request_observer, "tracking/page_observer",
"tracking/post_observer", "tracking/wiki_observer",
"tracking/page_observer", "tracking/post_observer", "tracking/wiki_observer",
"tracking/user_participation_observer", "tracking/group_participation_observer"
config.session_store :cookie_store,
......
#
# Also see page_sharing_test.rb for PageNotice related tests
#
require File::dirname(__FILE__) + '/../test_helper'
class NoticeTest < ActiveSupport::TestCase
fixtures :users
test "request observers" do
req = nil
assert_difference 'RequestNotice.count' do
req = RequestToFriend.create! recipient: users(:yellow), created_by: users(:green)
end
assert_equal req, RequestNotice.for_noticable(req).find(:first).request
assert_difference 'RequestNotice.dismissed(true).count' do
req.set_state!('approved', users(:yellow))
end
end
end
......@@ -40,7 +40,8 @@ class RequestTest < ActiveSupport::TestCase
req = RequestToFriend.create! created_by: u1,
recipient: u2,
message: 'hi, lets be friends'
assert_equal req, req.notice.request
RequestNotice.create request: req
assert_equal req, req.notices.first.request
assert_difference 'Notice.count', -1 do
req.destroy
end
......
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