locale_setter 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +19 -0
  4. data/Gemfile +19 -1
  5. data/README.md +44 -27
  6. data/Rakefile +67 -4
  7. data/lib/locale_setter.rb +28 -1
  8. data/lib/locale_setter/configuration.rb +8 -0
  9. data/lib/locale_setter/{rails.rb → controller.rb} +6 -6
  10. data/lib/locale_setter/domain.rb +17 -0
  11. data/lib/locale_setter/generic.rb +11 -5
  12. data/lib/locale_setter/railtie.rb +17 -0
  13. data/lib/locale_setter/user.rb +15 -6
  14. data/lib/locale_setter/version.rb +1 -1
  15. data/locale_setter.gemspec +1 -0
  16. data/spec/dummy/.rspec +2 -0
  17. data/spec/dummy/Rakefile +7 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +16 -0
  19. data/spec/dummy/app/controllers/articles_controller.rb +9 -0
  20. data/spec/dummy/app/controllers/users_controller.rb +9 -0
  21. data/spec/dummy/app/helpers/application_helper.rb +5 -0
  22. data/spec/dummy/app/models/article.rb +2 -0
  23. data/spec/dummy/app/models/user.rb +2 -0
  24. data/spec/dummy/app/views/_form.html.erb +25 -0
  25. data/spec/dummy/app/views/articles/_form.html.erb +25 -0
  26. data/spec/dummy/app/views/articles/edit.html.erb +6 -0
  27. data/spec/dummy/app/views/articles/index.html.erb +7 -0
  28. data/spec/dummy/app/views/articles/new.html.erb +5 -0
  29. data/spec/dummy/app/views/articles/show.html.erb +15 -0
  30. data/spec/dummy/app/views/edit.html.erb +6 -0
  31. data/spec/dummy/app/views/index.html.erb +25 -0
  32. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  33. data/spec/dummy/app/views/new.html.erb +5 -0
  34. data/spec/dummy/app/views/show.html.erb +15 -0
  35. data/spec/dummy/bin/rails +4 -0
  36. data/spec/dummy/config.ru +4 -0
  37. data/spec/dummy/config/application.rb +68 -0
  38. data/spec/dummy/config/boot.rb +5 -0
  39. data/spec/dummy/config/database.yml +25 -0
  40. data/spec/dummy/config/environment.rb +5 -0
  41. data/spec/dummy/config/environments/development.rb +33 -0
  42. data/spec/dummy/config/environments/production.rb +57 -0
  43. data/spec/dummy/config/environments/test.rb +31 -0
  44. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  45. data/spec/dummy/config/initializers/inflections.rb +15 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  47. data/spec/dummy/config/initializers/secret_token.rb +8 -0
  48. data/spec/dummy/config/initializers/session_store.rb +8 -0
  49. data/spec/dummy/config/locales/arr.yml +2 -0
  50. data/spec/dummy/config/locales/en.yml +2 -0
  51. data/spec/dummy/config/locales/http.yml +2 -0
  52. data/spec/dummy/config/locales/user.yml +2 -0
  53. data/spec/dummy/config/mongoid.yml +80 -0
  54. data/spec/dummy/config/routes.rb +4 -0
  55. data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
  56. data/spec/dummy/db/schema.rb +30 -0
  57. data/spec/dummy/db/seeds.rb +1 -0
  58. data/spec/dummy/lib/tasks/test.rake +16 -0
  59. data/spec/dummy/public/404.html +26 -0
  60. data/spec/dummy/public/422.html +26 -0
  61. data/spec/dummy/public/500.html +25 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/dummy/script/rails +6 -0
  64. data/spec/{locale/rails_spec.rb → locale_setter/controller_spec.rb} +6 -6
  65. data/spec/locale_setter/domain_spec.rb +19 -0
  66. data/spec/{locale → locale_setter}/generic_spec.rb +16 -7
  67. data/spec/{locale → locale_setter}/http_spec.rb +0 -0
  68. data/spec/locale_setter/locale_setter_spec.rb +28 -0
  69. data/spec/{locale → locale_setter}/matcher_spec.rb +0 -0
  70. data/spec/{locale → locale_setter}/user_spec.rb +0 -0
  71. data/spec/request_helper.rb +8 -0
  72. data/spec/requests/request_spec.rb +43 -0
  73. data/spec/support/dummy_app.rb +85 -0
  74. data/spec/support/matchers/have_text.rb +50 -0
  75. metadata +142 -13
