Commit acae4e8c authored by azul's avatar azul
Browse files

track wiki updates in controller - no more observers!

parent 4436e29d
......@@ -49,7 +49,7 @@ module Common::Tracking::Action
group: @group,
user: @user || current_user,
page: @page
Tracking::Action.track event.to_sym, event_options
::Tracking::Action.track event.to_sym, event_options
end
module ClassMethods
......
......@@ -50,9 +50,9 @@ class Pages::ParticipationsController < Pages::SidebarsController
if params[:access] == 'remove'
destroy
else
@page.add(participation.entity, access: params[:access]).save!
@page.add(@part.entity, access: params[:access]).save!
render :update do |page|
page.replace_html dom_id(participation), partial: 'pages/participations/permission_row', locals: {participation: participation.reload}
page.replace_html dom_id(@part), partial: 'pages/participations/permission_row', locals: {participation: @part.reload}
end
end
end
......@@ -61,17 +61,17 @@ class Pages::ParticipationsController < Pages::SidebarsController
## however, since currently the existance of a participation means
## view access, then we need to destory them to remove access.
def destroy
if may_remove_participation?(participation)
if participation.is_a? UserParticipation
@page.remove(participation.user)
if may_remove_participation?(@part)
if @part.is_a? UserParticipation
@page.remove(@part.user)
else
@page.remove(participation.group)
@page.remove(@part.group)
end
else
raise ErrorMessage.new(:remove_access_error.t)
end
render :update do |page|
page.hide dom_id(@gpart || @upart)
page.hide dom_id(@part || @upart)
end
end
......@@ -80,16 +80,19 @@ class Pages::ParticipationsController < Pages::SidebarsController
end
# we always load the user participation for page sidebar controllers
# so use the group participation if it's present and fallback to user part.
# so use the participation loaded in fetch_data and fallback to user part.
def participation
@gpart || @upart
@part || @upart
end
# we only act upon group participations access. There's no staring or
# watching group participations.
def fetch_data
if params[:group] && params[:access]
@gpart = GroupParticipation.find(params[:id]) if params[:id]
return unless params[:access] && params[:id]
if params[:group]
@part = GroupParticipation.find(params[:id])
else
@part = UserParticipation.find(params[:id])
end
end
......
......@@ -6,10 +6,18 @@
#
class Wikis::WikisController < Wikis::BaseController
include Common::Tracking::Action
skip_before_filter :login_required, only: :show
before_filter :authorized?, only: :show
# cancel button pressed
before_filter :cancel, only: :update, if: :cancel?
# no save param present - do nothing
before_filter :noop, only: :update, if: :noop?
track_actions :update
guard show: :may_show_wiki?
helper 'wikis/sections'
layout false
......@@ -50,16 +58,11 @@ class Wikis::WikisController < Wikis::BaseController
#
def update
WikiLock.transaction do
if params[:cancel]
@wiki.release_my_lock!(@section, current_user)
elsif params[:force_save]
@wiki.break_lock!(@section)
end
if params[:save] || params[:force_save]
version = params[:save] ? params[:wiki][:version] : nil # disable version checked if force save
@wiki.update_section!(@section, current_user, version, params[:wiki][:body])
success
end
@wiki.break_lock!(@section) if params[:force_save]
# disable version checked if force save
version = params[:force_save] ? nil : params[:wiki][:version]
@wiki.update_section!(@section, current_user, version, params[:wiki][:body])
success
end
render template: 'wikis/wikis/show'
rescue Wiki::VersionExistsError, Wiki::LockedError => exc
......@@ -71,4 +74,27 @@ class Wikis::WikisController < Wikis::BaseController
render template: '/wikis/wikis/edit'
end
protected
# only track wiki updates on pages that have been saved
def track_action(event = nil, event_options = {})
super if @page && @wiki.previous_changes[:body]
end
def cancel
@wiki.release_my_lock!(@section, current_user)
render template: 'wikis/wikis/show'
end
def cancel?
!!params[:cancel]
end
def noop
render template: 'wikis/wikis/show'
end
def noop?
!params[:save] && !params[:force_save]
end
end
class Tracking::WikiObserver < ActiveRecord::Observer
observe :wiki
def after_update(wiki)
return unless wiki.body_changed?
page = Page.where(data_type: "Wiki", data_id: wiki).first
PageHistory::UpdatedContent.create user: User.current, page: page
end
end
......@@ -17,7 +17,8 @@ module Tracking::Action
update_participation: ['PageHistory::UpdateParticipation'],
update_group_access: ['PageHistory::GrantGroupAccess'],
update_user_access: ['PageHistory::GrantUserAccess'],
update_title: ['PageHistory::ChangeTitle']
update_title: ['PageHistory::ChangeTitle'],
update_wiki: ['PageHistory::UpdatedContent']
}
def self.track(event, options = {})
......
require_relative "../lib/crabgrass/info.rb"
info "LOAD FRAMEWORK"
require_relative 'boot'
......@@ -22,7 +23,7 @@ module Crabgrass
config.autoload_paths << "#{Rails.root}/lib"
config.autoload_paths << "#{Rails.root}/app/models"
config.autoload_paths += %w(activity assets associations discussion chat observers profile poll task tracking requests mailers notice).
config.autoload_paths += %w(activity assets associations discussion chat profile poll task tracking requests mailers notice).
collect { |dir| "#{Rails.root}/app/models/#{dir}" }
config.autoload_paths << "#{Rails.root}/app/permissions"
config.autoload_paths << "#{Rails.root}/app/sweepers"
......@@ -48,8 +49,6 @@ module Crabgrass
# We use strong parameters instead like rails4 does.
#config.active_record.whitelist_attributes = true
config.active_record.observers = "tracking/wiki_observer"
config.session_store :cookie_store,
:key => 'crabgrass_session'
......
require 'test_helper'
class Page::TrackingObserverTest < ActiveSupport::TestCase
def setup
Page.delete_all
@pepe = FactoryGirl.create(:user, login: "pepe")
@manu = FactoryGirl.create(:user, login: "manu")
@manu.grant_access!(public: :pester)
User.current = @pepe
@page = FactoryGirl.create(:page, owner: @pepe)
@last_count = @page.page_histories.count
end
def teardown
Page.delete_all
end
def test_save_page_without_modifications
@page.save!
assert_equal @last_count, @page.page_histories.count
end
def test_add_tag
true
end
def test_remove_tag
true
end
def test_add_attachment
true
end
def test_remove_attachment
true
end
def test_update_content
page = FactoryGirl.create(:wiki_page, data: Wiki.new(user: @pepe, body: ""))
# for some reason creating the page didn't create a GrantUserFullExist history
# item. Instead that would be created as soon as the wiki is updated (because
# that triggers a page.save! to update the page terms). The GrantUserFullAccess
# item would then be created *after* the UpdatedContent item, which breaks this
# test. Saving the page here makes everything work as expected again.
page.save!
wiki = Wiki.find page.data_id
previous_page_history = page.page_histories.count
wiki.update_section!(:document, @pepe, 1, "dsds")
assert_equal PageHistory::UpdatedContent, PageHistory.last.class
assert_equal previous_page_history + 1, page.page_histories.count
assert_equal @pepe, PageHistory.last.user
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
# hisotries are destroyed too
true
end
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