module TweekCatalogue::Concerns::I18nSupport::ClassMethods
Public Instance Methods
i18n_fields()
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 51 def i18n_fields @i18n_fields ||= (i18n_keys + i18n_relations).uniq end
i18n_key(attr_name, opts = {})
click to toggle source
Create key with I18n support.
Actually this field will create one ore more fields based on the i18n_keys_for
locale list. Also additionally an accessor which will available the attribute value based on the I18n.locale
settings.
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 46 def i18n_key(attr_name, opts = {}) add_i18n_key!(attr_name) generate_i18n_helper_methods! attr_name, opts end
i18n_keys_for(*locales)
click to toggle source
Set boundary for supported locales.
You can set which set of locales should be supported by i18n_key
and
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 15 def i18n_keys_for(*locales) self.supported_locales = locales.map(&:to_sym) end
i18n_many(attr, opts = {})
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 19 def i18n_many(attr, opts = {}) add_i18n_relationship!(attr) generate_i18n_many(attr, opts) end
i18n_one(attr, opts = {})
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 24 def i18n_one(attr, opts = {}) add_i18n_relationship!(attr) generate_i18n_one(attr, opts) end
i18n_validates_presence_of(*fields)
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 29 def i18n_validates_presence_of(*fields) fields.each do |field| supported_locales.each do |locale| other_locales = supported_locales - [locale] other_fields = other_locales.map { |l| "#{field}_#{l}" } class_eval <<-CODE, __FILE__, __LINE__ validates_presence_of :"#{field}_#{locale}", unless: -> { [#{other_fields.join('.present?, ')}].any? } CODE end end end
Private Instance Methods
add_i18n_key!(attr_name)
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 57 def add_i18n_key! attr_name self.i18n_keys ||= [] self.i18n_keys += [attr_name] end
add_i18n_relationship!(attr_name)
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 62 def add_i18n_relationship! attr_name self.i18n_relations ||= [] self.i18n_relations += [attr_name] end
generate_i18n_helper_methods!(attr_name, opts)
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 91 def generate_i18n_helper_methods!(attr_name, opts) self.supported_locales ||= ['en'] supported_locales.each do |locale| # Set mongomapper key key :"#{attr_name}_#{locale.to_s}" # Generate attr_name_for suffix which accepts locale parameter class_eval <<-CODE, __FILE__, __LINE__ def #{attr_name}_for(locale) self.send :"#{attr_name}_\#{locale}" end CODE end end
generate_i18n_many(attr_name, opts)
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 79 def generate_i18n_many(attr_name, opts) supported_locales.each do |locale| many :"#{locale}_#{attr_name}", opts class_eval <<-CODE, __FILE__, __LINE__ def #{attr_name} self.send(:"\#{I18n.locale}_#{attr_name}") end CODE end end
generate_i18n_one(attr_name, opts)
click to toggle source
# File lib/tweek_catalogue/concerns/i18n_support.rb, line 67 def generate_i18n_one(attr_name, opts) supported_locales.each do |locale| one :"#{locale}_#{attr_name}", opts class_eval <<-CODE, __FILE__, __LINE__ def #{attr_name} self.send(:"\#{I18n.locale}_#{attr_name}") end CODE end end