Skip to content

Commit

Permalink
Merge pull request #21 from justCxx/exercise-16
Browse files Browse the repository at this point in the history
Complete Exercise 16 (I18n)
  • Loading branch information
v-kolesnikov committed Aug 24, 2015
2 parents df08796 + 488b95b commit 920f1e7
Show file tree
Hide file tree
Showing 34 changed files with 277 additions and 72 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ gem "aws-sdk"
gem "damerau-levenshtein"
# Cron jobs
gem "whenever", require: false
# Parse the Accept-Language header
gem "http_accept_language"
# locale data
gem "rails-i18n", "~> 4.0.4"

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger cli
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ GEM
multi_json (~> 1.8)
http-cookie (1.0.2)
domain_name (~> 0.5)
http_accept_language (2.0.5)
http_parser.rb (0.6.0)
i18n (0.7.0)
jbuilder (2.3.1)
Expand Down Expand Up @@ -202,6 +203,9 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
rails-i18n (4.0.4)
i18n (~> 0.6)
railties (~> 4.0)
rails_12factor (0.0.3)
rails_serve_static_assets
rails_stdout_logging
Expand Down Expand Up @@ -320,12 +324,14 @@ DEPENDENCIES
fuubar
guard
guard-livereload
http_accept_language
jbuilder (~> 2.0)
jquery-rails
nokogiri
paperclip
pg
rails (= 4.2.3)
rails-i18n (~> 4.0.4)
rails_12factor
rspec-rails
sass-rails (~> 5.0)
Expand Down
23 changes: 21 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :require_login
before_action :require_login, :set_locale

def not_authenticated
redirect_to login_url, alert: "First login to access this page!"
redirect_to login_url, alert: (t :require_login)
end

def set_locale
locale = detect_locale
if locale && I18n.available_locales.include?(locale.to_sym)
I18n.locale = locale.to_sym
end
end

private

def detect_locale
if current_user
current_user.locale
elsif params[:locale]
params[:locale]
else
http_accept_language.compatible_language_from(I18n.available_locales)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def create
private

def user_params
params.require(:user).permit(:email, :password)
params.require(:user).permit(:email, :password, :locale)
end
end
14 changes: 8 additions & 6 deletions app/controllers/reviews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ def create
review = @card.review(review_params[:answer])

if review[:success]
flash[:success] = "Right! Original: #{@card.original_text}, " \
"translated: #{@card.translated_text}. " \
"Your answer: #{review_params[:answer]}. " \
"Typos: #{review[:typos]}. " \
"Next review: #{@card.review_date.localtime}"
flash[:success] = t("review_success",
original: @card.original_text,
translated: @card.translated_text,
user_answer: review_params[:answer],
typos: review[:typos],
next: @card.review_date.localtime
)
else
flash[:danger] = "Wrong! Next review: #{@card.review_date.localtime}"
flash[:danger] = "#{t('review_wrong', next_review: @card.review_date)}"
end
redirect_to new_review_path
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/user_profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def destroy
private

def user_params
params.require(:user).permit(:email, :password)
params.require(:user).permit(:email, :password, :locale)
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: ENV["SMTP_USER"]
default from: "oh-my-flashcards <#{ENV['SMTP_USER']}>"
layout "mailer"
end
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ class User < ActiveRecord::Base
accepts_nested_attributes_for :authentications

validates :email, presence: true, uniqueness: true
validates :locale, presence: true
validates :password, length: { minimum: 6 }, if: :new_record?

before_validation :set_locale, if: :new_record?

def cards
Card.where(deck_id: decks)
end
Expand All @@ -22,4 +25,10 @@ def self.notify_review
NotificationsMailer.pending_cards(user).deliver_later
end
end

private

def set_locale
self.locale = I18n.default_locale
end
end
4 changes: 2 additions & 2 deletions app/views/cards/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
</div>
<% end %>
<%= f.input :deck_id, collection: current_user.decks, label: "Select deck" %>
<%= f.input :deck_id, collection: current_user.decks, label: (t :select_deck) %>

<p><b>Or creare new:</b></p>
<p><b><%= t :create_deck %>:</b></p>

<%= simple_fields_for Deck.new do |f_deck| %>
<%= f_deck.input :title %>
Expand Down
20 changes: 10 additions & 10 deletions app/views/cards/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<h3>All cards</h3>
<h3><%= t :cards %> (<%= @cards.count %>)</h3>

<table width="100%">
<tr>
<th>Original</th>
<th>Translated</th>
<th>Image</th>
<th>Review</th>
<th>Correct answers</th>
<th>Incorrect answers</th>
<th>Deck</th>
<th><%= Card.human_attribute_name(:original_text) %></th>
<th><%= Card.human_attribute_name(:translated_text) %></th>
<th><%= Card.human_attribute_name(:image) %></th>
<th><%= Card.human_attribute_name(:review_date) %></th>
<th><%= Card.human_attribute_name(:correct_answers) %></th>
<th><%= Card.human_attribute_name(:incorrect_answers) %></th>
<th><%= Deck.human_attribute_name(:title) %></th>
</tr>

<% @cards.each do |card| %>
Expand All @@ -20,8 +20,8 @@
<td><%= card.correct_answers %></td>
<td><%= card.incorrect_answers %></td>
<td><%= card.deck.title %></td>
<td><%= link_to "Edit", edit_card_path(card) %></td>
<td><%= link_to "Remove", card_path(card), method: :delete %></td>
<td><%= link_to t(:edit), edit_card_path(card) %></td>
<td><%= link_to t(:destroy), card_path(card), method: :delete %></td>
</tr>
<% end %>
</table>
12 changes: 6 additions & 6 deletions app/views/decks/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<h3>Колоды (<%= @decks.count %>)</h3>
<h3><%= t :decks %> (<%= @decks.count %>)</h3>

