Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1547] Add and use Current.locale #1623

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0ce23c5
Set Current.locale in set_current_locale before action
veganstraightedge May 25, 2020
50abf66
Use Current.locale in app controller before action
veganstraightedge May 25, 2020
c11637f
Delete unused Locale.current method
veganstraightedge May 25, 2020
bd6dd1f
Split extract redirect_to_locale_subdomain from set_current_locale_fr…
veganstraightedge May 25, 2020
5380968
Move set_current_theme before action to a middleware
veganstraightedge May 25, 2020
8629ff5
Move current locale methods to CurrentLocale middleware
veganstraightedge May 25, 2020
a8c9ac3
Use Current.locale in header link to language page
veganstraightedge May 25, 2020
e97da9b
Use Current.locale in 2020 homepage footer link to lite mode
veganstraightedge May 25, 2020
3e6ed68
Use Current.locale in 2017 footer link to lite mode
veganstraightedge May 25, 2020
f86aa40
Use Current.locale in language page subdomain switcher
veganstraightedge May 25, 2020
f868cb6
Use Current.locale in #preferred_localization
veganstraightedge May 25, 2020
c36db45
Delete Locale.english?
veganstraightedge May 25, 2020
edf4de4
Use Current.locale in TagsHelper
veganstraightedge May 25, 2020
147c4bc
Use Current.locale in MarkdownHelper
veganstraightedge May 25, 2020
0f90ac5
Use Current.locale in locale_nav_item_classes_for (2020 layout)
veganstraightedge May 25, 2020
edb00d6
Set Current.theme in spec_helper
veganstraightedge May 26, 2020
17477cd
Set Current.locale before each spec
veganstraightedge May 26, 2020
cea8506
Set Current.locale before each spec in spec helper
veganstraightedge May 26, 2020
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
28 changes: 0 additions & 28 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :set_current_locale
before_action :set_current_theme

before_action :set_site_locale
before_action :check_for_redirection
before_action :strip_file_extension
before_action :authorize, if: :staging?
Expand Down Expand Up @@ -46,25 +42,6 @@ def authorize
redirect_to [:signin], alert: 'You need to sign in to view that page.' unless signed_in?
end

def set_current_locale
locale = request.subdomain
I18n.locale = locale if I18n.available_locales.include?(locale.to_sym)

# Force the subdomain to match the locale.
# Don’t do this in development, because typically local development
# environments don’t support subdomains (en.localhost doesn’t resolve).
if (I18n.locale != I18n.default_locale) && # Don’t redirect to en.crimethinc.com
request.subdomain.empty? && # Don’t redirect if there’s a subdomain
Rails.env.production? # Don’t redirect in development

redirect_to({ subdomain: I18n.locale }.merge(params.permit!))
end
end

def set_current_theme
Current.theme = ENV.fetch('THEME') { '2017' }
end

def check_for_redirection
redirect = Redirect.where(source_path: request.path.downcase).last
redirect = Redirect.where(source_path: "#{request.path.downcase}/").last if redirect.blank?
Expand Down Expand Up @@ -146,10 +123,5 @@ def page_share_image_url
# TODO: implement this algorithm
'https://cloudfront.crimethinc.com/assets/share/crimethinc-site-share.png'
end

def set_site_locale
@site_locale = LocaleService.find(locale: nil, lang_code: I18n.locale)
end

# ...Page Share
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class StealSomethingFromWorkDayController < ApplicationController
def show
# Remove current locale from language switcher in the view
@locales = STEAL_SOMETHING_FROM_WORK_DAY_LOCALES.dup
@locales.delete I18n.locale
@locales.delete Current.locale

