routing_filter_locale_unless_api 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,17 @@
1
- pkg/*
1
+ *.rbc
2
+ *.sw?
2
3
  *.gem
4
+ .DS_Store
3
5
  .bundle
6
+ .rvmrc
7
+ .yardoc
8
+ Gemfile.lock
9
+ bin
10
+ coverage*
11
+ doc
12
+ gemfiles
13
+ pkg
14
+ rdoc
15
+ rerun.txt
16
+ tags
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.1
4
+ - 1.9.2
5
+ - ruby-head
6
+ - ree
data/.yardopts ADDED
@@ -0,0 +1,8 @@
1
+ --no-document
2
+ --no-private
3
+ --exclude features
4
+ --title 'Routing Filter\'s Locale Unless API Filter Documentation'
5
+ -
6
+ README.md
7
+ Changelog.md
8
+ License.txt
data/Appraisals ADDED
@@ -0,0 +1,10 @@
1
+ appraise "3.0" do
2
+ gem "i18n", "~> 0.5.0"
3
+ gem "actionpack", "~> 3.0"
4
+ end
5
+
6
+ # Not yet supported
7
+ # appraise "3.1.0.rc4" do
8
+ # gem "i18n", "~> 0.6.0"
9
+ # gem "actionpack", "~> 3.1.0.rc4"
10
+ # end
data/Changelog.md ADDED
@@ -0,0 +1,16 @@
1
+ ### Dev
2
+
3
+ [full changelog](https://github.com/ZenCocoon/routing_filter_locale_unless_api/compare/v0.1.0...master)
4
+
5
+ ### 0.1.0 / 2011-07-04
6
+
7
+ [full changelog](https://github.com/ZenCocoon/routing_filter_locale_unless_api/compare/v0.0.1...v0.1.0)
8
+
9
+ * Enhancements
10
+ * Isolate the LocaleUnlessApi filter under it's own namespace
11
+ * Add [Appraisal](https://github.com/thoughtbot/appraisal) support to test with all ActionPack supported version.
12
+ * Move tests to RSpec.
13
+
14
+ ### 0.0.1 / 2011-03-01
15
+
16
+ * Initial release.
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in routing_filter_locale_unless_api.gemspec
4
3
  gemspec
4
+
5
+ ### MRI 1.8 and 1.9
6
+ gem "rb-fsevent", "~> 0.3.9", :platforms => [:mri_18, :mri_19]
7
+ gem "ruby-prof", "~> 0.9.2", :platforms => [:mri_18, :mri_19]
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(/^spec\/(.*)_spec.rb/)
3
+ watch(/^lib\/(.*)\.rb/) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch(/^spec\/spec_helper.rb/) { "spec" }
5
+ end
6
+
7
+ guard 'cucumber' do
8
+ watch(%r{^features/.+\.feature$})
9
+ watch(%r{^features/support/.+$}) { 'features' }
10
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
11
+ end
data/License.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2011 Sébastien Grosjean
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Routing Filter, Locale Unless API [![build status](http://travis-ci.org/ZenCocoon/routing_filter_locale_unless_api.png)](http://travis-ci.org/ZenCocoon/routing_filter_locale_unless_api)
2
+
3
+ RoutingFilterLocaleUnlessAPI is a extension filter to Sven Fuchs’s routing-filter
4
+
5
+ http://github.com/svenfuchs/routing-filter
6
+
7
+ RoutingFilterLocaleUnlessAPI filter extracts segments matching `/:locale` from
8
+ the beginning of the recognized path and exposes the locale parameter
9
+ as `params[:locale]`. When a path is generated the filter adds the segments
10
+ to the path accordingly if the locale parameter is passed to the url helper.
11
+
12
+ It will not add the locale parameter as a url fragment if:
13
+
14
+ * the format is `XML` or `JSON` (or any defined API format)
15
+ * with the root url and default locale
16
+
17
+ ## Documentation
18
+
19
+ The [RDoc](http://rubydoc.info/gems/routing_filter_locale_unless_api/0.1.0/frames) provides
20
+ additional information for contributors and/or extenders.
21
+
22
+ All of the documentation is open source and a work in progress. If you find it
23
+ lacking or confusing, you can help improve it by submitting requests and
24
+ patches to the [routing_filter_locale_unless_api issue
25
+ tracker](https://github.com/ZenCocoon/routing_filter_locale_unless_api/issues).
26
+
27
+ ## Requirements
28
+
29
+ actionpack ~> 3.0.9
30
+ i18n >= 0.5.0
31
+ routing-filter ~> 0.2.3
32
+
33
+ ## Installation
34
+
35
+ in your `Gemfile`
36
+
37
+ gem 'routing_filter_locale_unless_api'
38
+
39
+ then in `config/routes.rb`
40
+
41
+ Rails.application.routes.draw do
42
+ filter :locale_unless_api
43
+ end
44
+
45
+ ## Usage
46
+
47
+ incoming url: /fr/products
48
+ filtered url: /products
49
+ params: params[:locale] = 'fr'
50
+
51
+ products_path(:locale => 'fr')
52
+ generated_path: /fr/products
53
+
54
+ products_path(:locale => 'fr', :format => 'xml')
55
+ generated_path: /products.xml
56
+
57
+ root_path(:locale => 'en')
58
+ generated_path: /
59
+
60
+ root_path(:locale => 'fr')
61
+ generated_path: /fr
62
+
63
+ More example visible in the tests cases
64
+
65
+ ## Configuration
66
+
67
+ You can customize the API formats with
68
+
69
+ Rails.application.routes.draw do
70
+ filter :locale_unless_api, :api_formats => %w( xml json xls )
71
+ end
72
+
73
+ ## Also see
74
+
75
+ * [http://github.com/svenfuchs/routing-filter](http://github.com/svenfuchs/routing-filter)
76
+
77
+ ## License
78
+
79
+ MIT License. Copyright 2011 Sébastien Grosjean, sponsored by [BookingSync, Vacation Rental's Booking Calendar Software](http://www.bookingsync.com)
data/Rakefile CHANGED
@@ -1,2 +1,31 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
1
+ require 'bundler/setup'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+ # require 'cucumber/rake/task'
5
+ require 'appraisal'
6
+
7
+ desc "Default: Run all specs under all ActionPack supported versions."
8
+ task :default => ["appraisal:install"] do
9
+ exec('rake appraisal spec')
10
+ end
11
+
12
+ desc "Run all specs"
13
+ RSpec::Core::RakeTask.new(:spec) do |t|
14
+ t.rspec_opts = %w[--color]
15
+ end
16
+
17
+ # Cucumber::Rake::Task.new(:cucumber)
18
+
19
+ task :clobber do
20
+ rm_rf 'pkg'
21
+ rm_rf 'tmp'
22
+ rm_rf 'coverage'
23
+ rm 'coverage.data'
24
+ end
25
+
26
+ namespace :clobber do
27
+ desc "remove generated rbc files"
28
+ task :rbc do
29
+ Dir['**/*.rbc'].each {|f| File.delete(f)}
30
+ end
31
+ end
@@ -0,0 +1,126 @@
1
+ require 'i18n'
2
+
3
+ module RoutingFilterLocaleUnlessAPI
4
+ module Filter
5
+ class LocaleUnlessApi < RoutingFilter::Filter
6
+ @@api_formats = %w( xml json )
7
+ cattr_writer :api_formats
8
+
9
+ # Class Methods
10
+ class << self
11
+ # Returns +true+ if the given format is considered as an API, otherwise +false+.
12
+ #
13
+ # @param [String,Symbol] format
14
+ #
15
+ # @return [Boolean]
16
+ def api_format?(format)
17
+ format && @@api_formats.include?(format.to_s.downcase)
18
+ end
19
+
20
+ # Returns all available locales
21
+ #
22
+ # @return [Array]
23
+ def locales
24
+ @@locales ||= I18n.available_locales.map(&:to_sym)
25
+ end
26
+
27
+ # Set locales.
28
+ #
29
+ # @param [Array] locales
30
+ #
31
+ # @return [Array]
32
+ # locales as an Array of Symbols
33
+ def locales=(locales)
34
+ @@locales = locales.map(&:to_sym)
35
+ end
36
+
37
+ # Define the locales pattern.
38
+ #
39
+ # @return [Regexp]
40
+ def locales_pattern
41
+ @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
42
+ end
43
+ end
44
+
45
+ # Extract locale segments from the URL.
46
+ #
47
+ # @param [String] path
48
+ # Current path to recognize, might have been modified by previous filters
49
+ #
50
+ # @param [Hash] env
51
+ # Current environment
52
+ #
53
+ # @yield
54
+ # Additional filters and finally routing
55
+ def around_recognize(path, env, &block)
56
+ locale = extract_segment!(self.class.locales_pattern, path) # remove the locale from the beginning of the path
57
+ yield.tap do |params| # invoke the given block (calls more filters and finally routing)
58
+ params[:locale] = locale if locale # set recognized locale to the resulting params hash
59
+ end
60
+ end
61
+
62
+ # Add locale segments to the generated URL if required.
63
+ #
64
+ # @param *args
65
+ # Resources and Params given for the route generation
66
+ #
67
+ # @yield
68
+ # Additional filters
69
+ def around_generate(*args, &block)
70
+ params = args.extract_options! # this is because we might get a call like forum_topics_path(forum, topic, :locale => :en)
71
+
72
+ format = params[:format] # save the current format
73
+ locale = params.delete(:locale) # extract the passed :locale option
74
+ locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
75
+ locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
76
+
77
+ args << params
78
+
79
+ yield.tap do |result|
80
+ prepend_segment!(result, locale) if prepend_locale?(result, locale, format)
81
+ end
82
+ end
83
+
84
+ protected
85
+
86
+ # Returns true if the locale is valid.
87
+ #
88
+ # @param [Symbol,String] locale
89
+ #
90
+ # @return [Boolean]
91
+ def valid_locale?(locale)
92
+ locale && self.class.locales.include?(locale.to_sym)
93
+ end
94
+
95
+ # Returns true if the locale is the default one.
96
+ #
97
+ # @param [Symbol,String] locale
98
+ #
99
+ # @return [Boolean]
100
+ def default_locale?(locale)
101
+ locale && locale.to_sym == I18n.default_locale.to_sym
102
+ end
103
+
104
+ # Returns true if root path.
105
+ #
106
+ # @param [String] path
107
+ #
108
+ # @return [Boolean]
109
+ def root_path?(path)
110
+ path == '/'
111
+ end
112
+
113
+ # Returns true if it should prepend the locale
114
+ #
115
+ # @param [String] path
116
+ # @param [Symbol,String] locale
117
+ # @param [String,Symbol] format
118
+ #
119
+ # @return [Boolean]
120
+ def prepend_locale?(path, locale, format)
121
+ locale && !self.class.api_format?(format) && !(root_path?(path) && default_locale?(locale))
122
+ end
123
+
124
+ end
125
+ end
126
+ end
@@ -1,3 +1,6 @@
1
- module RoutingFilterLocaleUnlessApi
2
- VERSION = "0.0.1"
1
+ module RoutingFilterLocaleUnlessAPI
2
+ # @private
3
+ module Version
4
+ STRING = "0.1.0"
5
+ end
3
6
  end
