Commit f490ed30 authored by azul's avatar azul
Browse files

replace membership observer with track_activity

parent e6c07240
......@@ -10,6 +10,10 @@ class Groups::MembershipsController < Groups::BaseController
destroy: :may_destroy_membership?,
create: :may_create_membership?
after_filter :track_activity,
only: [:create, :destroy],
unless: :federation_view?
#
# list all the memberships
#
......@@ -25,7 +29,7 @@ class Groups::MembershipsController < Groups::BaseController
# immediately destroy a membership
#
def destroy
@membership.group.remove_user! @membership.user # memberships must be destroyed via group.remove_user!
@group.remove_user! @user # memberships must be destroyed via group.remove_user!
render :update do |page|
page.hide dom_id(@membership)
end
......@@ -54,6 +58,7 @@ class Groups::MembershipsController < Groups::BaseController
@membership = @group.federatings.find(params[:id])
else
@membership = @group.memberships.find(params[:id])
@user = @membership.user
end
elsif action?(:create)
if params[:user_name]
......
......@@ -38,7 +38,9 @@ class Activity < ActiveRecord::Base
belongs_to :item, polymorphic: true # the "item" is the thing that is acted upon.
EVENT_ACTIVITY_CLASSES = {
create_group: ['GroupCreatedActivity', 'UserCreatedGroupActivity']
create_group: ['GroupCreatedActivity', 'UserCreatedGroupActivity'],
create_membership: ['GroupGainedUserActivity', 'UserJoinedGroupActivity'],
destroy_membership: ['GroupLostUserActivity', 'UserLeftGroupActivity']
}
def self.track(event, options = {})
......
......@@ -6,6 +6,10 @@ class GroupCreatedActivity < Activity
alias_attr :group, :subject
alias_attr :user, :item
# when build via Activity.track from the controller, the user who created
# the group will be current_user
alias_method :current_user=, :user=
def description(view=nil)
I18n.t(:activity_group_created,
user: user_span(:user),
......
......@@ -8,6 +8,10 @@ class UserCreatedGroupActivity < Activity
alias_attr :user, :subject
alias_attr :group, :item
# when build via Activity.track from the controller, the user who created
# the group will be current_user
alias_method :current_user=, :user=
def description(view=nil)
I18n.t(:activity_group_created,
user: user_span(:user),
......
class MembershipObserver < ActiveRecord::Observer
def after_create(membership)
key = rand(Time.now.to_i)
return if membership.group_id == Site.current.try(:network_id)
UserJoinedGroupActivity.create!(user: membership.user, group: membership.group, key: key)
GroupGainedUserActivity.create!(user: membership.user, group: membership.group, key: key)
end
def after_destroy(membership)
unless membership.skip_destroy_notification
key = rand(Time.now.to_i)
UserLeftGroupActivity.create!(user: membership.user, group: membership.group, key: key)
GroupLostUserActivity.create!(user: membership.user, group: membership.group, key: key)
end
end
end
......@@ -48,7 +48,7 @@ module Crabgrass
# We use strong parameters instead like rails4 does.
#config.active_record.whitelist_attributes = true
config.active_record.observers = :user_observer, :membership_observer,
config.active_record.observers = :user_observer,
:relationship_observer, :post_observer, :request_to_destroy_our_group_observer,
:request_observer, :page_observer, "tracking/page_observer",
"tracking/post_observer", "tracking/wiki_observer",
......
......@@ -33,7 +33,7 @@ class ActivityTest < ActiveSupport::TestCase
def test_group_created
group = FactoryGirl.create :group, created_by: @ann
Activity.track :create_group, group: group, user: @ann
Activity.track :create_group, group: group, current_user: @ann
act = GroupCreatedActivity.find(:last)
assert_activity_for_user_group(act, @ann, group)
......@@ -45,9 +45,10 @@ class ActivityTest < ActiveSupport::TestCase
assert_in_description(act, @ann)
end
def test_membership
def test_create_membership
ruth = FactoryGirl.create(:user)
@group.add_user!(ruth)
Activity.track :create_membership, group: @group, user: ruth
assert_nil UserJoinedGroupActivity.for_all(@ann).find_by_subject_id(ruth.id),
"The new peers don't get UserJoinedGroupActivities."
......@@ -63,21 +64,24 @@ class ActivityTest < ActiveSupport::TestCase
# users own activity should always show up:
act = UserJoinedGroupActivity.for_all(ruth).last
assert_equal @group.id, act.group.id
end
##
## Remove the user
##
@group.remove_user!(ruth)
##
## Remove the user
##
def test_destroy_membership
@group.remove_user!(@joe)
Activity.track :destroy_membership, group: @group, user: @joe
act = GroupLostUserActivity.for_all(@ann).last
assert_activity_for_user_group(act, ruth, @group)
assert_activity_for_user_group(act, @joe, @group)
act = GroupLostUserActivity.for_group(@group, @ann).last
assert_activity_for_user_group(act, ruth, @group)
assert_activity_for_user_group(act, @joe, @group)
act = UserLeftGroupActivity.for_all(ruth).last
assert_activity_for_user_group(act, ruth, @group)
act = UserLeftGroupActivity.for_all(@joe).last
assert_activity_for_user_group(act, @joe, @group)
end
def test_deleted_subject
......@@ -99,6 +103,7 @@ class ActivityTest < ActiveSupport::TestCase
@joe.add_contact!(@ann, :friend)
@joe.send_message_to!(@ann, "hi @ann")
new_group.add_user!(@joe)
Activity.track :create_membership, group: new_group, user: @joe
friend_act = FriendActivity.find_by_subject_id(@joe.id)
user_joined_act = UserJoinedGroupActivity.find_by_subject_id(@joe.id)
......
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