Commit 679c25c0 authored by azul's avatar azul
Browse files

track page updates in controller not observer

parent 205af33c
......@@ -7,6 +7,7 @@
class Pages::AttributesController < Pages::SidebarsController
before_filter :login_required
track_actions :update
guard update: :may_admin_page?
......@@ -26,6 +27,10 @@ class Pages::AttributesController < Pages::SidebarsController
protected
def track_action
super 'update_page'
end
def owner
return unless params[:owner]
if params[:owner] == current_user.name
......
......@@ -13,12 +13,14 @@
# (the two paths are the same... the action destinct.)
class Pages::CreateController < ApplicationController
include Common::Tracking::Action
before_filter :login_required, :authorization_required
before_filter :init_options, :set_owner, :catch_cancel
helper 'pages/share', 'pages/owner', 'pages/creation'
permissions :pages
guard :may_ACTION_page?
track_actions :create
# the page banner has links that the user cannot see when unauthorized, like membership.
# so, we must load the appropriate permissions from groups and me.
......@@ -78,6 +80,10 @@ class Pages::CreateController < ApplicationController
@owner = Group.find_by_name(owner_param) if owner_param.present?
end
def track_action
super :create_page, group: @owner
end
#
# helper methods
#
......
......@@ -3,6 +3,7 @@
# this controller.
#
class Pages::SidebarsController < ApplicationController
include Common::Tracking::Action
before_filter :fetch_page
before_filter :login_required
......
class Pages::TitleController < Pages::SidebarsController
guard :may_edit_page?
track_actions :update
# Return the edit title form. This is called by modalbox to load the popup contents.
def edit
......
......@@ -2,6 +2,7 @@ class Pages::TrashController < Pages::SidebarsController
guard :may_admin_page?
helper 'pages/trash'
track_actions :update
# loads popup
def edit
......@@ -18,6 +19,10 @@ class Pages::TrashController < Pages::SidebarsController
protected
def track_action
super "#{params[:type]}_page"
end
def redirect_url
if params[:type] == 'undelete'
page_url(@page)
......
class Tracking::PageObserver < ActiveRecord::Observer
observe :page
def after_create(page)
PageHistory::PageCreated.create(user: User.current, page: page)
end
def after_update(page)
params = { user: User.current, page: page}
PageHistory::ChangeTitle.create(params) if page.title_changed?
PageHistory::Deleted.create(params) if page.deleted?
PageHistory::MakePrivate.create(params) if page.marked_as_private?
PageHistory::MakePublic.create(params) if page.marked_as_public?
end
end
......@@ -81,6 +81,21 @@ class PageHistory::MakePrivate < PageHistory; end
class PageHistory::StartWatching < PageHistory; end
class PageHistory::StopWatching < PageHistory; end
# Factory class for the different page updates
class PageHistory::Update < PageHistory
def initialize(attrs = {}, options = {}, &block)
page = attrs[:page]
klass_for_update(page).new(attrs, options, &block)
end
def class_for_update(page)
return PageHistory::MakePrivate if page.marked_as_private?
return PageHistory::MakePublic if page.marked_as_public?
# return PageHistory::ChangeOwner if page.owner_id_changed?
end
end
class PageHistory::PageCreated < PageHistory
after_save :page_updated_at
end
......@@ -90,9 +105,13 @@ class PageHistory::ChangeTitle < PageHistory
after_save :page_updated_at
def add_details
self.details = {
from: self.page.title_was,
to: self.page.title
self.details = details_from_page
end
def details_from_page
{
from: page.previous_changes["title"].first,
to: page.title
}
end
end
......
......@@ -8,7 +8,13 @@ module Tracking::Action
create_friendship: ['FriendActivity'],
create_post: ['PageHistory::AddComment'],
update_post: ['PageHistory::UpdateComment'],
destroy_post: ['PageHistory::DestroyComment']
destroy_post: ['PageHistory::DestroyComment'],
update_page: ['PageHistory::Update'],
create_page: ['PageHistory::PageCreated'],
destroy_page: [],
delete_page: ['PageHistory::Deleted'],
undelete_page: [],
update_title: ['PageHistory::ChangeTitle']
}
def self.track(event, options = {})
......
......@@ -49,8 +49,9 @@ module Crabgrass
#config.active_record.whitelist_attributes = true
config.active_record.observers =
"tracking/page_observer", "tracking/wiki_observer",
"tracking/user_participation_observer", "tracking/group_participation_observer"
"tracking/wiki_observer",
"tracking/user_participation_observer",
"tracking/group_participation_observer"
config.session_store :cookie_store,
:key => 'crabgrass_session'
......
......@@ -19,6 +19,7 @@ class PageHistoryTest < ActiveSupport::TestCase
User.current = @user
@page = FactoryGirl.create(:page, created_by: @user)
PageHistory::PageCreated.create page: @page, user: @user
@site = FactoryGirl.create(:site, domain: "crabgrass.org",
title: "Crabgrass Social Network",
......@@ -37,9 +38,9 @@ class PageHistoryTest < ActiveSupport::TestCase
end
def test_validations
assert_raise ActiveRecord::RecordInvalid do PageHistory.create!(user: nil, page: nil) end
assert_raise ActiveRecord::RecordInvalid do PageHistory.create!(user: @user, page: nil) end
assert_raise ActiveRecord::RecordInvalid do PageHistory.create!(user: nil, page: @page) end
assert_invalid_attrs user: nil, page: nil
assert_invalid_attrs user: @user, page: nil
assert_invalid_attrs user: nil, page: @page
end
def test_associations
......@@ -121,6 +122,7 @@ class PageHistoryTest < ActiveSupport::TestCase
def test_change_title_saves_old_and_new_value
page = FactoryGirl.create(:page, title: "Bad title")
page.update_attribute :title, "Nice title"
Tracking::Action.track :update_title, user: @user, page: page
page_history = PageHistory::ChangeTitle.find :first, conditions: {page_id: page.id}
assert_equal "Bad title", page_history.details[:from]
assert_equal "Nice title", page_history.details[:to]
......@@ -212,18 +214,6 @@ class PageHistoryTest < ActiveSupport::TestCase
assert !PageHistory.recipients_for_single_notification(PageHistory.last).include?(user_c)
end
def test_destroy_page_leave_no_history
post = Post.create! @page, @user, body: "content"
@page.destroy
assert_equal 0, PageHistory.count
end
def test_delete_page_leaves_history
post = Post.create! @page, @user, body: "content"
@page.delete
assert_equal 1, PageHistory::Deleted.count
end
def test_send_pending_notifications
user_a = FactoryGirl.create(:user, receive_notifications: "Single")
User.current = user_a
......@@ -247,6 +237,12 @@ class PageHistoryTest < ActiveSupport::TestCase
private
def assert_invalid_attrs(attrs)
history = PageHistory.new attrs
assert !history.valid?,
"These attributes should be invalid for a Page History: #{attrs.inspect}"
end
def assert_change_updated_at(page, page_history)
page.reload
page_history.reload
......
......@@ -21,14 +21,6 @@ class PageTrackingObserverTest < ActiveSupport::TestCase
assert_equal @last_count, @page.page_histories.count
end
def test_change_page_title
@page.title = "Other title"
@page.save!
assert_equal @last_count + 1, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::ChangeTitle, PageHistory.last.class
end
def test_add_star
@upart = @page.add(@pepe, star: true ).save!
@page.reload
......@@ -46,29 +38,6 @@ class PageTrackingObserverTest < ActiveSupport::TestCase
assert_equal PageHistory::RemoveStar, PageHistory.last.class
end
def test_mark_as_public
@page.public = true
@page.save
assert_equal @last_count + 1, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::MakePublic, PageHistory.last.class
end
def test_mark_as_private
@page.public = false
@page.save
assert_equal @last_count + 1, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::MakePrivate, PageHistory.last.class
end
def test_page_deleted
@page.delete
assert_equal @last_count + 1, @page.page_histories.count
assert_equal @pepe, PageHistory.last.user
assert_equal PageHistory::Deleted, PageHistory.last.class
end
def test_add_tag
true
end
......@@ -85,13 +54,6 @@ class PageTrackingObserverTest < ActiveSupport::TestCase
true
end
def test_create_page
page = FactoryGirl.create(:page, owner: @pepe)
page.reload
assert_equal PageHistory::PageCreated, page.page_histories.first.class
assert_equal PageHistory::GrantUserFullAccess, page.page_histories.last.class
end
def test_start_watching
@upart = @page.add(@pepe, watch: true).save!
@page.reload
......
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