@@ -1,3 +1,61 @@
1
- module RoutingFilterLocaleUnlessApi
2
- require 'routing_filter_locale_unless_api/locale_unless_api'
3
- end
1
+ require 'routing_filter'
2
+ require 'action_controller'
3
+ require 'routing_filter_locale_unless_api/filter/locale_unless_api'
4
+ require 'routing_filter_locale_unless_api/version'
5
+
6
+ # RoutingFilterLocaleUnlessAPI is a extension filter to Sven Fuchs’s routing-filter
7
+ #
8
+ # http://github.com/svenfuchs/routing-filter
9
+ #
10
+ # RoutingFilter::LocaleUnlessAPI filter extracts segments matching +/:locale+ from
11
+ # the beginning of the recognized path and exposes the locale parameter
12
+ # as +params[:locale]+. When a path is generated the filter adds the segments
13
+ # to the path accordingly if the locale parameter is passed to the url helper.
14
+ #
15
+ # It will not add the locale parameter as a url fragment if:
16
+ #
17
+ # * the format is +XML+ or +JSON+ (or any defined API format)
18
+ # * with the root url and default locale
19
+ #
20
+ # === Install:
21
+ #
22
+ # in your +Gemfile+
23
+ #
24
+ # gem 'routing_filter_locale_unless_api'
25
+ #
26
+ # then in +config/routes.rb+
27
+ #
28
+ # Rails.application.routes.draw do
29
+ # filter :locale_unless_api
30
+ # end
31
+ #
32
+ # @example
33
+ # incoming url: /fr/products
34
+ # filtered url: /products
35
+ # params: params[:locale] = 'fr'
36
+ #
37
+ # products_path(:locale => 'fr')
38
+ # generated_path: /fr/products
39
+ #
40
+ # products_path(:locale => 'fr', :format => 'xml')
41
+ # generated_path: /products.xml
42
+ #
43
+ # root_path(:locale => 'en')
44
+ # generated_path: /
45
+ #
46
+ # root_path(:locale => 'fr')
47
+ # generated_path: /fr
48
+ #
49
+ # === Configuration
50
+ #
51
+ # You can customize the API formats with
52
+ #
53
+ # Rails.application.routes.draw do
54
+ # filter :locale_unless_api, :api_formats => %w( xml json xls )
55
+ # end
56
+ module RoutingFilterLocaleUnlessAPI
57
+ end
58
+
59
+ module RoutingFilter
60
+ include RoutingFilterLocaleUnlessAPI::Filter
61
+ end
@@ -1,24 +1,33 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "routing_filter_locale_unless_api/version"
3
+ require 'routing_filter_locale_unless_api/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "routing_filter_locale_unless_api"
7
- s.version = RoutingFilterLocaleUnlessApi::VERSION
7
+ s.version = RoutingFilterLocaleUnlessAPI::Version::STRING
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Sébastien Grosjean"]
10
- s.email = ["public@zencocoon.com"]
9
+ s.authors = "Sébastien Grosjean"
10
+ s.email = "public@zencocoon.com"
11
11
  s.homepage = "https://github.com/ZenCocoon/routing_filter_locale_unless_api"
