subdomain_locale 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gemtest ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "hoe"
2
+
3
+ Hoe.spec 'subdomain_locale' do
4
+ developer 'Semyon Perepelitsa', 'sema@sema.in'
5
+ end
6
+
7
+ require "rake/testtask"
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = Dir['test/**/*_test.rb']
11
+ t.verbose = true
12
+ end
13
+
14
+ task :default => :test
@@ -0,0 +1,13 @@
1
+ module SubdomainLocale
2
+ module Controller
3
+ def self.included(base)
4
+ base.before_filter :set_locale
5
+ end
6
+
7
+ private
8
+
9
+ def set_locale
10
+ Locale.set(request.subdomain)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module SubdomainLocale
2
+ class Locale
3
+ class << self
4
+ def set(subdomain)
5
+ I18n.locale = find(subdomain).to_sym
6
+ end
7
+
8
+ private
9
+
10
+ def find(subdomain)
11
+ new(subdomain).valid || default
12
+ end
13
+
14
+ def default
15
+ new(I18n.default_locale)
16
+ end
17
+ end
18
+
19
+ def initialize(str)
20
+ @str = str.to_s
21
+ end
22
+
23
+ def to_s
24
+ @str
25
+ end
26
+
27
+ def to_sym
28
+ @sym ||= @str.to_sym
29
+ end
30
+
31
+ def subdomain
32
+ if default?
33
+ 'www'
34
+ else
35
+ to_s
36
+ end
37
+ end
38
+
39
+ def default?
40
+ I18n.default_locale.to_sym == self.to_sym
41
+ end
42
+
43
+ def valid
44
+ self if valid?
45
+ end
46
+
47
+ def valid?
48
+ I18n.available_locales.include?(self.to_sym)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,12 @@
1
+ module SubdomainLocale
2
+ module UrlFor
3
+ def url_for(options=nil)
4
+ if options and locale = options.delete(:locale)
5
+ options[:subdomain] = Locale.new(locale).subdomain
6
+ options[:only_path] = false
7
+ end
8
+
9
+ super
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module SubdomainLocale
2
+ VERSION = "0.0.1"
3
+ autoload :UrlFor, "subdomain_locale/url_for"
4
+ autoload :Controller, "subdomain_locale/controller"
5
+ autoload :Locale, "subdomain_locale/locale"
6
+ end
@@ -0,0 +1,36 @@
1
+ require "test_helper"
2
+
3
+ class ControllerTest < MiniTest::Unit::TestCase
4
+ class Controller
5
+ class Request
6
+ def subdomain
7
+ "ru"
8
+ end
9
+ end
10
+
11
+ def self.before_filter(sym)
12
+ @@before_filters ||= []
13
+ @@before_filters << sym
14
+ end
15
+
16
+ def self.before_filters
17
+ @@before_filters
18
+ end
19
+
20
+ def run
21
+ @@before_filters.each{ |sym| send(sym) }
22
+ end
23
+
24
+ def request
25
+ Request.new
26
+ end
27
+
28
+ include SubdomainLocale::Controller
29
+ end
30
+
31
+ def test_sets_locale
32
+ assert_nil I18n.locale
33
+ Controller.new.run
34
+ assert_equal :ru, I18n.locale
35
+ end
36
+ end
data/test/i18n_stub.rb ADDED
@@ -0,0 +1,13 @@
1
+ module I18n
2
+ class << self
3
+ attr_accessor :locale
4
+
5
+ def available_locales
6
+ [:az, :ru, :en]
7
+ end
8
+
9
+ def default_locale
10
+ :az
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+
3
+ class LocaleTest < MiniTest::Unit::TestCase
4
+ include SubdomainLocale
5
+
6
+ def test_sets_locale
7
+ Locale.set("ru")
8
+ assert_equal :ru, I18n.locale
9
+ end
10
+
11
+ def test_sets_default_locale_for_nil
12
+ Locale.set(nil)
13
+ assert_equal :az, I18n.locale
14
+ end
15
+
16
+ def test_subdomain
17
+ assert_equal "ru", Locale.new(:ru).subdomain
18
+ end
19
+
20
+ def test_default_subdomain
21
+ assert_equal "www", Locale.new(:az).subdomain
22
+ end
23
+
24
+ def test_default?
25
+ assert Locale.new(:az).default?
26
+ end
27
+
28
+ def test_not_default?
29
+ refute Locale.new(:ru).default?
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path('..', __FILE__)
2
+ $LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
3
+ require "minitest/unit"
4
+ require "subdomain_locale"
5
+ require "i18n_stub"
6
+ MiniTest::Unit.autorun
@@ -0,0 +1,32 @@
1
+ require "test_helper"
2
+
3
+ class UrlForTest < MiniTest::Unit::TestCase
4
+ module UrlFor
5
+ def url_for(*args)
6
+ @actual = args
7
+ end
8
+ end
9
+
10
+ include UrlFor
11
+ include SubdomainLocale::UrlFor
12
+
13
+ def test_does_not_affect_if_there_is_no_locale
14
+ url_for(foo: 'bar')
15
+ assert_equal [{foo: 'bar'}], @actual
16
+ end
17
+
18
+ def test_replaces_locale_with_subdomain_and_forces_not_only_path
19
+ url_for(foo: 'bar', locale: :ru)
20
+ assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
21
+ end
22
+
23
+ def test_replaces_locale_with_subdomain_and_overrides_only_path
24
+ url_for(foo: 'bar', locale: :ru, only_path: true)
25
+ assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
26
+ end
27
+
28
+ def test_sets_subdomain_to_www_for_default_locale
29
+ url_for(foo: 'bar', locale: :az)
30
+ assert_equal [{foo: 'bar', subdomain: 'www', only_path: false}], @actual
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subdomain_locale
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Semyon Perepelitsa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &70239990318660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70239990318660
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ requirement: &70239990318180 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.12'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70239990318180
36
+ description: ''
37
+ email:
38
+ - sema@sema.in
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - Rakefile
44
+ - lib/subdomain_locale.rb
45
+ - lib/subdomain_locale/controller.rb
46
+ - lib/subdomain_locale/locale.rb
47
+ - lib/subdomain_locale/url_for.rb
48
+ - test/controller_test.rb
49
+ - test/i18n_stub.rb
50
+ - test/locale_test.rb
51
+ - test/test_helper.rb
52
+ - test/url_for_test.rb
53
+ - .gemtest
54
+ homepage:
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --main
59
+ - README.txt
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project: subdomain_locale
76
+ rubygems_version: 1.8.13
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: ''
80
+ test_files:
81
+ - test/test_helper.rb