Sha256: cc8570d9935fb1366430fd41be37292ef03417692db7719560da90227d0f926a

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

module Mobility
  module Backend
=begin

Stores translated attribute as a column on the model table.

To use this backend, ensure that the model table has columns named
+<attribute>_<locale>+ for every locale in +I18n.available_locales+.

==Backend Options

There are no options for this backend. Also, the +locale_accessors+ option will
be ignored if set, since it would cause a conflict with column accessors.

@see Mobility::Backend::ActiveRecord::Column
@see Mobility::Backend::Sequel::Column

=end
    module Column
      include OrmDelegator

      # @!group Backend Accessors
      #
      # @!macro backend_reader
      def read(locale, **options)
        model.send(column(locale))
      end

      # @!macro backend_writer
      def write(locale, value, **options)
        model.send("#{column(locale)}=", value)
      end
      # @!endgroup

      # Returns name of column where translated attribute is stored
      # @param [Symbol] locale
      # @return [String]
      def column(locale = Mobility.locale)
        Column.column_name_for(attribute, locale)
      end

      # Returns name of column where translated attribute is stored
      # @param [String] attribute
      # @param [Symbol] locale
      # @return [String]
      def self.column_name_for(attribute, locale = Mobility.locale)
        normalized_locale = locale.to_s.downcase.sub("-", "_")
        "#{attribute}_#{normalized_locale}".to_sym
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mobility-0.1.3 lib/mobility/backend/column.rb
mobility-0.1.2 lib/mobility/backend/column.rb
mobility-0.1.1 lib/mobility/backend/column.rb
mobility-0.1.0 lib/mobility/backend/column.rb