12
12
  s.summary = %q{routing filter that add locale unless XML or JSON, or root_url with default locale}
13
13
  s.description = %q{A routing-filter additional filter that add locale unless XML or JSON, or root_url with default locale}
14
14
 
15
- s.rubyforge_project = "[none]"
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
17
+ s.extra_rdoc_files = "README.md"
18
+ s.rdoc_options = "--charset=UTF-8"
19
+ s.require_path = "lib"
16
20
 
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
21
-
21
+ s.add_dependency "actionpack", "~> 3.0.9"
22
+ s.add_dependency "i18n", ">= 0.5.0"
22
23
  s.add_dependency "routing-filter", "~> 0.2.3"
23
- s.add_development_dependency 'test_declarative'
24
+
25
+ s.add_development_dependency "rake", "~> 0.9"
26
+ s.add_development_dependency 'rspec', '~> 2.6.0'
27
+ s.add_development_dependency "cucumber", "1.0.0"
28
+ s.add_development_dependency "guard-rspec", "0.1.9"
29
+ s.add_development_dependency 'guard-cucumber', "~> 0.5.1"
30
+ s.add_development_dependency "growl", "1.0.3"
31
+ s.add_development_dependency 'yard', "~> 0.7.2"
32
+ s.add_development_dependency "appraisal", '~> 0.3.6'
24
33
  end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+ module RoutingFilterLocaleUnlessAPI::Filter
