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
@@ -0,0 +1,31 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Configure static asset server for tests with Cache-Control for performance
11
+ # config.serve_static_assets = true
12
+ # config.static_cache_control = "public, max-age=3600"
13
+
14
+ # Show full error reports and disable caching
15
+ config.consider_all_requests_local = true
16
+ config.action_controller.perform_caching = false
17
+
18
+ # Raise exceptions instead of rendering exception templates
19
+ config.action_dispatch.show_exceptions = false
20
+
21
+ # Disable request forgery protection in test environment
22
+ config.action_controller.allow_forgery_protection = false
23
+
24
+ # Raise exception on mass assignment protection for Active Record models
25
+ # config.active_record.mass_assignment_sanitizer = :strict
26
+
27
+ # Print deprecation notices to the stderr
28
+ config.active_support.deprecation = :stderr
29
+
30
+ config.eager_load = false
31
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_key_base = 'c2e3474d3816f60bf6dd0f3b983e7283c7ff5373e11a96935340b544a31964dbe5ee077136165ee2975e0005f5e80207c0059e6d5589699031242ba5a06dcb87'
8
+ Dummy::Application.config.secret_token = 'c2e3474d3816f60bf6dd0f3b983e7283c7ff5373e11a96935340b544a31964dbe5ee077136165ee2975e0005f5e80207c0059e6d5589699031242ba5a06dcb87'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,2 @@
1
+ arr:
2
+ title: "A'hoy, mate!"
@@ -0,0 +1,2 @@
1
+ en:
2
+ title: "Hello, World!"
@@ -0,0 +1,2 @@
1
+ http:
2
+ title: "Hello, HTTP!"
@@ -0,0 +1,2 @@
1
+ user:
2
+ title: "Hello, User!"
@@ -0,0 +1,80 @@
1
+ development:
2
+ # Configure available database sessions. (required)
3
+ sessions:
4
+ # Defines the default session. (required)
5
+ default:
6
+ # Defines the name of the default database that Mongoid can connect to.
7
+ # (required).
8
+ database: dummy_development
9
+ # Provides the hosts the default session can connect to. Must be an array
10
+ # of host:port pairs. (required)
11
+ hosts:
12
+ - localhost:27017
13
+ options:
14
+ # Change whether the session persists in safe mode by default.
15
+ # (default: false)
16
+ # safe: false
17
+
18
+ # Change the default consistency model to :eventual or :strong.
19
+ # :eventual will send reads to secondaries, :strong sends everything
20
+ # to master. (default: :eventual)
21
+ # consistency: :eventual
22
+
23
+ # How many times Moped should attempt to retry an operation after
24
+ # failure. (default: 30)
25
+ # max_retries: 30
26
+
27
+ # The time in seconds that Moped should wait before retrying an
28
+ # operation on failure. (default: 1)
29
+ # retry_interval: 1
30
+ # Configure Mongoid specific options. (optional)
31
+ options:
32
+ # Configuration for whether or not to allow access to fields that do
33
+ # not have a field definition on the model. (default: true)
34
+ # allow_dynamic_fields: true
35
+
36
+ # Enable the identity map, needed for eager loading. (default: false)
37
+ # identity_map_enabled: false
38
+
39
+ # Includes the root model name in json serialization. (default: false)
40
+ # include_root_in_json: false
41
+
42
+ # Include the _type field in serializaion. (default: false)
43
+ # include_type_for_serialization: false
44
+
45
+ # Preload all models in development, needed when models use
46
+ # inheritance. (default: false)
47
+ # preload_models: false
48
+
49
+ # Protect id and type from mass assignment. (default: true)
50
+ # protect_sensitive_fields: true
51
+
52
+ # Raise an error when performing a #find and the document is not found.
53
+ # (default: true)
54
+ # raise_not_found_error: true
55
+
56
+ # Raise an error when defining a scope with the same name as an
57
+ # existing method. (default: false)
58
+ # scope_overwrite_exception: false
59
+
60
+ # Skip the database version check, used when connecting to a db without
61
+ # admin access. (default: false)
62
+ # skip_version_check: false
63
+
64
+ # User Active Support's time zone in conversions. (default: true)
65
+ # use_activesupport_time_zone: true
66
+
67
+ # Ensure all times are UTC in the app side. (default: false)
68
+ # use_utc: false
69
+ test:
70
+ sessions:
71
+ default:
72
+ database: dummy_test
73
+ hosts:
74
+ - localhost:27017
75
+ options:
76
+ consistency: :strong
77
+ # In the test environment we lower the retries and retry interval to
78
+ # low amounts for fast failures.
79
+ max_retries: 1
80
+ retry_interval: 0
@@ -0,0 +1,4 @@
1
+ Dummy::Application.routes.draw do
2
+ resources :users
3
+ resources :articles
4
+ end
@@ -0,0 +1,8 @@
1
+ class CreatePosts < ActiveRecord::Migration
2
+ def change
3
+ create_table :posts do |t|
4
+
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20121019224754) do
15
+
16
+ create_table "articles", :force => true do |t|
17
+ t.string "title"
18
+ t.text "body"
19
+ t.datetime "created_at", :null => false
20
+ t.datetime "updated_at", :null => false
21
+ end
22
+
23
+ create_table "users", :force => true do |t|
24
+ t.string "name"
25
+ t.string "locale"
26
+ t.datetime "created_at", :null => false
27
+ t.datetime "updated_at", :null => false
28
+ end
29
+
30
+ end
@@ -0,0 +1 @@
1
+ User.create(:name => "Sample User", :locale => "user")
@@ -0,0 +1,16 @@
1
+ require 'rake/testtask'
2
+ require 'rspec/core/rake_task'
3
+
4
+ Rake::Task[:test].clear
5
+ Rake::TestTask.new :test do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
9
+
10
+ RSpec::Core::RakeTask.new :spec
11
+
12
+ RSpec::Core::RakeTask.new :fast_spec do |t|
13
+ t.pattern = "fast_spec/**/*_spec.rb"
14
+ end
15
+
16
+ task :default => [:test, :spec, :fast_spec]
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe LocaleSetter::Rails do
3
+ describe LocaleSetter::Controller do
4
4
  it "exists" do
