Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add exclusion-option to locale filter

  • Loading branch information...
commit c7d7791559da9882c275a5cf88e58afc35263e7f 1 parent 7bf21a8
Moritz Behr authored
20 Gemfile.lock
View
@@ -1,28 +1,28 @@
PATH
remote: .
specs:
- routing-filter (0.2.4)
+ routing-filter (0.3.0)
actionpack
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
- actionpack (3.0.11)
- activemodel (= 3.0.11)
- activesupport (= 3.0.11)
+ actionpack (3.0.12)
+ activemodel (= 3.0.12)
+ activesupport (= 3.0.12)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.5.0)
- rack (~> 1.2.1)
+ rack (~> 1.2.5)
rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
- activemodel (3.0.11)
- activesupport (= 3.0.11)
+ activemodel (3.0.12)
+ activesupport (= 3.0.12)
builder (~> 2.1.2)
i18n (~> 0.5.0)
- activesupport (3.0.11)
+ activesupport (3.0.12)
appraisal (0.4.0)
bundler
rake
@@ -35,7 +35,7 @@ GEM
linecache (0.43)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
- rack (1.2.4)
+ rack (1.2.5)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
@@ -57,7 +57,7 @@ GEM
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
test_declarative (0.0.5)
- tzinfo (0.3.31)
+ tzinfo (0.3.32)
PLATFORMS
ruby
39 lib/routing_filter/filters/locale.rb
View
@@ -43,6 +43,14 @@ def locales_pattern
end
end
+
+ attr_reader :exclude
+ def initialize(*args)
+ super
+ @exclude = options[:exclude]
+ end
+
+
def around_recognize(path, env, &block)
locale = extract_segment!(self.class.locales_pattern, path) # remove the locale from the beginning of the path
yield.tap do |params| # invoke the given block (calls more filters and finally routing)
@@ -59,23 +67,32 @@ def around_generate(*args, &block)
args << params
- yield.tap do |result|
- prepend_segment!(result, locale) if prepend_locale?(locale)
+ yield.tap do |result|
+ url = result.is_a?(Array) ? result.first : result
+ prepend_segment!(result, locale) if prepend_locale?(locale) && !excluded?(url)
end
end
protected
+ def valid_locale?(locale)
+ locale && self.class.locales.include?(locale.to_sym)
+ end
- def valid_locale?(locale)
- locale && self.class.locales.include?(locale.to_sym)
- end
-
- def default_locale?(locale)
- locale && locale.to_sym == I18n.default_locale.to_sym
- end
+ def default_locale?(locale)
+ locale && locale.to_sym == I18n.default_locale.to_sym
+ end
- def prepend_locale?(locale)
- locale && (self.class.include_default_locale? || !default_locale?(locale))
+ def prepend_locale?(locale)
+ locale && (self.class.include_default_locale? || !default_locale?(locale))
+ end
+
+ def excluded?(url)
+ case exclude
+ when Regexp
+ url =~ exclude
+ when Proc
+ exclude.call(url)
end
+ end
end
end
7 test/filters/locale_test.rb
View
@@ -14,9 +14,10 @@ def setup
@show_params = { :controller => 'some', :action => 'show', :id => '1' }
@routes = draw_routes do
- filter :locale
+ filter :locale, :exclude => /^\/themes/
match 'products/:id', :to => 'some#show'
match '/', :to => 'some#index'
+ match '/themes/products/new', :to => 'some#new'
end
end
@@ -66,4 +67,8 @@ def setup
RoutingFilter::Locale.include_default_locale = false
assert_generates '/products/1', routes.generate(show_params.merge(:locale => I18n.default_locale))
end
+
+ test 'excludes with custom regexp' do
+ assert_generates '/themes/products/new', routes.generate(:controller => 'some', :action => 'new')
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.