class Netfira::WebConnect::Model::Record::Translation

Subclasses of this model represents rows from translation tables, e.g. nf_products_l10n. Subclasses are materialized when their parent record models are materialized.

Public Class Methods

materialize(owner_class) click to toggle source
# File lib/netfira/web_connect/model/record/translation.rb, line 9
def materialize(owner_class)

  # Make a translation model, e.g. Product::Translation
  klass = Class.new(self)
  owner_class.const_set :Translation, klass

  # Keep a reference to the owner class
  klass.instance_variable_set :@owner_class, owner_class

  # Give translations references back to their parent records
  klass.belongs_to :owner,
                   class_name: owner_class.name,
                   foreign_key: "#{owner_class.name.demodulize.underscore}_id",
                   inverse_of: :translation_models

  # Add a translations relation, e.g. product.translations
  owner_class.has_many :translation_models,
                       class_name: klass.name,
                       inverse_of: :owner,
                       # TODO: cleanup on `really_destroy!`
                       dependent: Netfira::WebConnect.paranoia? ? nil : :delete_all,
                       autosave: true

  # Add translations to the default scope, as they will almost
  # always be needed for lookups
  owner_class.default_scope { includes :translation_models }

end
plural_name() click to toggle source
# File lib/netfira/web_connect/model/record/translation.rb, line 54
def plural_name
  @plural_name ||= Netfira::WebConnect.db_table_l10n_suffix(@owner_class.plural_name).freeze
end
single_name() click to toggle source
# File lib/netfira/web_connect/model/record/translation.rb, line 50
def single_name
  @single_name ||= Netfira::WebConnect.db_table_l10n_suffix(@owner_class.single_name).freeze
end
table_name() click to toggle source
# File lib/netfira/web_connect/model/record/translation.rb, line 42
def table_name
  @table_name ||= if self == Model::Record::Translation
    Models::Table.table_name
  else
    Netfira::WebConnect.db_table_prefix(plural_name).to_s
  end
end
translated_attribute_names() click to toggle source
# File lib/netfira/web_connect/model/record/translation.rb, line 38
def translated_attribute_names
  @translated_attribute_names ||= attribute_names - ['id', 'language', "#{@owner_class.single_name}_id"]
end