@sections = [
'Introduction',
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/locales_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def locale abbreviation
def locale_nav_item_classes_for locale
classes = []
classes << locale.abbreviation
classes << 'current' if I18n.locale.to_s == locale.abbreviation
classes << 'current' if Current.locale == locale
classes
end
end
9 changes: 8 additions & 1 deletion app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module MarkdownHelper
def render_markdown_for page:
content = File.read [Rails.root, "config/locales/pages/#{I18n.locale}", "#{page}.markdown"].join('/')
content = File.read [
Rails.root,
'config',
'locales',
'pages',
Current.locale.abbreviation,
"#{page}.markdown"
].join('/')

render_markdown content
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/tags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module TagsHelper
attr_accessor :html_id

def html_dir
article_locale&.language_direction.presence || t('language_direction')
article_locale&.language_direction.presence || Current.locale.language_direction
end

def html_lang
article_locale&.abbreviation.presence || I18n.locale
article_locale&.abbreviation.presence || Current.locale.abbreviation
end

def html_prefix
Expand Down
86 changes: 86 additions & 0 deletions app/middlewares/rack/current_locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module Rack
class CurrentLocale
def initialize app
@app = app
end

def call env
@req = Rack::Request.new(env)

set_current_locale
set_current_locale_from_subdomain
redirect_to_locale_subdomain

@app.call(env)
end

private

def set_current_locale
Current.locale = ::Locale.find_by abbreviation: I18n.locale
end

Comment on lines +19 to +22
Copy link
Contributor

@astronaut-wannabe astronaut-wannabe May 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is why the tests are failing.

You are making it so that the routing logic now depends on the locale database tables to exist an be populated

this will be nil in the specs because there are no locales, and that will make line 36 fail.

return if Current.locale.english?

you can get the specs passing by putting before { create(:locale, :en) } at the top of the failing specs, but this brings up a question for me

should middleware routing be tied to the state of the DB? and does making a database call outside of application code introduce any risks (security or otherwise)

I am not familiar enough with rack to really know, but if it's fine then adding the before in failing specs should do the trick

def set_current_locale_from_subdomain
return if subdomain.blank?
return unless I18n.available_locales.include? subdomain.to_sym

Current.locale = ::Locale.find_by abbreviation: subdomain
end

def redirect_to_locale_subdomain
# Force the subdomain to match the locale.
# Don’t do this in development, because typically local development
# environments don’t support subdomains (en.localhost doesn’t resolve).

# Don’t redirect to en.crimethinc.com
return if Current.locale.english?

# Don’t redirect if there’s a subdomain
return if subdomain.present?

# Don’t redirect in development
return if Rails.env.production?

redirect localized_url
end

def subdomain
fully_qualified_domain = @req.host
locale_subdomain = fully_qualified_domain.split('.').first

locale_subdomain unless locale_subdomain == fully_qualified_domain
end

def decorated_subdomain
"#{subdomain}." unless subdomain.blank?
end

def decorated_port
":#{@req.port}" if Rails.env.development?
end

def decorated_query_params
"?#{@req.query_string}" if @req.query_string.present?
end

def localized_url
[
@req.scheme,
'://',
decorated_subdomain,
@req.host,
decorated_port,
@req.path,
decorated_query_params
].join
end

def redirect location
[
301,
{ 'Location' => location, 'Content-Type' => 'text/html' },
['Moved Permanently']
]
end
end
end
13 changes: 13 additions & 0 deletions app/middlewares/rack/current_theme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Rack
class CurrentTheme
def initialize app
@app = app
end

def call env
Current.theme = ENV.fetch('THEME') { '2017' }

@app.call(env)
end
end
end
6 changes: 3 additions & 3 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ def localizations

def localization_in locale
[
Article.published.live.find_by(locale: locale, canonical_id: id),
Article.published.live.find_by(locale: locale, id: canonical_id)
Article.published.live.find_by(locale: locale.abbreviation, canonical_id: id),
Article.published.live.find_by(locale: locale.abbreviation, id: canonical_id)
].compact.first
end

def preferred_localization
localization_in(I18n.locale).presence || self
localization_in(Current.locale).presence || self
end

def canonical_article
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def id_and_name
end

def preferred_localization
localization_in(I18n.locale).presence || self
localization_in(Current.locale).presence || self
end

def localization_in locale
self.class.published.live.where(locale: locale).where('id = ? OR canonical_id = ?', canonical_id, id).limit(1).first
self.class.published.live.where(locale: locale.abbreviation).where('id = ? OR canonical_id = ?', canonical_id, id).limit(1).first
end

def localizations
Expand Down
2 changes: 1 addition & 1 deletion app/models/current.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user, :theme
attribute :user, :locale, :theme
end
8 changes: 0 additions & 8 deletions app/models/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ class Locale < ApplicationRecord
default_scope { order(abbreviation: :asc) }

class << self
def current
I18n.locale
end

def english?
I18n.locale == :en
end

def live
Locale.unscoped.order(articles_count: :desc, abbreviation: :asc).where.not(articles_count: 0)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/2017/languages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</nav>

<div class="subdomain-switcher">
<% unless @locale.abbreviation == I18n.locale.to_s %>
<% unless Current.locale == @locale %>
<%= render_markdown t("views.languages.view_site_in_locale", locale: @locale.abbreviation) %>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion app/views/2017/shared/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= render_themed "shared/footer/contact" %>
</div>

<% if media_mode? && I18n.locale == :en %>
<% if media_mode? && Current.locale.english? %>
<p class="site-mode-link-container">
<%= link_to(t("footer.site_mode"), url_for_current_path_with_subdomain(subdomain: :lite)) %>
</p>
Expand Down
4 changes: 2 additions & 2 deletions app/views/2017/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<li class="nav-link"><%= link_to t("header.about"), "/about" %></li>
</ul>

<% unless Locale.english? %>
<% unless Current.locale.english? %>
<ul>
<li class="nav-link"><%= link_to t('header.locale_articles'), language_path(@site_locale.canonical) %></li>
<li class="nav-link"><%= link_to t('header.locale_articles'), language_path(Current.locale.name) %></li>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is the source of the test failures, but canonical == slug

Suggested change
<li class="nav-link"><%= link_to t('header.locale_articles'), language_path(Current.locale.name) %></li>
<li class="nav-link"><%= link_to t('header.locale_articles'), language_path(Current.locale.slug) %></li>

</ul>
<% end %>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion app/views/2020/shared/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
</div><!-- .column column-25 -->
</div><!-- .row -->

<% if media_mode? && I18n.locale == :en %>
<% if media_mode? && Current.locale.english? %>
<p class='site-mode-link-container'>
<%= link_to(t('footer.site_mode'), url_for_current_path_with_subdomain(subdomain: :lite)) %>
</p>
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require_relative '../app/middlewares/rack/pic_twitter_redirect'
require_relative '../app/middlewares/rack/redirect'
require_relative '../app/middlewares/rack/teapot'
require_relative '../app/middlewares/rack/current_locale'
require_relative '../app/middlewares/rack/current_theme'
require 'rack/contrib'

# Require the gems listed in Gemfile, including any gems
Expand All @@ -25,6 +27,8 @@ class Application < Rails::Application
config.middleware.use Rack::Redirect
config.middleware.use Rack::Attack
config.middleware.use Rack::Locale
config.middleware.use Rack::CurrentLocale
config.middleware.use Rack::CurrentTheme
config.middleware.insert_after ActionDispatch::Static, Rack::Deflater

# Initialize configuration defaults for originally generated Rails version.
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
driven_by :selenium_headless, using: :firefox
end

config.before do
Current.locale = FactoryBot.build_stubbed :locale
Current.theme = '2017'
end

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
Expand Down