<table width="100%">
<tr>
<th>Название</th>
<th>Карточек</th>
<th>Для просмотра</th>
<th><%= Deck.human_attribute_name(:title) %></th>
<th><%= t :cards %></th>
<th><%= t :for_review %></th>
</tr>

<% @decks.each do |deck| %>
<tr>
<td><%= link_to deck.title, deck_path(deck) %></td>
<td><%= deck.cards.count %></td>
<td><%= deck.cards.for_review.count %></td>
<td><%= link_to 'Редактировать', edit_deck_path(deck) %></td>
<td><%= link_to 'Удалить', deck_path(deck), method: :delete %></td>
<td><%= link_to t(:edit), edit_deck_path(deck) %></td>
<td><%= link_to t(:destroy), deck_path(deck), method: :delete %></td>
</tr>
<% end %>
</table>
2 changes: 1 addition & 1 deletion app/views/decks/new.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render 'form', deck: @deck %>
<%= render "form", deck: @deck %>
22 changes: 12 additions & 10 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,37 @@
<section id="container">

<header class="clearfix">
<h1>Flashcards</h1>
<h1><%= t :app_name %></h1>
</header>

<nav class="navbar navbar-default">
<a class="navbar-brand" href="/">FlashCard</a>
<a class="navbar-brand" href="/"> <%= t :home %> </a>

<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><%= link_to "Decks", decks_path %></li>
<li><%= link_to "Cards", cards_path %></li>
<li><%= link_to "Add deck", new_deck_path %></li>
<li><%= link_to "Add card", new_card_path%></li>
<li><%= link_to t(:decks), decks_path %></li>
<li><%= link_to t(:cards), cards_path %></li>
<li><%= link_to t(:add_card), new_card_path%></li>
</ul>

<ul class="nav navbar-nav navbar-right">
<%= t :locale %>:
<%= I18n.locale %>
<% if logged_in? %>
<%= link_to "Logout", logout_path, class: "btn btn-default navbar-btn" %>
<%= link_to t(:logout), logout_path, class: "btn btn-default navbar-btn" %>
<p class="navbar-text">
Signed in as <%= link_to current_user.email, profile_path %>
<%= t :signed_as %>:
<%= link_to current_user.email, profile_path %>
</p>
<% else %>
<%= link_to "Sign Up", signup_path, class: "btn btn-default navbar-btn" %>
<%= link_to t(:sign_up), signup_path, class: "btn btn-default navbar-btn" %>
<% end %>
</ul>
</div>
</nav>

<p style="font-size: 22px;">
The world's first user-friendly manager flashcards. Exactly.
<%= t :app_descr %>
</p>

<% flash.each do |type, message| %>
Expand Down
1 change: 1 addition & 0 deletions app/views/registration/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<%= f.input :email, as: :string %>
<%= f.input :password %>
<%= f.input :locale, collection: I18n.available_locales %>
<%= f.button :submit %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/registration/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h1>Sign Up!</h1>

<%= render 'form', user: @user %>
<%= render "form", user: @user %>
4 changes: 2 additions & 2 deletions app/views/reviews/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<%= image_tag card.image.url(:medium) %>
<p><%= card.translated_text %></p>
</div>
<%= f.input :answer %>
<%= f.button :submit, "Проверить" %>
<%= f.input :answer, label: (t :review_answer) %>
<%= f.button :submit, (t :review_check) %>
<% end %>
1 change: 1 addition & 0 deletions app/views/user_profile/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<%= f.input :email, as: :string %>
<%= f.input :password %>
<%= f.input :locale, collection: I18n.available_locales %>
<%= f.button :submit %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/user_profile/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h3>Edit profile</h3>
<h3><%= t :user_profile %></h3>

<%= render "form" %>
Expand Down
15 changes: 8 additions & 7 deletions app/views/user_profile/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<h3>User profile</h3>
<h3><%= t :user_profile %></h3>

<p>E-mail: <%= current_user.email %></p>
<p>Decks: <%= current_user.decks.count %></p>
<p>Default deck:
<p><%= t :email %>: <%= current_user.email %></p>
<p><%= t :decks %>: <%= current_user.decks.count %></p>
<p><%= User.human_attribute_name(:default_deck) %>:
<% if current_user.default_deck %>
<%= current_user.default_deck.title %>
<% else %>
no default deck
<%= t :no_default_deck %>
<% end %>
<p><%= t :locale %>: <%= current_user.locale %></p>
</p>


<%= link_to "Edit", edit_profile_path %> |
<%= link_to "Destroy", profile_path, method: :delete %>
<%= link_to t(:edit), edit_profile_path %> |
<%= link_to t(:destroy), profile_path, method: :delete %>
2 changes: 1 addition & 1 deletion app/views/user_sessions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= simple_form_for :user, url: user_sessions_path do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.button :submit, "Sign In" %>
<%= f.button :submit, (t :signin) %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/user_sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>Login</h1>
<h1><%= t :login %></h1>

<%= render "form" %>
Expand Down
17 changes: 11 additions & 6 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

module Flashcard
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Settings in config/environments take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

Expand All @@ -29,13 +29,18 @@ class Application < Rails::Application
host: ENV["APP_HOST"]
}

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# Set Time.zone default to the specified zone and make Active Record
# auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
# Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# The default locale is :en and all translations from
# config/locales/*.rb,yml are auto loaded.
# locales_path = Rails.root.join("config", "locales", "**", "*.{rb,yml}").to_s
# config.i18n.load_path += Dir[locales_path]

config.i18n.available_locales = [:en, :ru]

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
Expand Down

0 comments on commit 920f1e7

Please sign in to comment.