File without changes
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe LocaleSetter do
4
+ context "configuring" do
5
+
6
+ before { described_class.configuration = nil }
7
+
8
+ it "should be possible to set values via plain calls" do
9
+ described_class.config.url_param = :some_method
10
+ expect(described_class.config.url_param).to eq(:some_method)
11
+ end
12
+
13
+ it "should be possible to set values via block" do
14
+ described_class.configure do |conf|
15
+ conf.url_param = :some_method
16
+ end
17
+
18
+ expect(described_class.config.url_param).to eq(:some_method)
19
+ end
20
+
21
+ it "initializes config with default values" do
22
+ expect(described_class.config.url_param).to eq(described_class::URL_PARAM)
23
+ expect(described_class.config.user_locale_method).to eq(described_class::USER_METHOD)
24
+ expect(described_class.config.localized_domains).to eq({})
25
+ expect(described_class.config.current_user_method).to eq(described_class::CURRENT_USER_METHOD)
26
+ end
27
+ end
28
+ end
File without changes
File without changes
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ require 'locale_setter'
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with(:rspec) {|c| c.syntax = :expect}
6
+ config.order = :random
7
+ end
8
+
@@ -0,0 +1,43 @@
1
+ require 'request_helper'
2
+ require 'support/dummy_app'
3
+ require 'support/matchers/have_text'
4
+
5
+ app = DummyApp.new(ENV["RAILS_ENV"])
6
+
7
+ app.start_server do
8
+ describe "articles#index" do
9
+ it "uses the default locale title by default" do
10
+ page = app.get("/articles")
11
+ expect(page).to have_text("Hello, World!")
12
+ end
13
+
14
+ context "with a URL parameter locale" do
15
+ it "uses the specified locale title" do
16
+ page = app.get("/articles?locale=arr")
17
+ expect(page).to have_text("A'hoy, mate!")
18
+ end
19
+ end
20
+
21
+ context "with a current_user who specifies a locale" do
22
+ it "uses the specified locale title" do
23
+ page = app.get("/articles?user=true")
24
+ expect(page).to have_text("Hello, User!")
25
+ end
26
+ end
27
+
28
+ context "with only HTTP_ACCEPT_LANGUAGE specified" do
29
+ Capybara.register_driver :rack_test_accept_language do |app|
30
+ Capybara::RackTest::Driver.new(app, :headers => { 'HTTP_ACCEPT_LANGUAGE' => 'http' })
31
+ end
32
+
33
+ it "uses the specified locale title" do
34
+ pending "needs refactoring to work with new setup" do
35
+ Capybara.current_driver = :rack_test_accept_language
36
+ page = app.get("/articles")
37
+ expect(page).to have_text("Hello, HTTP!")
38
+ Capybara.use_default_driver
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,85 @@
1
+ require 'socket'
2
+ require 'net/http'
3
+
4
+ # Adapted from code by Jon Leighton
5
+ # https://github.com/jonleighton/focused_controller/blob/ec7ccf1/test/acceptance/app_test.rb
6
+
7
+ class DummyApp
8
+
9
+ def initialize(environment)
10
+ raise ArgumentError, "Environment must be development or production" unless ["development", "production"].include?(environment.to_s)
11
+ @environment = environment
12
+ end
13
+
14
+ attr_reader :environment
15
+
16
+ def url
17
+ "http://#{localhost}:#{port}"
18
+ end
19
+
20
+ def get(path)
21
+ Net::HTTP.get(URI(url + path))
22
+ end
23
+
24
+ def within_app(&block)
25
+ Dir.chdir(root, &block)
26
+ end
27
+
28
+ def start_server
29
+ within_app do
30
+ IO.popen("bundle exec rails s -e #{@environment} -p #{port} 2>&1") do |out|
31
+ start = Time.now
32
+ started = false
33
+ output = ""
34
+ timeout = 60.0
35
+
36
+ while !started && !out.eof? && Time.now - start <= timeout
37
+ output << read_output(out)
38
+ sleep 0.1
39
+
40
+ begin
41
+ TCPSocket.new(localhost, port)
42
+ rescue Errno::ECONNREFUSED
43
+ else
44
+ started = true
45
+ end
46
+ end
47
+
48
+ raise "Server failed to start:\n#{output}" unless started
49
+
50
+ yield
51
+
52
+ Process.kill("KILL", out.pid)
53
+ File.delete("tmp/pids/server.pid")
54
+ end
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def root
61
+ File.expand_path("../../dummy", __FILE__)
62
+ end
63
+
64
+ def localhost
65
+ "127.0.0.1"
66
+ end
67
+
68
+ def port
69
+ @port ||= begin
70
+ server = TCPServer.new(localhost, 0)
71
+ server.addr[1]
72
+ ensure
73
+ server.close if server
74
+ end
75
+ end
76
+
77
+ def read_output(stream)
78
+ read = IO.select([stream], [], [stream], 0.1)
79
+ output = ""
80
+ loop { output << stream.read_nonblock(1024) } if read
81
+ output
82
+ rescue Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError
83
+ output
84
+ end
85
+ end
@@ -0,0 +1,50 @@
1
+ require 'capybara'
2
+
3
+ module HaveTextMatcher
4
+ def have_text(text)
5
+ HaveText.new(text)
6
+ end
7
+
8
+ class HaveText
9
+ def initialize(text)
10
+ @text = text
11
+ end
12
+
13
+ def in(css)
14
+ @css = css
15
+ self
16
+ end
17
+
18
+ def matches?(subject)
19
+ @subject = Capybara.string(subject)
20
+
21
+ @subject.has_css?(@css || "*", :text => @text)
22
+ end
23
+
24
+ def failure_message_for_should
25
+ "expected to find #{@text.inspect} #{within}"
26
+ end
27
+
28
+ def failure_message_for_should_not
29
+ "expected not to find #{@text.inspect} #{within}"
30
+ end
31
+
32
+ private
33
+
34
+ def within
35
+ if @css && @subject.has_css?(@css)
36
+ "within\n#{@subject.find(@css).native}"
37
+ else
38
+ "#{inside} within\n#{@subject.native}"
39
+ end
40
+ end
41
+
42
+ def inside
43
+ @css ? "inside #{@css.inspect}" : "anywhere"
44
+ end
45
+ end
46
+ end
47
+
48
+ RSpec.configure do |config|
49
+ config.include HaveTextMatcher
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locale_setter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Casimir
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-10 00:00:00.000000000 Z
11
+ date: 2013-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.13.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: capybara
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Automatically set per-request locale in Rails applications
42
56
  email:
