Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ gem "webpacker"
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
gem "coffee-rails"
# Use rack-rewrite to ensure default locale is always used
gem "rack-rewrite", "~> 1.5.1"

# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem "turbolinks", "~> 5"
Expand Down
4 changes: 3 additions & 1 deletion rails/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ GEM
rack
rack-proxy (0.6.5)
rack
rack-rewrite (1.5.1)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.3)
Expand Down Expand Up @@ -306,6 +307,7 @@ DEPENDENCIES
pg (>= 0.18, < 2.0)
pry
puma (~> 3.11)
rack-rewrite (~> 1.5.1)
rails (~> 5.2.3)
rails-i18n
rspec-rails
Expand All @@ -329,4 +331,4 @@ RUBY VERSION
ruby 2.6.3p62

BUNDLED WITH
2.0.1
2.0.2
4 changes: 4 additions & 0 deletions rails/app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ $highlight-text: #C06D19;
font-size: 1.8rem;
}

#active-lang {
color: $white;
}

// Buttons //

a.btn {
Expand Down
22 changes: 12 additions & 10 deletions rails/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!

before_action :set_locale
around_action :set_locale

def set_locale
I18n.locale = I18n.default_locale
locale = params[:locale].to_s.strip.to_sym
protected

I18n.locale = locale if I18n.available_locales.include?(locale)
def set_locale(&action)
locale = params[:locale].to_s.strip.to_sym
if (!I18n.available_locales.include?(locale))
locale = I18n.default_locale
end
I18n.with_locale(locale, &action)
end

# def default_url_options(options = {})
# options.merge(locale: I18n.locale)
# end

protected
def default_url_options(options = {})
# options.merge(locale: I18n.locale)
{ locale: I18n.locale == I18n.default_locale ? I18n.default_locale : I18n.locale }
end

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: %i[name email password password_confirmation])
Expand Down
4 changes: 2 additions & 2 deletions rails/app/views/dives/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1><%=t '.title' %></h1>
<h1><%= t('.title') %></h1>

<p><%=t '.checklist_preface' %></p>
<p><%= t('.checklist_preface') %></p>

<%= simple_form_for dive do |f| %>
<%= f.input :i_have_been_responsible, as: :boolean %>
Expand Down
4 changes: 2 additions & 2 deletions rails/app/views/dives/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1><%=t '.title', dive_id: dive.id %></h1>
<h1><%= t('.title', dive_id: dive.id) %></h1>

<%- if dive.finished? %>
<%=t '.finished_at_log_message', finished_at: dive.finished_at.to_formatted_s(:db) %>
<%= t('.finished_at_log_message', finished_at: dive.finished_at.to_formatted_s(:db)) %>
<%- else %>


Expand Down
1 change: 1 addition & 0 deletions rails/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<%= link_to t('.logout'), destroy_user_session_path, method: :delete, data: { confirm: 'Please confirm you wish to log out.' }, :class =>"nav-link" %>
</li>
<% end %>
<%= render 'shared/locale_links' %>
</nav>
</header>
</div> <%# End navbar %>
Expand Down
10 changes: 10 additions & 0 deletions rails/app/views/shared/_locale_links.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<li class="nav-item">
Language:&nbsp;
<% locale_links = I18n.available_locales.map do |locale|
<<-ENDHTML
#{link_to locale.to_s.upcase, request.parameters.merge(locale: locale.to_s), className: "nav-link", id: locale.to_s == request.parameters['locale'] ? "active-lang" : ""}

ENDHTML
end %>
<%= raw(locale_links.join('&nbsp;|&nbsp;')) %>
</li>
15 changes: 15 additions & 0 deletions rails/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
require "rack/rewrite"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand All @@ -34,5 +35,19 @@ class Application < Rails::Application

config.i18n.default_locale = :en
config.i18n.available_locales = %i[en fr]

# Rewrite non-locale-specific URLs to use default locale before processing
locales_neg_regex = "#{
config.i18n.available_locales.map{ |l|
"\\/#{l.to_s}$|\\/#{l.to_s}\\/"
}.join("|")
}"
default_locale = config.i18n.default_locale.to_s

