Class: Netfira::WebConnect::Model::Record::Translation

Inherits:
Netfira::WebConnect::Model show all
Defined in:
lib/netfira/web_connect/model/record/translation.rb,
lib/netfira/web_connect/model.rb

Overview

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

Class Method Summary collapse

Methods inherited from Netfira::WebConnect::Model

#dispatch_event

Class Method Details

.materialize(owner_class) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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_nameObject



54
55
56
# 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_nameObject



50
51
52
# 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_nameObject



42
43
44
45
46
47
48
# 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_namesObject



38
39
40
# 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