43
57
  - jeff@casimircreative.com
@@ -47,26 +61,85 @@ extra_rdoc_files: []
47
61
  files:
48
62
  - .gitignore
49
63
  - .rspec
64
+ - .travis.yml
50
65
  - Gemfile
51
66
  - LICENSE.txt
52
67
  - README.md
53
68
  - Rakefile
54
69
  - lib/locale_setter.rb
70
+ - lib/locale_setter/configuration.rb
71
+ - lib/locale_setter/controller.rb
72
+ - lib/locale_setter/domain.rb
55
73
  - lib/locale_setter/generic.rb
56
74
  - lib/locale_setter/http.rb
57
75
  - lib/locale_setter/matcher.rb
58
76
  - lib/locale_setter/param.rb
59
- - lib/locale_setter/rails.rb
77
+ - lib/locale_setter/railtie.rb
60
78
  - lib/locale_setter/user.rb
61
79
  - lib/locale_setter/version.rb
62
80
  - locale_setter.gemspec
63
- - spec/locale/generic_spec.rb
64
- - spec/locale/http_spec.rb
65
- - spec/locale/matcher_spec.rb
66
- - spec/locale/rails_spec.rb
67
- - spec/locale/user_spec.rb
81
+ - spec/dummy/.rspec
82
+ - spec/dummy/Rakefile
83
+ - spec/dummy/app/controllers/application_controller.rb
84
+ - spec/dummy/app/controllers/articles_controller.rb
85
+ - spec/dummy/app/controllers/users_controller.rb
86
+ - spec/dummy/app/helpers/application_helper.rb
87
+ - spec/dummy/app/models/article.rb
88
+ - spec/dummy/app/models/user.rb
89
+ - spec/dummy/app/views/_form.html.erb
90
+ - spec/dummy/app/views/articles/_form.html.erb
91
+ - spec/dummy/app/views/articles/edit.html.erb
92
+ - spec/dummy/app/views/articles/index.html.erb
93
+ - spec/dummy/app/views/articles/new.html.erb
94
+ - spec/dummy/app/views/articles/show.html.erb
95
+ - spec/dummy/app/views/edit.html.erb
96
+ - spec/dummy/app/views/index.html.erb
97
+ - spec/dummy/app/views/layouts/application.html.erb
98
+ - spec/dummy/app/views/new.html.erb
99
+ - spec/dummy/app/views/show.html.erb
100
+ - spec/dummy/bin/rails
101
+ - spec/dummy/config.ru
102
+ - spec/dummy/config/application.rb
103
+ - spec/dummy/config/boot.rb
104
+ - spec/dummy/config/database.yml
105
+ - spec/dummy/config/environment.rb
106
+ - spec/dummy/config/environments/development.rb
107
+ - spec/dummy/config/environments/production.rb
108
+ - spec/dummy/config/environments/test.rb
109
+ - spec/dummy/config/initializers/backtrace_silencers.rb
110
+ - spec/dummy/config/initializers/inflections.rb
111
+ - spec/dummy/config/initializers/mime_types.rb
112
+ - spec/dummy/config/initializers/secret_token.rb
113
+ - spec/dummy/config/initializers/session_store.rb
114
+ - spec/dummy/config/locales/arr.yml
115
+ - spec/dummy/config/locales/en.yml
116
+ - spec/dummy/config/locales/http.yml
117
+ - spec/dummy/config/locales/user.yml
118
+ - spec/dummy/config/mongoid.yml
119
+ - spec/dummy/config/routes.rb
120
+ - spec/dummy/db/migrate/20121019115657_create_posts.rb
121
+ - spec/dummy/db/schema.rb
122
+ - spec/dummy/db/seeds.rb
123
+ - spec/dummy/lib/tasks/test.rake
124
+ - spec/dummy/log/.gitkeep
125
+ - spec/dummy/public/404.html
126
+ - spec/dummy/public/422.html
127
+ - spec/dummy/public/500.html
128
+ - spec/dummy/public/favicon.ico
129
+ - spec/dummy/script/rails
130
+ - spec/locale_setter/controller_spec.rb
131
+ - spec/locale_setter/domain_spec.rb
132
+ - spec/locale_setter/generic_spec.rb
133
+ - spec/locale_setter/http_spec.rb
134
+ - spec/locale_setter/locale_setter_spec.rb
135
+ - spec/locale_setter/matcher_spec.rb
136
+ - spec/locale_setter/user_spec.rb
137
+ - spec/request_helper.rb
138
+ - spec/requests/request_spec.rb
68
139
  - spec/spec_helper.rb