4
+ describe LocaleUnlessApi do
5
+ before do
6
+ I18n.locale = nil
7
+ I18n.default_locale = :en
8
+ I18n.available_locales = %w(fr en)
9
+
10
+ RoutingFilter::LocaleUnlessApi.api_formats = %w( xml json )
11
+ end
12
+
13
+ let(:root) { { :controller => 'some', :action => 'index' } }
14
+ let(:params) { { :controller => 'some', :action => 'show', :id => '1' } }
15
+ let(:routes) do
16
+ draw_routes do
17
+ filter :locale_unless_api
18
+ match 'products/:id', :to => 'some#show'
19
+ root :to => 'some#index'
20
+ end
21
+ end
22
+
23
+ context "path recognition" do
24
+ it 'should recognize /en/products/1' do
25
+ params.merge(:locale => 'en').should == routes.recognize_path('/en/products/1')
26
+ end
27
+
28
+ it 'should recognize /fr/products/1' do
29
+ params.merge(:locale => 'fr').should == routes.recognize_path('/fr/products/1')
30
+ end
31
+
32
+ it 'should recognize /products/1.xml' do
33
+ params.merge(:format => 'xml').should == routes.recognize_path('/products/1.xml')
34
+ end
35
+
36
+ it 'should recognize /products/1.json' do
37
+ params.merge(:format => 'json').should == routes.recognize_path('/products/1.json')
38
+ end
39
+
40
+ it 'should recognize /en' do
41
+ root.merge(:locale => 'en').should == routes.recognize_path('/en/')
42
+ end
43
+
44
+ it 'should recognize /fr' do
45
+ root.merge(:locale => 'fr').should == routes.recognize_path('/fr')
46
+ end
47
+
48
+ it 'should recognize /' do
49
+ root.should == routes.recognize_path('/')
50
+ end
51
+ end
52
+
53
+ context "route generation" do
54
+ context "with locale set" do
55
+ before(:each) do
56
+ I18n.locale = 'fr'
57
+ end
58
+
59
+ context "prepends the segments /:locale to the generated path" do
60
+ it 'if the current locale is not the default locale' do
61
+ routes.generate(params).should == '/fr/products/1'
62
+ end
63
+
64
+ it 'if the current locale is the default locale' do
65
+ I18n.locale = 'en'
66
+ routes.generate(params).should == '/en/products/1'
67
+ end
68
+
69
+ it 'if the current locale is not the default locale and with root url' do
70
+ routes.generate(root).should == '/fr'
71
+ end
72
+
73
+ it 'if the format XLS is not considered as api' do
74
+ routes.generate(params.merge(:format => 'xls')).should == '/fr/products/1.xls'
75
+ end
76
+ end
77
+
78
+ context "does not prepend the segments /:locale to the generated path" do
79
+ it 'if the current locale is the default locale and with root url' do
80
+ I18n.locale = 'en'
81
+ routes.generate(root).should == '/'
82
+ end
83
+
84
+ it 'if the format XML is considered as api' do
85
+ routes.generate(params.merge(:format => 'xml')).should == '/products/1.xml'
86
+ end
87
+
88
+ it 'if the format JSON is considered as api' do
89
+ routes.generate(params.merge(:format => 'json')).should == '/products/1.json'
90
+ end
91
+
92
+ it 'if the format XLS is considered as api' do
93
+ RoutingFilter::LocaleUnlessApi.api_formats = %w( xml json xls )
94
+ routes.generate(params.merge(:format => 'xls')).should == '/products/1.xls'
95
+ end
96
+ end
97
+
98
+ it 'should work with format as symbol' do
99
+ routes.generate(params.merge(:format => :xml)).should == '/products/1.xml'
100
+ end
101
+ end
102
+
103
+ context "with locale as param" do
104
+ let(:params) { { :controller => 'some', :action => 'show', :id => '1', :locale => 'fr' } }
105
+
106
+ context "prepends the segments /:locale to the generated path" do
107
+ it 'if the given locale is not the default locale' do
108
+ routes.generate(params).should == '/fr/products/1'
109
+ end
110
+
111
+ it 'if the given locale is the default locale' do
112
+ routes.generate(params.merge(:locale => 'en')).should == '/en/products/1'
113
+ end
114
+
115
+ it 'if the given locale is not the default locale and with root url' do
116
+ routes.generate(root.merge(:locale => 'fr')).should == '/fr'
117
+ end
118
+
119
+ it 'if a given locale and the format XLS is not considered as api' do
120
+ routes.generate(params.merge(:format => 'xls')).should == '/fr/products/1.xls'
121
+ end
122
+ end
123
+
124
+ context "does not prepend the segments /:locale to the generated path" do
125
+ it 'if a given locale is the default locale and with the root url' do
126
+ routes.generate(root.merge(:locale => 'en')).should == '/'
127
+ end
128
+
129
+ it 'if a given locale and the format XML is considered as api' do
130
+ routes.generate(params.merge(:format => 'xml')).should == '/products/1.xml'
131
+ end
132
+
133
+ it 'if a given locale and the format JSON is considered as api' do
134
+ routes.generate(params.merge(:format => 'json')).should == '/products/1.json'
135
+ end
136
+
137
+ it 'if a given locale and the format XLS is considered as api' do
138
+ RoutingFilter::LocaleUnlessApi.api_formats = %w( xml json xls )
139
+ routes.generate(params.merge(:format => 'xls')).should == '/products/1.xls'
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,14 @@
1
+ require 'routing_filter_locale_unless_api'
2
+
3
+ RSpec::configure do |config|
4
+ config.color_enabled = true
5
+ end
6
+
7
+ include RoutingFilter
8
+
9
+ class SomeController < ActionController::Base
10
+ end
11
+
12
+ def draw_routes(&block)
13
+ ActionDispatch::Routing::RouteSet.new.tap { |set| set.draw(&block) }
14
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routing_filter_locale_unless_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "S\xC3\xA9bastien Grosjean"
@@ -15,13 +15,45 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-01 00:00:00 +02:00
18
+ date: 2011-07-04 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: routing-filter
22
+ name: actionpack
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 21
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 9
34
+ version: 3.0.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: i18n
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 11
46
+ segments:
47
+ - 0
48
+ - 5
49
+ - 0
50
+ version: 0.5.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: routing-filter
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
25
57
  none: false
