forked from oriolgual/silvia.cat
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request oriolgual#14 from oriolgual/feature/globalize
I18n
- Loading branch information
Showing
45 changed files
with
691 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
= simple_form_for [ :backend, resource ] do |f| | ||
= f.input :name_ca | ||
= f.input :name_es | ||
= f.input :active | ||
= f.submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
I18n.available_locales = [:ca, :es] |
Oops, something went wrong.