Module: HasLocalizationTable::ActiveRecord::Attributes
- Defined in:
- lib/has_localization_table/active_record/attributes.rb
Instance Method Summary collapse
-
#method_missing(name, *args, &block) ⇒ Object
Define attribute getters and setters.
- #read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale, options = {}) ⇒ Object
- #respond_to?(*args) ⇒ Boolean
- #write_localized_attribute(attribute, value, locale = HasLocalizationTable.current_locale) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Define attribute getters and setters
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/has_localization_table/active_record/attributes.rb', line 33 def method_missing(name, *args, &block) if name.to_s =~ /\A([a-z0-9_]+)(=)?\Z/i if localized_attributes.include?($1.to_sym) if $2.nil? # No equals sign -- not a setter # Try to load a string for the given locale # If that fails, try for the primary locale raise ArgumentError, "wrong number of arguments (#{args.size} for 0 or 1)" unless args.size.between?(0, 1) = args. return read_localized_attribute($1, args.first, ) || read_localized_attribute($1, HasLocalizationTable.primary_locale, ) else raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" unless args.size == 1 return write_localized_attribute($1, args.first) end end end super end |
Instance Method Details
#read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/has_localization_table/active_record/attributes.rb', line 4 def read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale, = {}) locale ||= HasLocalizationTable.current_locale attribute_cache[attribute.to_sym][locale.id] ||= begin attr = localization_association.detect{ |a| a.send(HasLocalizationTable.locale_foreign_key) == locale.id }.send(attribute) rescue '' attr ||= '' # if the attribute somehow is nil, change it to a blank string so we're always dealing with strings fallback = .fetch(:fallback, HasLocalizationTable.config.fallback_locale) if fallback && attr.blank? fallback = fallback.call(self) if fallback.respond_to?(:call) return read_localized_attribute(attribute, fallback) unless fallback == locale end attr end end |
#respond_to?(*args) ⇒ Boolean
52 53 54 55 |
# File 'lib/has_localization_table/active_record/attributes.rb', line 52 def respond_to?(*args) return true if args.first.to_s =~ /\A([a-z0-9_]+)=?\Z/i and localized_attributes.include?($1.to_sym) super end |
#write_localized_attribute(attribute, value, locale = HasLocalizationTable.current_locale) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/has_localization_table/active_record/attributes.rb', line 23 def write_localized_attribute(attribute, value, locale = HasLocalizationTable.current_locale) value = value.to_s localization = localization_association.detect{ |a| a.send(HasLocalizationTable.locale_foreign_key) == locale.id } || localization_association.build(HasLocalizationTable.locale_foreign_key => locale.id) localization.send(:"#{attribute}=", value) attribute_cache[attribute.to_sym][locale.id] = value.blank? ? nil : value end |