Sha256: 02dad879933acc7fccce655859ea7b60eeff5092396d03afa3f2e670af5b5546

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Traco
  module ClassMethods
    LOCALE_RE = /[a-zA-Z]{2}(?:-[a-zA-Z]{2})?/

    def locales_for_attribute(attribute)
      re = /\A#{attribute}_(#{LOCALE_RE})\z/

      column_names.
        grep(re) { $1.to_sym }.
        sort_by(&locale_sort_value)
    end

    def locale_columns(*attributes)
      attributes.inject([]) { |memo, attribute|
        memo += locales_for_attribute(attribute).map { |locale|
          :"#{attribute}_#{locale}"
        }
      }
    end

    def human_attribute_name(attribute, options = {})
      default = super(attribute, options.merge(:default => ""))
      if default.blank? && attribute.to_s.match(/\A(\w+)_(#{LOCALE_RE})\z/)
        column, locale = $1, $2.to_sym
        if translates?(column)
          return "#{super(column, options)} (#{locale_name(locale)})"
        end
      end
      super
    end

    private

    def locale_sort_value
      lambda { |locale|
        if locale == I18n.default_locale
          # Sort the default locale first.
          "0"
        else
          # Sort the rest alphabetically.
          locale.to_s
        end
      }
    end

    def translates?(attribute)
      translatable_attributes.include?(attribute.to_sym)
    end

    def locale_name(locale)
      I18n.t(locale, :scope => :"i18n.languages", :default => locale.to_s.upcase)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
traco-1.3.0 lib/traco/class_methods.rb