26
58
  requirements:
27
59
  - - ~>
@@ -33,48 +65,166 @@ dependencies:
33
65
  - 3
34
66
  version: 0.2.3
35
67
  type: :runtime
36
- version_requirements: *id001
68
+ version_requirements: *id003
37
69
  - !ruby/object:Gem::Dependency
38
- name: test_declarative
70
+ name: rake
39
71
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
72
+ requirement: &id004 !ruby/object:Gem::Requirement
41
73
  none: false
42
74
  requirements:
43
- - - ">="
75
+ - - ~>
44
76
  - !ruby/object:Gem::Version
45
- hash: 3
77
+ hash: 25
46
78
  segments:
47
79
  - 0
48
- version: "0"
80
+ - 9
81
+ version: "0.9"
49
82
  type: :development
50
- version_requirements: *id002
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ prerelease: false
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ hash: 23
93
+ segments:
94
+ - 2
95
+ - 6
96
+ - 0
97
+ version: 2.6.0
98
+ type: :development
99
+ version_requirements: *id005
100
+ - !ruby/object:Gem::Dependency
101
+ name: cucumber
102
+ prerelease: false
103
+ requirement: &id006 !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - "="
107
+ - !ruby/object:Gem::Version
108
+ hash: 23
109
+ segments:
110
+ - 1
111
+ - 0
112
+ - 0
113
+ version: 1.0.0
114
+ type: :development
115
+ version_requirements: *id006
116
+ - !ruby/object:Gem::Dependency
117
+ name: guard-rspec
118
+ prerelease: false
119
+ requirement: &id007 !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - "="
123
+ - !ruby/object:Gem::Version
124
+ hash: 9
125
+ segments:
126
+ - 0
127
+ - 1
128
+ - 9
129
+ version: 0.1.9
130
+ type: :development
131
+ version_requirements: *id007
132
+ - !ruby/object:Gem::Dependency
133
+ name: guard-cucumber
134
+ prerelease: false
135
+ requirement: &id008 !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ~>
139
+ - !ruby/object:Gem::Version
140
+ hash: 9
141
+ segments:
142
+ - 0
143
+ - 5
144
+ - 1
145
+ version: 0.5.1
146
+ type: :development
147
+ version_requirements: *id008
148
+ - !ruby/object:Gem::Dependency
149
+ name: growl
150
+ prerelease: false
151
+ requirement: &id009 !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - "="
155
+ - !ruby/object:Gem::Version
156
+ hash: 17
157
+ segments:
158
+ - 1
159
+ - 0
160
+ - 3
161
+ version: 1.0.3
162
+ type: :development
163
+ version_requirements: *id009
164
+ - !ruby/object:Gem::Dependency
165
+ name: yard
166
+ prerelease: false
167
+ requirement: &id010 !ruby/object:Gem::Requirement
168
+ none: false
169
+ requirements:
170
+ - - ~>
171
+ - !ruby/object:Gem::Version
172
+ hash: 7
173
+ segments:
174
+ - 0
175
+ - 7
176
+ - 2
177
+ version: 0.7.2
178
+ type: :development
179
+ version_requirements: *id010
180
+ - !ruby/object:Gem::Dependency
181
+ name: appraisal
182
+ prerelease: false
183
+ requirement: &id011 !ruby/object:Gem::Requirement
184
+ none: false
185
+ requirements:
186
+ - - ~>
187
+ - !ruby/object:Gem::Version
188
+ hash: 31
189
+ segments:
190
+ - 0
191
+ - 3
192
+ - 6
193
+ version: 0.3.6
194
+ type: :development
195
+ version_requirements: *id011
51
196
  description: A routing-filter additional filter that add locale unless XML or JSON, or root_url with default locale
