factors out acts_as_watchable/routes matching completly into the class

This was done to improve testibility but it also cleans up the code
pull/58/head
Jens Ulferts 12 years ago
parent 0ff995f800
commit e225aa6032
  1. 4
      config/routes.rb
  2. 13
      lib/plugins/acts_as_watchable/lib/acts_as_watchable/routes.rb
  3. 50
      spec/lib/acts_as_watchable/lib/acts_as_watchable/routes_spec.rb
  4. 53
      spec/routing/watchers_spec.rb
  5. 22
      test/integration/routing_test.rb

@ -38,9 +38,7 @@ OpenProject::Application.routes.draw do
# generic route for adding/removing watchers.
# Models declared as acts_as_watchable will be automatically added to
# OpenProject::Acts::Watchable::Routes.watched
scope ':object_type/:object_id', :constraints => lambda { |req|
OpenProject::Acts::Watchable::Routes.watched?(req.path_parameters[:object_type]) &&
/\d+/.match(req.path_parameters[:object_id]) } do
scope ':object_type/:object_id', :constraints => OpenProject::Acts::Watchable::Routes do
resources :watchers, :only => [:new, :create]
match '/watch' => 'watchers#watch', :via => :post

@ -4,8 +4,11 @@ module OpenProject
module Routes
mattr_accessor :models
def self.watched?(object)
@watchregexp.present? && @watchregexp.match(object).present?
def self.matches?(request)
params = request.path_parameters
watched?(params[:object_type]) &&
/\d+/.match(params[:object_id])
end
def self.add_watched(watched)
@ -15,6 +18,12 @@ module OpenProject
@watchregexp = Regexp.new(self.models.join("|"))
end
private
def self.watched?(object)
@watchregexp.present? && @watchregexp.match(object).present?
end
end
end
end

@ -0,0 +1,50 @@
require 'spec_helper'
describe OpenProject::Acts::Watchable::Routes do
let(:request) { Struct.new(:type, :id) do
def path_parameters
{ :object_id => id,
:object_type => type }
end
end.new(type, id) }
describe "matches?" do
shared_examples_for "watched model" do
describe "for a valid id string" do
let(:id) { "1" }
it "should be true" do
OpenProject::Acts::Watchable::Routes.matches?(request).should be_true
end
end
describe "for an invalid id string" do
let(:id) { "schmu" }
it "should not be false" do
OpenProject::Acts::Watchable::Routes.matches?(request).should be_false
end
end
end
['issues', 'news', 'news', 'boards', 'messages', 'wikis', 'wiki_pages'].each do |type|
describe "routing #{type} watches" do
let(:type) { type }
it_should_behave_like "watched model"
end
end
describe "for a non watched model" do
let(:type) { "schmu" }
let(:id) { "4" }
it "should be false" do
OpenProject::Acts::Watchable::Routes.matches?(request).should be_false
end
end
end
end

@ -0,0 +1,53 @@
require 'spec_helper'
describe WatchersController do
shared_examples_for "watched model" do
before do
OpenProject::Acts::Watchable::Routes.should_receive(:matches?).and_return(true)
end
it "should connect GET /:object_type/:object_id/watch to watchers#watch" do
post("/#{type}/1/watch").should route_to( :controller => 'watchers',
:action => 'watch',
:object_type => type,
:object_id => '1' )
end
it "should connect DELETE /:object_type/:id/unwatch to watchers#unwatch" do
delete("/#{type}/1/unwatch").should route_to( :controller => 'watchers',
:action => 'unwatch',
:object_type => type,
:object_id => '1' )
end
it "should connect GET /:object_type/:id/watchers/new to watchers#new" do
get("/#{type}/1/watchers/new").should route_to( :controller => 'watchers',
:action => 'new',
:object_type => type,
:object_id => '1' )
end
it "should connect POST /:object_type/:object_id/watchers to watchers#create" do
post("/#{type}/1/watch").should route_to( :controller => 'watchers',
:action => 'watch',
:object_type => type,
:object_id => '1' )
end
end
['issues', 'news', 'news', 'boards', 'messages', 'wikis', 'wiki_pages'].each do |type|
describe "routing #{type} watches" do
let(:type) { type }
it_should_behave_like "watched model"
end
end
it "should connect DELETE watchers/:id to watchers#destroy" do
delete("/watchers/1").should route_to( :controller => 'watchers',
:action => 'destroy',
:id => '1' )
end
end

@ -263,28 +263,6 @@ class RoutingTest < ActionDispatch::IntegrationTest
end
end
context "watches" do
['issues', 'messages', 'boards', 'wikis', 'wiki_pages', 'news'].each do |type|
should route(:post, "/#{type}/1/watch").to( :controller => 'watchers',
:action => 'watch',
:object_type => type,
:object_id => '1' )
should route(:delete, "/#{type}/1/unwatch").to( :controller => 'watchers',
:action => 'unwatch',
:object_type => type,
:object_id => '1' )
should route(:get, "/#{type}/1/watchers/new").to( :controller => 'watchers',
:action => 'new',
:object_type => type,
:object_id => '1' )
end
should route(:delete, "/watchers/1").to( :controller => 'watchers',
:action => 'destroy',
:id => '1' )
end
context "enumerations" do
context "within admin" do

Loading…
Cancel
Save