dragoman 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dragoman/mapper.rb +3 -49
- data/lib/dragoman/railtie.rb +2 -5
- data/lib/dragoman/route_set.rb +40 -0
- data/lib/dragoman/version.rb +1 -1
- data/lib/dragoman.rb +1 -6
- data/spec/fixtures/config/routes.rb +6 -0
- data/spec/fixtures/locales/routes.yml +1 -0
- data/spec/routing/routes_spec.rb +17 -9
- data/spec/spec_helper.rb +4 -0
- metadata +3 -4
- data/lib/dragoman/mapper_rails3.rb +0 -39
- data/lib/dragoman/mapper_rails4.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a203343dac859415cb421f222a4ca5be311f540d
|
4
|
+
data.tar.gz: e38180d72757b4752bc88ffcfa6e7fd38115a8b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2377cbbd03c6ea5c8fedce7a3e9943632bb2f62ac1c5a503100ee7bf7d580b046ee80b8d06da086d793f849454ebe707dcff0c20d0fdc4690892d7e8efad8bfa
|
7
|
+
data.tar.gz: 738305615fa10ed287b2889cb71991642515b00b0b3fe33c39603918c382fe6fe670c1351232b65f38b40bdc36a05120312491639d5678b8bb3f23e5634d435d
|
data/lib/dragoman/mapper.rb
CHANGED
@@ -1,57 +1,11 @@
|
|
1
1
|
module Dragoman
|
2
2
|
module Mapper
|
3
|
-
|
4
|
-
module Mapping
|
5
|
-
def build_with_localization scope, set, path, as, options
|
6
|
-
if options[:locale]
|
7
|
-
Dragoman::UrlHelpers.add_untranslated_helpers as, set.named_routes.path_helpers_module, set.named_routes.url_helpers_module if as
|
8
|
-
as = "#{as}_#{options[:locale]}" if as.present?
|
9
|
-
as = nil if as && set.named_routes.routes[as.to_sym] # TODO: why do we need to set as to nil?
|
10
|
-
end
|
11
|
-
build_without_localization scope, set, path, as, options
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
3
|
def localize
|
18
|
-
@
|
19
|
-
locales
|
20
|
-
locales.each do |locale|
|
21
|
-
@current_locale = locale.to_s
|
4
|
+
@locales = I18n.available_locales
|
5
|
+
scope '(:locale)', defaults: {dragoman_options: {locales: @locales}} do
|
22
6
|
yield
|
23
7
|
end
|
24
|
-
@
|
25
|
-
end
|
26
|
-
|
27
|
-
def add_route_with_localization(action, options)
|
28
|
-
if @current_locale
|
29
|
-
options[:path] = Dragoman::Translator.translate_path(options[:path] || action, @current_locale) unless canonical_action?(action)
|
30
|
-
options[:locale] = @current_locale
|
31
|
-
end
|
32
|
-
add_route_without_localization action, options
|
33
|
-
end
|
34
|
-
|
35
|
-
def scope_with_localization(*args, &block)
|
36
|
-
if @current_locale
|
37
|
-
options = args.extract_options!.dup
|
38
|
-
if options[:path] || args.any?
|
39
|
-
options[:path] = Dragoman::Translator.translate_path(options[:path] || args.flatten.join('/'), @current_locale)
|
40
|
-
options[:shallow_path] = Dragoman::Translator.translate_path(options[:shallow_path], @current_locale) if options[:shallow_path]
|
41
|
-
end
|
42
|
-
scope_without_localization options, &block
|
43
|
-
else
|
44
|
-
scope_without_localization *args, &block
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def resources_with_localization(*resources, &block)
|
49
|
-
options = resources.extract_options!.dup
|
50
|
-
if @current_locale
|
51
|
-
options[:path] = Dragoman::Translator.translate_path(options[:path] || resources.last, @current_locale)
|
52
|
-
end
|
53
|
-
resources_without_localization(*resources, options, &block)
|
8
|
+
@locales = nil
|
54
9
|
end
|
55
|
-
|
56
10
|
end
|
57
11
|
end
|
data/lib/dragoman/railtie.rb
CHANGED
@@ -2,11 +2,8 @@ module Dragoman
|
|
2
2
|
|
3
3
|
class Railtie < Rails::Railtie
|
4
4
|
initializer 'dragoman.insert_into_routing_mapper' do |app|
|
5
|
-
|
6
|
-
|
7
|
-
else
|
8
|
-
ActionDispatch::Routing::Mapper.send :include, Dragoman::MapperRails3
|
9
|
-
end
|
5
|
+
ActionDispatch::Routing::Mapper.send :include, Dragoman::Mapper
|
6
|
+
ActionDispatch::Routing::RouteSet.send :include, Dragoman::RouteSet
|
10
7
|
end
|
11
8
|
end
|
12
9
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Dragoman
|
2
|
+
module RouteSet
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send :alias_method_chain, :add_route, :localization
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_route_with_localization(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
|
9
|
+
dragoman_options = defaults.delete :dragoman_options
|
10
|
+
path_helpers, url_helpers = helper_modules
|
11
|
+
if dragoman_options
|
12
|
+
Dragoman::UrlHelpers.add_untranslated_helpers name, path_helpers, url_helpers if name
|
13
|
+
dragoman_options[:locales].each do |locale|
|
14
|
+
new_conditions = conditions.dup
|
15
|
+
new_requirements = requirements.dup
|
16
|
+
|
17
|
+
new_name = name ? "#{name}_#{locale}" : nil
|
18
|
+
new_name = nil if new_name && named_routes.routes[new_name.to_sym] # Why is this necessary
|
19
|
+
|
20
|
+
if new_path = new_conditions.delete(:path_info)
|
21
|
+
new_path = Dragoman::Translator.translate_path new_path, locale
|
22
|
+
new_conditions[:path_info] = new_path
|
23
|
+
new_conditions[:parsed_path_info] = Dragoman::Journey::Parser.new.parse new_path if new_conditions[:parsed_path_info]
|
24
|
+
end
|
25
|
+
new_requirements[:locale] = locale
|
26
|
+
add_route_without_localization app, new_conditions, new_requirements, defaults.dup, new_name, anchor
|
27
|
+
end
|
28
|
+
else
|
29
|
+
add_route_without_localization app, conditions, requirements, defaults, name, anchor
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def helper_modules
|
36
|
+
named_routes.respond_to?(:path_helpers_module) ? [named_routes.path_helpers_module, named_routes.url_helpers_module] : [named_routes.module, named_routes.module]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
data/lib/dragoman/version.rb
CHANGED
data/lib/dragoman.rb
CHANGED
@@ -13,9 +13,4 @@ require "dragoman/url_helpers"
|
|
13
13
|
require "dragoman/mapper"
|
14
14
|
require "dragoman/railtie" if defined?(Rails)
|
15
15
|
require "dragoman/version"
|
16
|
-
|
17
|
-
if Rails::VERSION::MAJOR >= 4
|
18
|
-
require "dragoman/mapper_rails4"
|
19
|
-
else
|
20
|
-
require "dragoman/mapper_rails3"
|
21
|
-
end
|
16
|
+
require "dragoman/route_set"
|
data/spec/routing/routes_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class ProductsController < ApplicationController
|
4
|
-
end
|
3
|
+
class ProductsController < ApplicationController; end
|
4
|
+
class VotesController < ApplicationController; end
|
5
5
|
|
6
6
|
describe 'routes' do
|
7
7
|
|
@@ -15,10 +15,10 @@ describe 'routes' do
|
|
15
15
|
it 'translates resources' do
|
16
16
|
expect(products_en_path).to eq '/products'
|
17
17
|
expect(new_product_en_path).to eq '/products/new'
|
18
|
-
expect(edit_product_en_path(1)).to eq '/products/1/edit'
|
18
|
+
expect(edit_product_en_path(id: 1)).to eq '/products/1/edit'
|
19
19
|
expect(products_nl_path).to eq '/producten'
|
20
20
|
expect(new_product_nl_path).to eq '/producten/nieuw'
|
21
|
-
expect(edit_product_nl_path(1)).to eq '/producten/1/wijzigen'
|
21
|
+
expect(edit_product_nl_path(id: 1)).to eq '/producten/1/wijzigen'
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'translates resource' do
|
@@ -41,8 +41,11 @@ describe 'routes' do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'sets the correct locale' do
|
44
|
-
|
45
|
-
|
44
|
+
expect(get: '/producten').to route_to(controller: 'products', action: 'index')
|
45
|
+
expect(get: '/products').to route_to(controller: 'products', action: 'index')
|
46
|
+
expect(get: '/nl/producten').to route_to(controller: 'products', action: 'index', locale: 'nl')
|
47
|
+
expect(get: '/en/products').to route_to(controller: 'products', action: 'index', locale: 'en')
|
48
|
+
expect(get: '/en/producten').not_to be_routable
|
46
49
|
end
|
47
50
|
|
48
51
|
it 'skips empty paths' do
|
@@ -56,7 +59,7 @@ describe 'routes' do
|
|
56
59
|
end
|
57
60
|
|
58
61
|
it 'looks up translations by the path option if present' do
|
59
|
-
expect(edit_invitation_nl_path(2)).to eq '/uitnodigingen/2/accepteren'
|
62
|
+
expect(edit_invitation_nl_path(id: 2)).to eq '/uitnodigingen/2/accepteren'
|
60
63
|
end
|
61
64
|
|
62
65
|
it 'looks up resources translations by the path option if present' do
|
@@ -65,8 +68,8 @@ describe 'routes' do
|
|
65
68
|
end
|
66
69
|
|
67
70
|
it 'translated shallow paths' do
|
68
|
-
expect(driver_nl_path(3)).to eq '/snelle/bestuurders/3'
|
69
|
-
expect(driver_en_path(3)).to eq '/fast/drivers/3'
|
71
|
+
expect(driver_nl_path(id: 3)).to eq '/snelle/bestuurders/3'
|
72
|
+
expect(driver_en_path(id: 3)).to eq '/fast/drivers/3'
|
70
73
|
end
|
71
74
|
|
72
75
|
it 'skips blank translations' do
|
@@ -78,6 +81,11 @@ describe 'routes' do
|
|
78
81
|
expect(sounds_nl_path).to eq '/sounds'
|
79
82
|
end
|
80
83
|
|
84
|
+
it 'translates routes in the original order as specified in routes.rb' do
|
85
|
+
expect(get: 'votes/positive').to route_to(action: 'positive', controller: 'votes')
|
86
|
+
expect(get: 'votes/positief').to route_to(action: 'positive', controller: 'votes') # should use 'positive' action instead of 'show'
|
87
|
+
end
|
88
|
+
|
81
89
|
describe 'adds untranslated path helpers' do
|
82
90
|
it 'uses the I18n locale' do
|
83
91
|
I18n.locale = :nl
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dragoman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pieter Visser
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-05-
|
12
|
+
date: 2015-05-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec-rails
|
@@ -59,9 +59,8 @@ files:
|
|
59
59
|
- gemfiles/rails_4.2.gemfile
|
60
60
|
- lib/dragoman.rb
|
61
61
|
- lib/dragoman/mapper.rb
|
62
|
-
- lib/dragoman/mapper_rails3.rb
|
63
|
-
- lib/dragoman/mapper_rails4.rb
|
64
62
|
- lib/dragoman/railtie.rb
|
63
|
+
- lib/dragoman/route_set.rb
|
65
64
|
- lib/dragoman/translation_visitor.rb
|
66
65
|
- lib/dragoman/translator.rb
|
67
66
|
- lib/dragoman/url_helpers.rb
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module Dragoman
|
2
|
-
|
3
|
-
module MapperRails3
|
4
|
-
include Mapper
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
base.send :alias_method_chain, :add_route, :localization
|
8
|
-
base.send :alias_method_chain, :scope, :localization
|
9
|
-
|
10
|
-
base.send :alias_method_chain, :resources, :localization
|
11
|
-
|
12
|
-
base::Mapping.send :include, MappingRails3
|
13
|
-
end
|
14
|
-
|
15
|
-
module MappingRails3
|
16
|
-
def self.included(base)
|
17
|
-
base.send :alias_method_chain, :to_route, :localization
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_route_with_localization
|
21
|
-
if @options[:locale]
|
22
|
-
Dragoman::UrlHelpers.add_untranslated_helpers @options[:as], @set.named_routes.module, @set.named_routes.module if @options[:as]
|
23
|
-
@options[:as] = "#{@options[:as]}_#{@options[:locale]}" if @options[:as].present?
|
24
|
-
@options[:as] = nil if @options[:as] && @set.named_routes.routes[@options[:as].to_sym] # TODO: why do we need to set as to nil?
|
25
|
-
end
|
26
|
-
to_route_without_localization
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def add_route_with_localization(action, options)
|
31
|
-
if @current_locale
|
32
|
-
options[:path] = Dragoman::Translator.translate_path(options[:path] || action, @current_locale) unless canonical_action?(action, true)
|
33
|
-
options[:locale] = @current_locale
|
34
|
-
end
|
35
|
-
add_route_without_localization action, options
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Dragoman
|
2
|
-
module MapperRails4
|
3
|
-
|
4
|
-
include Mapper
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
base.send :alias_method_chain, :add_route, :localization
|
8
|
-
base.send :alias_method_chain, :scope, :localization
|
9
|
-
|
10
|
-
base.send :alias_method_chain, :resources, :localization
|
11
|
-
|
12
|
-
base::Mapping.extend Mapping
|
13
|
-
base::Mapping.class_eval do
|
14
|
-
class << self
|
15
|
-
alias_method_chain :build, :localization
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|