52
- email:
53
- - public@zencocoon.com
197
+ email: public@zencocoon.com
54
198
  executables: []
55
199
 
56
200
  extensions: []
57
201
 
58
- extra_rdoc_files: []
59
-
202
+ extra_rdoc_files:
203
+ - README.md
60
204
  files:
61
205
  - .gitignore
206
+ - .travis.yml
207
+ - .yardopts
208
+ - Appraisals
209
+ - Changelog.md
62
210
  - Gemfile
63
- - README.rdoc
211
+ - Guardfile
212
+ - License.txt
213
+ - README.md
64
214
  - Rakefile
65
215
  - lib/routing_filter_locale_unless_api.rb
66
- - lib/routing_filter_locale_unless_api/locale_unless_api.rb
216
+ - lib/routing_filter_locale_unless_api/filter/locale_unless_api.rb
67
217
  - lib/routing_filter_locale_unless_api/version.rb
68
218
  - routing_filter_locale_unless_api.gemspec
69
- - test/locale_unless_api_test.rb
70
- - test/test_helper.rb
219
+ - spec/routing_filter_locale_unless_api/filter/locale_unless_api_spec.rb
220
+ - spec/spec_helper.rb
71
221
  has_rdoc: true
72
222
  homepage: https://github.com/ZenCocoon/routing_filter_locale_unless_api
73
223
  licenses: []
74
224
 
75
225
  post_install_message:
76
- rdoc_options: []
77
-
226
+ rdoc_options:
227
+ - --charset=UTF-8
78
228
  require_paths:
79
229
  - lib
80
230
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -97,11 +247,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
247
  version: "0"
98
248
  requirements: []
99
249
 
100
- rubyforge_project: "[none]"
101
- rubygems_version: 1.3.7
250
+ rubyforge_project:
251
+ rubygems_version: 1.6.2
102
252
  signing_key:
103
253
  specification_version: 3
104
254
  summary: routing filter that add locale unless XML or JSON, or root_url with default locale
105
255
  test_files:
