Skip to content

Commit

Permalink
add exclusion-option to locale filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Behr committed Mar 8, 2012
1 parent 7bf21a8 commit c7d7791
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
39 changes: 28 additions & 11 deletions lib/routing_filter/filters/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 changes: 6 additions & 1 deletion test/filters/locale_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit c7d7791

Please sign in to comment.