Skip to content

Commit

Permalink
Merge pull request oriolgual#14 from oriolgual/feature/globalize
Browse files Browse the repository at this point in the history
I18n
  • Loading branch information
oriolgual committed Jan 29, 2012
2 parents 910f084 + a2a178b commit 323966b
Show file tree
Hide file tree
Showing 45 changed files with 691 additions and 83 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -31,6 +31,7 @@ gem 'fancybox-rails'
gem 'friendly_id', '~> 4.0.0.beta14'
gem 'simple-navigation'
gem 'kaminari'
gem 'globalize3'

group :development do
gem 'rack-webconsole'
Expand Down
44 changes: 23 additions & 21 deletions Gemfile.lock
Expand Up @@ -38,7 +38,7 @@ GEM
addressable (2.2.6)
arel (2.2.1)
builder (3.0.0)
capybara (1.1.1)
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
Expand All @@ -49,7 +49,7 @@ GEM
capybara (>= 1.0.0, < 1.2)
carrierwave (0.5.7)
activesupport (~> 3.0)
childprocess (0.2.2)
childprocess (0.3.0)
ffi (~> 1.0.6)
chunky_png (1.2.4)
coderay (1.0.0)
Expand All @@ -60,17 +60,16 @@ GEM
css-bootstrap-rails (0.0.6)
railties (~> 3.1)
thor (~> 0.14)
cucumber (1.1.0)
cucumber (1.1.4)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.5.0)
gherkin (~> 2.7.1)
json (>= 1.4.6)
term-ansicolor (>= 1.0.6)
cucumber-rails (0.5.2)
capybara (>= 1.0.0.rc1)
cucumber (>= 0.10.5)
nokogiri (>= 1.4.4)
rack-test (>= 0.5.7)
cucumber-rails (1.2.1)
capybara (>= 1.1.2)
cucumber (>= 1.1.3)
nokogiri (>= 1.5.0)
database_cleaner (0.6.7)
diff-lcs (1.1.3)
erubis (2.7.0)
Expand All @@ -79,7 +78,7 @@ GEM
multi_json (~> 1.0)
fancybox-rails (0.1.4)
railties (>= 3.1.0)
ffi (1.0.9)
ffi (1.0.11)
fog (1.1.1)
builder
excon (~> 0.7.4)
Expand All @@ -96,10 +95,13 @@ GEM
formalize-rails (0.0.4)
railties (~> 3.1.0.rc1)
formatador (0.2.1)
friendly_id (4.0.0.beta14)
friendly_id (4.0.0)
fssm (0.2.7)
gherkin (2.5.1)
gherkin (2.7.4)
json (>= 1.4.6)
globalize3 (0.1.0)
activemodel (>= 3.0.0)
activerecord (>= 3.0.0)
growl (1.0.3)
guard (0.7.0)
thor (~> 0.14.6)
Expand Down Expand Up @@ -127,8 +129,7 @@ GEM
jquery-rails (1.0.14)
railties (~> 3.0)
thor (~> 0.14)
json (1.6.4)
json_pure (1.6.1)
json (1.6.5)
kaminari (0.12.4)
rails (>= 3.0.0)
kgio (2.6.0)
Expand Down Expand Up @@ -163,7 +164,7 @@ GEM
method_source (>= 0.6.5)
ruby_parser (>= 2.0.5)
slop (~> 2.1.0)
rack (1.3.5)
rack (1.3.6)
rack-cache (1.1)
rack (>= 0.4)
rack-mount (0.8.3)
Expand Down Expand Up @@ -203,18 +204,18 @@ GEM
ruby-hmac (0.4.0)
ruby_parser (2.3.1)
sexp_processor (~> 3.0)
rubyzip (0.9.4)
rubyzip (0.9.5)
sass (3.1.7)
sass-rails (3.1.2)
actionpack (~> 3.1.0)
railties (~> 3.1.0)
sass (>= 3.1.4)
sprockets (~> 2.0.0)
tilt (~> 1.3.2)
selenium-webdriver (2.7.0)
childprocess (>= 0.2.1)
ffi (>= 1.0.7)
json_pure
selenium-webdriver (2.17.0)
childprocess (>= 0.2.5)
ffi (~> 1.0.9)
multi_json (~> 1.0.4)
rubyzip
sexp_processor (3.0.7)
simple-navigation (3.6.0)
Expand All @@ -237,7 +238,7 @@ GEM
sqlite3 (1.3.4)
subexec (0.1.0)
temple (0.3.4)
term-ansicolor (1.0.6)
term-ansicolor (1.0.7)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
Expand Down Expand Up @@ -273,6 +274,7 @@ DEPENDENCIES
foreman
formalize-rails
friendly_id (~> 4.0.0.beta14)
globalize3
growl
guard
guard-cucumber
Expand Down
1 change: 1 addition & 0 deletions Guardfile
Expand Up @@ -4,6 +4,7 @@ end