106
- - test/locale_unless_api_test.rb
107
- - test/test_helper.rb
256
+ - spec/routing_filter_locale_unless_api/filter/locale_unless_api_spec.rb
257
+ - spec/spec_helper.rb
data/README.rdoc DELETED
@@ -1,54 +0,0 @@
1
- = Routing Filter : Locale Unless API
2
-
3
- The LocaleUnlessAPI filter is an extension to Sven Fuchs's routing-filter
4
-
5
- https://github.com/svenfuchs/routing-filter
6
-
7
- LocaleUnlessAPI filter extracts segments matching /:locale from the beginning of
8
- the recognized path and exposes the locale parameter as params[:locale]. When a
9
- path is generated the filter adds the segments to the path accordingly if
10
- the locale parameter is passed to the url helper.
11
-
12
- It will not add the locale parameter as a url fragment if:
13
-
14
- * the format is XML or JSON
15
- * with the root url and default locale
16
-
17
- == Requirements
18
-
19
- routing-filter ~> 0.2.3
20
-
21
- == Installation
22
-
23
- in config/routes.rb
24
-
25
- Rails.application.routes.draw do
26
- filter :locale_unless_api
27
- end
28
-
29
- == Usage
30
-
31
- incoming url: /fr/products
32
- filtered url: /products
33
- params: params[:locale] = 'fr'
34
-
35
- products_path(:locale => 'fr')
36
- generated_path: /fr/products
37
-
38
- More example visible in the tests cases
39
-
40
- == Configuration
41
-
42
- You can customize the API formats with
43
-
44
- Rails.application.routes.draw do
45
- filter :locale_unless_api, :api_formats => %w( xml json xls )
46
- end
47
-
48
- == Maintainers
49
-
50
- * Sébastien Grosjean (http://www.github.com/ZenCocoon)
51
-
52
- == License
53
-
54
- MIT License. Copyright 2011 Sébastien Grosjean, sponsored by {BookingSync Vacation Rental Booking Calendar Software}[http://www.bookingsync.com]
@@ -1,95 +0,0 @@
1
- # The LocaleUnlessAPI filter is an extension to Sven Fuchs's routing-filter
2
- #
3
- # https://github.com/svenfuchs/routing-filter
4
- #
5
- # LocaleUnlessAPI filter extracts segments matching /:locale from the beginning of
6
- # the recognized path and exposes the locale parameter as params[:locale]. When a
7
- # path is generated the filter adds the segments to the path accordingly if
8
- # the locale parameter is passed to the url helper.
9
- #
10
- # It will not add the locale parameter as a url fragment if:
11
- #
12
- # * the format is XML or JSON
13
- # * with the root url and default locale
14
- #
15
- # incoming url: /fr/products
16
- # filtered url: /products
17
- # params: params[:locale] = 'fr'
18
- #
19
- # products_path(:locale => 'fr')
20
- # generated_path: /fr/products
21
- #
22
- # You can install the filter like this:
23
- #
24
- # in config/routes.rb
25
- #
26
- # Rails.application.routes.draw do
27
- # filter :locale_unless_api
28
- # end
29
-
30
- require 'i18n'
31
-
32
- module RoutingFilter
33
- class LocaleUnlessApi < Filter
34
- @@api_formats = %w( xml json )
35
- cattr_writer :api_formats
36
-
37
- class << self
38
- def api_format?(format)
39
- format && @@api_formats.include?(format.to_s.downcase)
40
- end
41
-
42
- def locales
43
- @@locales ||= I18n.available_locales.map(&:to_sym)
44
- end
45
-
46
- def locales=(locales)
47
- @@locales = locales.map(&:to_sym)
48
- end
49
-
50
- def locales_pattern
51
- @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
52
- end
53
- end
54
-
55
- def around_recognize(path, env, &block)
56
- locale = extract_segment!(self.class.locales_pattern, path) # remove the locale from the beginning of the path
57
- yield.tap do |params| # invoke the given block (calls more filters and finally routing)
58
- params[:locale] = locale if locale # set recognized locale to the resulting params hash
59
- end
60
- end
61
-
62
- def around_generate(*args, &block)
63
- params = args.extract_options! # this is because we might get a call like forum_topics_path(forum, topic, :locale => :en)
64
-
65
- format = params[:format] # save the current format
66
- locale = params.delete(:locale) # extract the passed :locale option
67
- locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
68
- locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
69
-
70
- args << params
71
-
72
- yield.tap do |result|
73
- prepend_segment!(result, locale) if prepend_locale?(result, locale, format)
74
- end
75
- end
76
-
77
- protected
78
-
79
- def valid_locale?(locale)
80
- locale && self.class.locales.include?(locale.to_sym)
81
- end
82
-
83
- def default_locale?(locale)
84
- locale && locale.to_sym == I18n.default_locale.to_sym
85
- end
86
-
87
- def root_path?(result)
88
- result == '/'
89
- end
90
-
91
- def prepend_locale?(result, locale, format)
92
- locale && !self.class.api_format?(format) && !(root_path?(result) && default_locale?(locale))
93
- end
94
- end
95
- end
@@ -1,130 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- class LocaleUnlessApiTest < Test::Unit::TestCase
4
- attr_reader :routes, :root, :params
5
-
6
- def setup
7
- I18n.locale = nil
8
- I18n.default_locale = :en
9
- I18n.available_locales = %w(fr en)
10
-
11
- RoutingFilter::LocaleUnlessApi.api_formats = %w( xml json )
12
-
13
- @root = { :controller => 'some', :action => 'index' }
14
- @params = { :controller => 'some', :action => 'show', :id => '1' }
15
-
16
- @routes = draw_routes do
17
- filter :locale_unless_api
18
- match 'products/:id', :to => 'some#show'
19
- root :to => 'some#index'
20
- end
21
- end
22
-
23
- test 'recognizes the path /en/products/1' do
24
- assert_equal params.merge(:locale => 'en'), routes.recognize_path('/en/products/1')
25
- end
26
-
27
- test 'recognizes the path /fr/products/1' do
28
- assert_equal params.merge(:locale => 'fr'), routes.recognize_path('/fr/products/1')
29
- end
30
-
31
- test 'recognizes the path /products/1.xml' do
32
- assert_equal params.merge(:format => 'xml'), routes.recognize_path('/products/1.xml')
33
- end
34
-
35
- test 'recognizes the path /products/1.json' do
36
- assert_equal params.merge(:format => 'json'), routes.recognize_path('/products/1.json')
37
- end
38
-
39
- test 'recognizes the path /en' do
40
- assert_equal root.merge(:locale => 'en'), routes.recognize_path('/en/')
41
- end
42
-
43
- test 'recognizes the path /fr' do
44
- assert_equal root.merge(:locale => 'fr'), routes.recognize_path('/fr')
45
- end
46
-
47
- test 'recognizes the path /' do
48
- assert_equal root, routes.recognize_path('/')
49
- end
50
-
51
-
52
- test 'prepends the segments /:locale to the generated path if the current locale is not the default locale' do
53
- I18n.locale = 'fr'
54
- assert_equal '/fr/products/1', routes.generate(params)
55
- end
56
-
57
- test 'prepends the segments /:locale to the generated path even if the current locale is not the default locale' do
58
- I18n.locale = 'en'
59
- assert_equal '/en/products/1', routes.generate(params)
60
- end
61
-
62
- test 'does not prepend the segments /:locale to the generated path if the current locale is the default locale and with root url' do
63
- I18n.locale = 'en'
64
- assert_equal '/', routes.generate(root)
65
- end
66
-
67
- test 'prepends the segments /:locale to the generated path if the current locale is not the default locale and with root url' do
68
- I18n.locale = 'fr'
69
- assert_equal '/fr', routes.generate(root)
70
- end
71
-
72
- test 'does not prepend the segments /:locale to the generated path if the format XML is considered as api' do
73
- I18n.locale = 'fr'
74
- assert_equal '/products/1.xml', routes.generate(params.merge(:format => 'xml'))
75
- end
76
-
77
- test 'does not prepend the segments /:locale to the generated path if the format JSON is considered as api' do
78
- I18n.locale = 'fr'
79
- assert_equal '/products/1.json', routes.generate(params.merge(:format => 'json'))
80
- end
81
-
82
- test 'prepends the segments /:locale to the generated path if the format XLS is not considered as api' do
83
- I18n.locale = 'fr'
84
- assert_equal '/fr/products/1.xls', routes.generate(params.merge(:format => 'xls'))
85
- end
86
-
87
- test 'does not prepend the segments /:locale to the generated path if the format XLS is considered as api' do
88
- I18n.locale = 'fr'
89
- RoutingFilter::LocaleUnlessApi.api_formats = %w( xml json xls )
90
- assert_equal '/products/1.xls', routes.generate(params.merge(:format => 'xls'))
91
- end
92
-
93
- test 'should work with format as symbol' do
94
- assert_equal '/products/1.xml', routes.generate(params.merge(:format => :xml))
95
- end
96
-
97
-
98
- test 'prepends the segments /:locale to the generated path if the given locale is not the default locale' do
99
- assert_equal '/fr/products/1', routes.generate(params.merge(:locale => 'fr'))
100
- end
101
-
102
- test 'prepends the segments /:locale to the generated path even if the given locale is the default locale' do
103
- assert_equal '/en/products/1', routes.generate(params.merge(:locale => 'en'))
104
- end
105
-
106
- test 'does not prepend the segments /:locale to the generated path if the given locale is the default locale and with root url' do
107
- assert_equal '/', routes.generate(root.merge(:locale => 'en'))
108
- end
109
-
110
- test 'prepends the segments /:locale to the generated path if the given locale is not the default locale and with root url' do
111
- assert_equal '/fr', routes.generate(root.merge(:locale => 'fr'))
112
- end
113
-
114
- test 'does not prepend the segments /:locale to the generated path if a given locale and the format XML is considered as api' do
115
- assert_equal '/products/1.xml', routes.generate(params.merge(:format => 'xml', :locale => 'fr'))
116
- end
117
-
118
- test 'does not prepend the segments /:locale to the generated path if a given locale and the format JSON is considered as api' do
119
- assert_equal '/products/1.json', routes.generate(params.merge(:format => 'json', :locale => 'fr'))
120
- end
121
-
122
- test 'prepends the segments /:locale to the generated path if a given locale and the format XLS is not considered as api' do
123
- assert_equal '/fr/products/1.xls', routes.generate(params.merge(:format => 'xls', :locale => 'fr'))
124
- end
125
-
126
- test 'does not prepend the segments /:locale to the generated path if a given locale and the format XLS is considered as api' do
127
- RoutingFilter::LocaleUnlessApi.api_formats = %w( xml json xls )
128
- assert_equal '/products/1.xls', routes.generate(params.merge(:format => 'xls', :locale => 'fr'))
129
- end
130
- end
data/test/test_helper.rb DELETED
@@ -1,26 +0,0 @@
1
- ENV['RAILS_ENV'] = 'test'
2
-
3
- $:.unshift File.expand_path('../../lib', __FILE__)
4
-
5
- require 'rubygems'
6
-
7
- require 'test/unit'
8
- require 'i18n'
9
- require 'action_pack'
10
- require 'active_support'
11
- require 'action_controller'
12
- require 'active_support/core_ext/enumerable.rb'
13
- require 'test_declarative'
14
- require 'routing_filter'
15
- require 'routing_filter_locale_unless_api'
16
-
17
- include RoutingFilter
18
-
19
- class SomeController < ActionController::Base
20
- end
21
-
22
- class Test::Unit::TestCase
23
- def draw_routes(&block)
24
- ActionDispatch::Routing::RouteSet.new.tap { |set| set.draw(&block) }
25
- end
26
- end