Module: HasLocalizationTable::ActiveRecord::Attributes

Defined in:
lib/has_localization_table/active_record/attributes.rb,
lib/has_localization_table/active_record/attributes/cache.rb

Defined Under Namespace

Classes: Cache

Constant Summary collapse

LOCALIZED_ATTRIBUTE_REGEX =
/\A(?<name>[a-z0-9_]+)(?<suffix>=|_changed\?)?\Z/i

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Define attribute getters and setters



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/has_localization_table/active_record/attributes.rb', line 37

def method_missing(name, *args, &block)
  match = name.to_s.match(LOCALIZED_ATTRIBUTE_REGEX)

  if match
    if localized_attributes.include?(match[:name].to_sym)
      if match[:suffix].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)
        options = args.extract_options!
        return read_localized_attribute($1, args.first, options) || read_localized_attribute($1, HasLocalizationTable.primary_locale, options)
      elsif match[:suffix] == '='
        raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" unless args.size == 1
        return write_localized_attribute($1, args.first)
      elsif current_localization.respond_to?(name).inspect
        return localized_attribute_changed?(name.to_s.sub(/_changed\?/, ''))
      end
    end
  end

  super
end

Instance Method Details

#read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/has_localization_table/active_record/attributes.rb', line 8

def read_localized_attribute(attribute, locale = HasLocalizationTable.current_locale, options = {})
  locale ||= HasLocalizationTable.current_locale

  localized_attribute_cache.get(attribute, locale) do
    attr = localization_for(locale).send(attribute) rescue ''
    attr ||= '' # if the attribute somehow is nil, change it to a blank string so we're always dealing with strings

    fallback = options.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

#reset_localized_attribute_cacheObject



66
67
68
# File 'lib/has_localization_table/active_record/attributes.rb', line 66

def reset_localized_attribute_cache
  localized_attribute_cache.reset
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/has_localization_table/active_record/attributes.rb', line 60

def respond_to?(*args)
  match = args.first.to_s.match(LOCALIZED_ATTRIBUTE_REGEX)
  return true if match && localized_attributes.include?(match[:name].to_sym)
  super
end

#write_localized_attribute(attribute, value, locale = HasLocalizationTable.current_locale) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/has_localization_table/active_record/attributes.rb', line 27

def write_localized_attribute(attribute, value, locale = HasLocalizationTable.current_locale)
  value = value.to_s
  localization = localization_for(locale) ||
    localization_association.build(HasLocalizationTable.locale_foreign_key => locale.id)

  localization.send(:"#{attribute}=", value)
  value.blank? ? localized_attribute_cache.clear(attribute, locale) : localized_attribute_cache.set(attribute, locale, value)
end