guard 'minitest', drb: false, rubygems: false, bundler: false, notify: false do
watch(%r{app/(.+)/(.+)\.rb}) { |m| "spec/#{m[1]}/#{m[2]}_spec.rb" }
watch(%r{lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{spec/(.+)/(.+)_spec\.rb})
watch(%r{spec/spec_helper.rb}) { "test" }
end
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/partials/_layout.sass
Expand Up @@ -4,7 +4,7 @@ $silvia-color: #741519
$menu-color: #666666

#sidebar
position: fixed
position: absolute
top: 0px
left: 0px

Expand Down
6 changes: 5 additions & 1 deletion app/assets/stylesheets/partials/_sidebar.sass
Expand Up @@ -14,7 +14,7 @@
height: 352px
display: block

ul#sidebar_navigation li
ul#sidebar_navigation li, ul#language li
+horizontal-list
margin-bottom: 10px
padding: 9px
Expand All @@ -38,8 +38,12 @@
&:hover
color: $silvia-color

ul#language
margin-top: 15px

ul#social
list-style-type: none
height: 30px
li
a
float: left
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/application_controller.rb
@@ -1,8 +1,10 @@
require "application_responder"
require 'application_responder'
require 'locale_detector'

class ApplicationController < ActionController::Base
self.responder = ApplicationResponder
respond_to :html
before_filter :set_locale

protect_from_forgery

Expand All @@ -18,4 +20,9 @@ def select_layout
def pjax_request?
request.headers['X-PJAX']
end

def set_locale
session[:locale] = LocaleDetector.new(params, session, request.env).detect
I18n.locale = session[:locale]
end
end
2 changes: 1 addition & 1 deletion app/controllers/contact_forms_controller.rb
Expand Up @@ -7,7 +7,7 @@ def create
@contact_form = ContactForm.new(params[:contact_form])
@contact_form.request = request
if @contact_form.deliver
flash.now[:notice] = 'Thank you for contacting me! You will be receiving an answer soon'
flash.now[:notice] = t('contact_forms.thank_you')
end
render action: 'new'
end
Expand Down
23 changes: 22 additions & 1 deletion app/models/category.rb
@@ -1,15 +1,36 @@
# Categories are used to classify Illustrations and Videos
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
include GlobalizeExtensions

friendly_id :name, use: :simple_i18n
validates :name, presence: true

has_many :illustrations, dependent: :nullify
has_many :videos, dependent: :nullify

translates :name
translate_accessors_in :ca, :es
before_save :set_friendly_id

# A simple scope to just return active categories
#
def self.active
where(active: true)
end

# Never generate automatically a new slug because it has problems with
# multiple locales.
def should_generate_new_friendly_id?
false
end

# Set the slug in all available locales
def set_friendly_id(*args)
I18n.available_locales.each do |locale|
I18n.with_locale(locale) do
super(name, locale)
end
end
end
end
23 changes: 23 additions & 0 deletions app/models/concerns/globalize_extensions.rb
@@ -0,0 +1,23 @@
module GlobalizeExtensions
extend ActiveSupport::Concern

included do
def self.translate_accessors_in(*locales)
locales.each do |locale|
translated_attribute_names.each do |field|
define_method("#{field}_#{locale}") do
::Globalize.with_locale(locale) do
self.send("#{field}")
end
end

define_method("#{field}_#{locale}=") do |value|
::Globalize.with_locale(locale) do
self.send("#{field}=", value)
end
end
end
end
end
end
end
24 changes: 22 additions & 2 deletions app/models/illustration.rb
@@ -1,9 +1,10 @@
# An Illustration is a media type with a picture attached
#
class Illustration < ActiveRecord::Base
include Thumbnailer
include GlobalizeExtensions
extend FriendlyId
friendly_id :name, use: :slugged

friendly_id :name, use: :simple_i18n

default_scope order('created_at')

Expand All @@ -14,6 +15,10 @@ class Illustration < ActiveRecord::Base
# A category is needed in order to classify the illustration
belongs_to :category

translates :name, :description
translate_accessors_in :ca, :es
before_save :set_friendly_id

# A scope to get illustration that belong to a category
#
# category - The Category that illustration shoudl belong to
Expand All @@ -22,4 +27,19 @@ class Illustration < ActiveRecord::Base
def self.by_category(category)
where(category_id: category)
end

# Never generate automatically a new slug because it has problems with
# multiple locales.
def should_generate_new_friendly_id?
false
end

# Set the slug in all available locales
def set_friendly_id(*args)
I18n.available_locales.each do |locale|
I18n.with_locale(locale) do
super(name, locale)
end
end
end
end
6 changes: 5 additions & 1 deletion app/models/video.rb
@@ -1,6 +1,10 @@
# A Video is a media type with a linked video to an external service
class Video < ActiveRecord::Base
include GlobalizeExtensions
belongs_to :category

validates :name, :url, :category, presence: true

belongs_to :category
translates :name, :description
translate_accessors_in :ca, :es
end
5 changes: 5 additions & 0 deletions app/views/backend/categories/_form.html.slim
@@ -0,0 +1,5 @@
= simple_form_for [ :backend, resource ] do |f|
= f.input :name_ca
= f.input :name_es
= f.input :active
= f.submit
6 changes: 4 additions & 2 deletions app/views/backend/illustrations/_form.html.slim
@@ -1,6 +1,8 @@
= simple_form_for [ :backend, resource ] do |f|
- (attributes - ['image', 'category_id', 'slug']).each do |attr|
= f.input attr
= f.input :name_ca, required: true
= f.input :name_es, required: true
= f.input :description_ca, as: :text
= f.input :description_es, as: :text

#illustration
= f.input :image
Expand Down
2 changes: 1 addition & 1 deletion app/views/backend/illustrations/_show.html.slim
@@ -1,5 +1,5 @@
dl
- (attributes - %w(image thumbnail_coordinates category_id)).each do |attr|
- (attributes - %w(image thumbnail_coordinates category_id) + %w(name_ca name_es description_ca description_es)).each do |attr|
dt= resource_class.human_attribute_name(attr)
dd= resource.public_send(attr)
dt= resource_class.human_attribute_name(:image)
Expand Down
2 changes: 1 addition & 1 deletion app/views/backend/resource/new.html.slim
@@ -1,6 +1,6 @@
.block
.content
h2 New #{resource_class.model_name.human}
h2=resource_class.model_name.human

.inner
= render "form", attributes: attributes
Expand Down
9 changes: 9 additions & 0 deletions app/views/backend/videos/_form.html.slim
@@ -0,0 +1,9 @@
= simple_form_for [ :backend, resource ] do |f|
= f.input :name_ca, required: true
= f.input :name_es, required: true
= f.input :description_ca, as: :text
= f.input :description_es, as: :text
= f.input :url
= f.association :category, collection: Category.all
= f.submit

2 changes: 1 addition & 1 deletion app/views/contact_forms/new.html.slim
Expand Up @@ -5,7 +5,7 @@ section#contact

section#contact-form
= twitter_bootstrap_form_for @contact_form, url: contact_path, validate: false do |f|
=f.inputs 'Contacte' do
=f.inputs t('.title') do
=f.text_field :name
=f.email_field :email, placeholder: 'email@example.org'
=f.text_area :message, class: 'xxlarge'
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/_sidebar.html.slim
Expand Up @@ -5,3 +5,4 @@ h1
nav
==render_navigation(context: :sidebar)
==render_navigation(context: :social)
==render_navigation(context: :language)
2 changes: 2 additions & 0 deletions config/application.rb
Expand Up @@ -18,6 +18,7 @@ class Application < Rails::Application
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(
#{config.root}/app/services
#{config.root}/app/models/concerns
)

# Only load the plugins named here, in the order given (default is alphabetical).
Expand All @@ -34,6 +35,7 @@ class Application < Rails::Application
# 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 = :ca
config.i18n.fallbacks = true

# Please note that JavaScript expansions are *ignored altogether* if the asset
# pipeline is enabled (see config.assets.enabled below). Put your defaults in
Expand Down
1 change: 1 addition & 0 deletions config/initializers/i18n.rb
@@ -0,0 +1 @@
I18n.available_locales = [:ca, :es]

0 comments on commit 323966b

Please sign in to comment.