5
- expect{ LocaleSetter::Rails }.to_not raise_error
5
+ expect{ LocaleSetter::Controller }.to_not raise_error
6
6
  end
7
7
 
8
8
  class BareController
@@ -12,16 +12,16 @@ describe LocaleSetter::Rails do
12
12
  describe ".included" do
13
13
  it "sets a before filter" do
14
14
  BareController.should_receive(:before_filter).with(:set_locale)
15
- BareController.send(:include, LocaleSetter::Rails)
15
+ BareController.send(:include, LocaleSetter::Controller)
16
16
  end
17
17
 
18
18
  it "skips setting the before_filter if not supported" do
19
- expect{ BareController.send(:include, LocaleSetter::Rails) }.to_not raise_error
19
+ expect{ BareController.send(:include, LocaleSetter::Controller) }.to_not raise_error
20
20
  end
21
21
  end
22
22
 
23
23
  class Controller < BareController
24
- include LocaleSetter::Rails
24
+ include LocaleSetter::Controller
25
25
  end
26
26
 
27
27
  let(:controller){ Controller.new }
@@ -60,4 +60,4 @@ describe LocaleSetter::Rails do
60
60
  controller.default_url_options({})[:locale].should == :sample
61
61
  end
62
62
  end
63
- end
63
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe LocaleSetter::Domain do
4
+ describe '#for' do
5
+ before { described_class.localized_domains = { "my_domain.com" => "domain_locale" }}
6
+
7
+ it "ignores a blank domain" do
8
+ expect(described_class.for("", ["domain_locale"])).not_to be
9
+ end
10
+
11
+ it "ignores domains not from the list" do
12
+ expect(described_class.for("my_domain.com", ["default"])).not_to be
13
+ end
14
+
15
+ it "detects right domain" do
16
+ expect(described_class.for("my_domain.com", ["domain_locale"])).to eq(:domain_locale)
17
+ end
18
+ end
19
+ end
@@ -8,7 +8,7 @@ describe LocaleSetter::Generic do
8
8
  context "with nothing" do
9
9
  it "uses the default" do
10
10
  setter.set_locale(i18n)
11
- i18n.locale.should == i18n.default_locale
11
+ expect(i18n.locale).to eq(i18n.default_locale)
12
12
  end
13
13
  end
14
14
 
@@ -18,18 +18,18 @@ describe LocaleSetter::Generic do
18
18
  it "makes use of the HTTP headers" do
19
19
  i18n.available_locales = [:en, :es]
20
20
  setter.set_locale(i18n, {:env => request})
21
- i18n.locale.should == :es
21
+ expect(i18n.locale).to eq(:es)
22
22
  end
23
23
 
24
24
  it "only sets an available locale" do
25
25
  i18n.available_locales = [:arr, :en]
26
26
  setter.set_locale(i18n, {:env => request})
27
- i18n.locale.should == :en
27
+ expect(i18n.locale).to eq(:en)
28
28
  end
29
29
 
30
30
  it "does nothing when HTTP_ACCEPT_LANGUAGE is missing" do
31
31
  setter.set_locale(i18n, {:env => {}})
32
- i18n.locale.should == i18n.default_locale
32
+ expect(i18n.locale).to eq(i18n.default_locale)
33
33
  end
34
34
  end
35
35
 
@@ -38,7 +38,16 @@ describe LocaleSetter::Generic do
38
38
  i18n.available_locales = [:en, :user_specified]
39
39
  user = OpenStruct.new( {:locale => :user_specified} )
40
40
  setter.set_locale(i18n, {:user => user} )
41
- i18n.locale.should == :user_specified
41
+ expect(i18n.locale).to eq(:user_specified)
42
+ end
43
+ end
44
+
45
+ context "with localized domains" do
46
+ it "uses the locale from config" do
47
+ i18n.available_locales = [:en, :domain_specified]
48
+ LocaleSetter::Domain.localized_domains = { "localized_domain.com" => :domain_specified }
49
+ setter.set_locale(i18n, {:domain => "localized_domain.com"} )
50
+ expect(i18n.locale).to eq(:domain_specified)
42
51
  end
43
52
  end
44
53
 
@@ -51,13 +60,13 @@ describe LocaleSetter::Generic do
51
60
 
52
61
  it "uses the URL parameter" do
53
62
  setter.set_locale(i18n, {:params => params} )
54
- i18n.locale.should == :param_specified
63
+ expect(i18n.locale).to eq(:param_specified)
55
64
  end
56
65
 
57
66
  it "only allows supported locales" do
58
67
  i18n.available_locales = [:en]
59
68
  setter.set_locale(i18n, {:params => params} )
60
- i18n.locale.should == i18n.default_locale
69
+ expect(i18n.locale).to eq(i18n.default_locale)
61
70
  end
62
71
 
63
72
  it "does not pollute the symbol table when given an unsuported locale" do