config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
rewrite %r{^(https?\:\/\/(?:[\w\.]+)(?::\d+)?)?((?!#{locales_neg_regex})\/[\w\/\?,\-=%+]+)$},
"$1/#{default_locale}/$2"
rewrite '/', "/#{default_locale}/"
end
end
end
1 change: 1 addition & 0 deletions rails/config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fr:
title: "En plongée #%{dive_id}"
finished_at_log_message: "Terminé à %{finished_at}."
finish_button: "Terminer la plongée"
log_restoration: "Enregistrer une activité de restauration"
nursery_tables:
index:
title: "Tables de Pépinière"
Expand Down
44 changes: 21 additions & 23 deletions rails/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
Rails.application.routes.draw do
root to: "home#index"

# User routes
devise_for :users
resources :users, only: %i[index show]
devise_scope :user do
get "sign_in", to: "devise/sessions#new"
get "sign_up", to: "devise/registrations#new"
end
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root to: "home#index"

resources :restoration_activity_log_entries
resources :nursery_tables
resources :zones
resources :dives, only: %i[new create show] do
post :finish, on: :member
resources :restoration_activity_log_entries, only: %i[new create]
end
# User routes
devise_for :users
resources :users, only: %i[index show]
devise_scope :user do
get "sign_in", to: "devise/sessions#new"
get "sign_up", to: "devise/registrations#new"
end

devise_scope :user do
get "sign_in", to: "devise/sessions#new"
get "sign_up", to: "devise/registrations#new"
end
resources :restoration_activity_log_entries
resources :nursery_tables
resources :zones
resources :dives, only: %i[new create show] do
post :finish, on: :member
resources :restoration_activity_log_entries, only: %i[new create]
end

resources :users, only: %i[index show]
devise_scope :user do
get "sign_in", to: "devise/sessions#new"
get "sign_up", to: "devise/registrations#new"
end

scope "(:locale)", locale: /en|fr/ do
root to: "home#index"
resources :users, only: %i[index show]
end
end
8 changes: 8 additions & 0 deletions rails/spec/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Helpers
def locale_path_prefixes
I18n.available_locales.map{ |l| "/#{l}" } << ""
end
def default_locale
I18n.default_locale
end
end
3 changes: 3 additions & 0 deletions rails/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Add additional requires below this line. Rails is not loaded until this point!

require_relative "./support/capybara"
require "./spec/helpers"

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand Down Expand Up @@ -69,4 +70,6 @@
Capybara.server_port = 4000
Capybara.app_host = "http://web:4000"
end

config.include Helpers
end
70 changes: 43 additions & 27 deletions rails/spec/requests/dives_spec.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,72 @@
require "rails_helper"
require "support/authentication"

RSpec.describe "Dives", type: :request do
RSpec.describe "Dives", type: :request, focus: true do
include_context "logged in"
describe "GET show" do
let!(:dive) { FactoryBot.create(:dive) }

it "renders the dive hub" do
get "/dives/#{dive.id}"

expect(response).to be_successful
locale_path_prefixes.each do |locale|
get "#{locale}/dives/#{dive.id}"
expect(response).to be_successful
end
end
end

describe "GET new" do
it "succeeds" do
get "/dives/new"

expect(response).to be_successful
locale_path_prefixes.each do |locale|
get "#{locale}/dives/new"
expect(response).to be_successful
end
end
end

describe "POST create" do
context "given a complete checklist" do
subject do
lambda {
post "/dives", params: { dive: { i_have_been_responsible: 1 } }
lambda { |locale|
post_path = "#{locale}/dives"
post post_path, params: { dive: { i_have_been_responsible: 1 } }
}
end

it "redirects to the dive page" do
subject.call
expect(response).to be_redirect
locale_path_prefixes.each do |locale|
subject.call(locale)
expect(response).to be_redirect

follow_redirect!
expect(response).to be_successful
follow_redirect!
expect(response).to be_successful
end
end

it "creates the dive" do
expect { subject.call }.to change { Dive.count }.by(1)
locale_path_prefixes.each do |locale|
expect { subject.call(locale) }.to change { Dive.count }.by(1)
end
end
end

context "given an incomplete checklist" do
subject do
lambda {
post "/dives", params: { dive: { i_have_been_responsible: nil } }
lambda { |locale|
post "#{locale}/dives", params: { dive: { i_have_been_responsible: nil } }
}
end

it "does not create the dive" do
expect { subject.call }.not_to change(Dive, :count)
locale_path_prefixes.each do |locale|
expect { subject.call(locale) }.not_to change(Dive, :count)
end
end

it "renders an error page" do
subject.call
expect(response.body).to match(/can&#39;t be blank/)
locale_path_prefixes.each do |locale|
subject.call(locale)
expect(response.body).to match(/can&#39;t be blank/)
end
end
end
end
Expand All @@ -64,24 +75,29 @@
let!(:dive) { FactoryBot.create(:dive) }

subject do
lambda {
lambda { |locale|
post "/dives/#{dive.id}/finish"
}
end

it "redirects to the dive page" do
subject.call
expect(response).to be_redirect
locale_path_prefixes.each do |locale|
subject.call(locale)
expect(response).to be_redirect

follow_redirect!
expect(response).to be_successful
follow_redirect!
expect(response).to be_successful
end
end

it "marks the dive as finished" do
subject.call
locale_path_prefixes.each do |locale|
subject.call(locale)

dive.reload
expect(dive.finished_at).not_to be_nil
dive.reload
follow_redirect!
expect(dive.finished_at).not_to be_nil
end
end
end
end
14 changes: 12 additions & 2 deletions rails/spec/requests/home_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
include_context "logged in"
it "can render the root page" do
get "/"

expect(response).to be_successful

sign_out current_user
get "/"

expect(response).to redirect_to("/users/sign_in")
expect(response).to redirect_to("/#{default_locale}/users/sign_in")
end
xit "can render a locale-specific root page" do
locale_path_prefixes.each do |locale|
get "#{locale}/"
expect(response).to be_successful

sign_out current_user
get "#{locale}/"

expect(response).to redirect_to("#{locale}/users/sign_in")
end
end
end
4 changes: 2 additions & 2 deletions rails/spec/requests/nursery_tables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
let!(:zone) { FactoryBot.create(:zone) }

it "renders the nursery table" do
get nursery_tables_path
get nursery_tables_path(locale: default_locale)

expect(response).to be_successful
end

it "allows to create nursery table" do
post nursery_tables_path, params: { nursery_table: { capacity: 24, name: "Blah", zone: zone } }
post nursery_tables_path(locale: default_locale), params: { nursery_table: { capacity: 24, name: "Blah", zone: zone } }

expect(response).to have_http_status(200)
end
Expand Down