140
+ - spec/support/dummy_app.rb
69
141
  - spec/support/i18n.rb
142
+ - spec/support/matchers/have_text.rb
70
143
  homepage: http://github.com/jcasimir/locale_setter
71
144
  licenses: []
72
145
  metadata: {}
@@ -91,10 +164,66 @@ signing_key:
91
164
  specification_version: 4
92
165
  summary: Automatically set per-request locale in Rails applications
93
166
  test_files:
94
- - spec/locale/generic_spec.rb
95
- - spec/locale/http_spec.rb
96
- - spec/locale/matcher_spec.rb
97
- - spec/locale/rails_spec.rb
98
- - spec/locale/user_spec.rb
167
+ - spec/dummy/.rspec
168
+ - spec/dummy/Rakefile
169
+ - spec/dummy/app/controllers/application_controller.rb
170
+ - spec/dummy/app/controllers/articles_controller.rb
171
+ - spec/dummy/app/controllers/users_controller.rb
172
+ - spec/dummy/app/helpers/application_helper.rb
173
+ - spec/dummy/app/models/article.rb
174
+ - spec/dummy/app/models/user.rb
175
+ - spec/dummy/app/views/_form.html.erb
176
+ - spec/dummy/app/views/articles/_form.html.erb
177
+ - spec/dummy/app/views/articles/edit.html.erb
178
+ - spec/dummy/app/views/articles/index.html.erb
179
+ - spec/dummy/app/views/articles/new.html.erb
180
+ - spec/dummy/app/views/articles/show.html.erb
181
+ - spec/dummy/app/views/edit.html.erb
182
+ - spec/dummy/app/views/index.html.erb
183
+ - spec/dummy/app/views/layouts/application.html.erb
184
+ - spec/dummy/app/views/new.html.erb
185
+ - spec/dummy/app/views/show.html.erb
186
+ - spec/dummy/bin/rails
187
+ - spec/dummy/config.ru
188
+ - spec/dummy/config/application.rb
189
+ - spec/dummy/config/boot.rb
190
+ - spec/dummy/config/database.yml
191
+ - spec/dummy/config/environment.rb
192
+ - spec/dummy/config/environments/development.rb
193
+ - spec/dummy/config/environments/production.rb
194
+ - spec/dummy/config/environments/test.rb
195
+ - spec/dummy/config/initializers/backtrace_silencers.rb
196
+ - spec/dummy/config/initializers/inflections.rb
197
+ - spec/dummy/config/initializers/mime_types.rb
198
+ - spec/dummy/config/initializers/secret_token.rb
199
+ - spec/dummy/config/initializers/session_store.rb
200
+ - spec/dummy/config/locales/arr.yml
201
+ - spec/dummy/config/locales/en.yml
202
+ - spec/dummy/config/locales/http.yml
203
+ - spec/dummy/config/locales/user.yml
204
+ - spec/dummy/config/mongoid.yml
205
+ - spec/dummy/config/routes.rb
206
+ - spec/dummy/db/migrate/20121019115657_create_posts.rb
207
+ - spec/dummy/db/schema.rb
208
+ - spec/dummy/db/seeds.rb
209
+ - spec/dummy/lib/tasks/test.rake
210
+ - spec/dummy/log/.gitkeep
211
+ - spec/dummy/public/404.html
212
+ - spec/dummy/public/422.html
213
+ - spec/dummy/public/500.html
214
+ - spec/dummy/public/favicon.ico
215
+ - spec/dummy/script/rails
216
+ - spec/locale_setter/controller_spec.rb
217
+ - spec/locale_setter/domain_spec.rb
218
+ - spec/locale_setter/generic_spec.rb
219
+ - spec/locale_setter/http_spec.rb
220
+ - spec/locale_setter/locale_setter_spec.rb
221
+ - spec/locale_setter/matcher_spec.rb
222
+ - spec/locale_setter/user_spec.rb
223
+ - spec/request_helper.rb
224
+ - spec/requests/request_spec.rb
99
225
  - spec/spec_helper.rb
226
+ - spec/support/dummy_app.rb
100
227
  - spec/support/i18n.rb
228
+ - spec/support/matchers/have_text.rb
229
+ has_rdoc: