Commit 205af33c authored by azul's avatar azul
Browse files

track_actions in Pages::PostsController

replaces the Tracking::PostsObserver
parent fa205a9c
......@@ -261,6 +261,8 @@ module Common::Application::RescueErrors
if exception
alert_message :error, exception
end
log_exception(exception)
return if performed? # error in after_filter
render :update do |page|
hide_spinners(page)
update_alert_messages(page)
......
......@@ -42,7 +42,6 @@ module Common::Posts
protected
def render_posts_refresh(posts)
@post = Post.new # for the reply form
render :update do |page|
standard_update(page)
page.replace('posts', partial: 'common/posts/list', locals: {posts: posts})
......
......@@ -43,8 +43,8 @@ module Common::Tracking::Action
extend ActiveSupport::Concern
def track_action(event = nil, options = {})
event, options = nil, event if options.nil? && event.is_a?(Hash)
event ||= "#{action_string}_#{controller_name}"
event, options = nil, event if options.blank? && event.is_a?(Hash)
event ||= "#{action_string}_#{controller_name.singularize}"
event_options = options.reverse_merge current_user: current_user,
group: @group,
user: @user || current_user,
......
class Pages::PostsController < ApplicationController
include Common::Tracking::Action
include_controllers 'common/posts'
......@@ -12,6 +13,8 @@ class Pages::PostsController < ApplicationController
guard :may_ALIAS_post?
guard show: :may_show_page?
track_actions :create, :update, :destroy
# if something goes wrong with create, redirect to the page url.
rescue_render create: lambda { |controller| redirect_to(page_url(@page)) }
......@@ -64,5 +67,9 @@ class Pages::PostsController < ApplicationController
def post_params
params.require(:post).permit(:body)
end
def track_action
super item: @post
end
end
class Tracking::PostObserver < ActiveRecord::Observer
observe :post
def after_create(post)
return unless post.try(:discussion)
PageHistory::AddComment.create params_for_post(post)
end
def after_update(post)
return unless post.body_changed? && post.try(:discussion)
PageHistory::UpdateComment.create params_for_post(post)
end
def after_destroy(post)
return unless post.try(:discussion)
PageHistory::DestroyComment.create params_for_post(post)
end
protected
def params_for_post(post)
{ user: User.current, page: post.discussion.page, item: post }
end
end
......@@ -25,9 +25,10 @@ module PageExtension::Comments
end
def add_post(user, post_attributes)
Post.create! self, user, post_attributes
user.updated(self)
save
Post.create!(self, user, post_attributes).tap do
user.updated(self)
save
end
end
#
......
......@@ -5,10 +5,14 @@ module Tracking::Action
create_membership: ['GroupGainedUserActivity', 'UserJoinedGroupActivity'],
destroy_membership: ['GroupLostUserActivity', 'UserLeftGroupActivity'],
request_to_destroy_group: ['UserProposedToDestroyGroupActivity'],
create_friendship: ['FriendActivity']
create_friendship: ['FriendActivity'],
create_post: ['PageHistory::AddComment'],
update_post: ['PageHistory::UpdateComment'],
destroy_post: ['PageHistory::DestroyComment']
}
def self.track(event, options = {})
# Activities have keys to link the different activities for the same event
options[:key] ||= rand(Time.now.to_i)
EVENT_CREATES_ACTIVITIES[event].each do |class_name|
klass = class_name.constantize
......
......@@ -9,7 +9,10 @@
-#
- remote = local_assigns[:remote] || false
= form_for @post, url: posts_path, remote: remote,
- # display the current post if it has unsaved changes
- record = @post if @post.new_record? or @post.changed?
- record ||= Post.new
= form_for record, url: posts_path, remote: remote,
html: {onsubmit: show_spinner('post')} do |f|
= hidden_field_tag('in_reply_to_id', last_id) if last_id
......
......@@ -49,7 +49,7 @@ module Crabgrass
#config.active_record.whitelist_attributes = true
config.active_record.observers =
"tracking/page_observer", "tracking/post_observer", "tracking/wiki_observer",
"tracking/page_observer", "tracking/wiki_observer",
"tracking/user_participation_observer", "tracking/group_participation_observer"
config.session_store :cookie_store,
......
......@@ -13,6 +13,7 @@ class Pages::PostsControllerTest < ActionController::TestCase
body: body
}
assert_successfully_posted_to @page
assert_history_tracked 'AddComment', Post.last
end
def test_edit_post
......@@ -44,6 +45,13 @@ class Pages::PostsControllerTest < ActionController::TestCase
assert_equal @user, page.updated_by
end
def assert_history_tracked(subclass_string, item = nil)
assert history = @page.page_histories.last, "Missing history record"
assert_equal @user, history.user
assert_equal "PageHistory::#{subclass_string}", history.class.name
assert_equal item, history.item if item.present?
end
def body
"Test Message"
end
......
......@@ -200,35 +200,6 @@ class PageTrackingObserverTest < ActiveSupport::TestCase
assert_equal @pepe, PageHistory.last.user
end
def test_add_comment
Post.create! @page, @pepe, body: "Some nice comment"
assert_equal @last_count + 1, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::AddComment, PageHistory.last.class
assert_equal Post, PageHistory.last.item.class
assert_equal Post.last, PageHistory.last.item
end
def test_edit_comment
Post.create! @page, @pepe, body: "Some nice comment"
@post = Post.last
@post.update_attribute("body", "Some nice comment, congrats!")
assert_equal @last_count + 2, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::UpdateComment, PageHistory.last.class
assert_equal Post, PageHistory.last.item.class
assert_equal Post.last, PageHistory.last.item
end
def test_delete_comment
Post.create! @page, @pepe, body: "Some nice comment"
@post = Post.last
@post.destroy
assert_equal @last_count + 2, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::DestroyComment, PageHistory.last.class
end
def test_page_destroyed
# hmm we need to figure out another way to store this action
# to be notified since when the page record is destroyed all
......
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