Class: Vulgata::Strategies::LocaleColumnTranslations

Inherits:
Object
  • Object
show all
Defined in:
lib/vulgata/strategies/locale_column_translations.rb

Direct Known Subclasses

I18nTranslations

Instance Method Summary collapse

Constructor Details

#initialize(translation_identifier_attribute, translated_attributes) ⇒ LocaleColumnTranslations

Returns a new instance of LocaleColumnTranslations.



5
6
7
8
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 5

def initialize(translation_identifier_attribute, translated_attributes)
  @translation_identifier_attribute = translation_identifier_attribute
  @translated_attributes = translated_attributes
end

Instance Method Details

#save_translation(translation_state) ⇒ Object

saves a single translation



11
12
13
14
15
16
17
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 11

def save_translation translation_state
  resource = translation_state.source_translation_state.item_type.constantize.find_or_initialize_by(:locale => translation_state.locale, @translation_identifier_attribute => translation_state.item[@translation_identifier_attribute])
  resource.vlg_skip_init = true
  resource.vlg_skip_pendify = true
  resource.attributes = translation_state.translation_data
  resource.save(validate: false)
end

#scope_by_source_items(items) ⇒ Object

when translations are stored in the same table the sources are distincted by the locale



20
21
22
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 20

def scope_by_source_items items
  items.where(locale: I18n.default_locale)
end

#source_translation(instance) ⇒ Object

returns the source translation data hash with locale key-value pair



25
26
27
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 25

def source_translation instance
  instance.class.find_by(:locale => I18n.default_locale, @translation_identifier_attribute => instance[@translation_identifier_attribute])
end

#translated_attribute_names(klass) ⇒ Object

returns array of translated attribute name (i.e [:title, :body])



43
44
45
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 43

def translated_attribute_names klass
  @translated_attributes
end

#translation_data(translation_state) ⇒ Object

returns the translation data in a form of { attr1_name: attr1_value, attr2_name: attr2_value }



35
36
37
38
39
40
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 35

def translation_data translation_state
  if data = translation_data_with_meta(translation_state)
    return data.slice(*@translated_attributes).with_indifferent_access
  end
  nil
end

#translation_data_changed?(instance) ⇒ Boolean

checks if a translated attribute is changed

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 53

def translation_data_changed? instance
  translated_attributes = @translated_attributes.map(&:to_s)
  instance.changed.select{ |attr| translated_attributes.include?(attr) }.any?
end

#translation_data_with_meta(translation_state) ⇒ Object

returns the translation data along with some meta data (locale, timestamps)



30
31
32
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 30

def translation_data_with_meta translation_state
  translation_state.item_class.find_by(:locale => translation_state.locale, @translation_identifier_attribute => translation_state.item[@translation_identifier_attribute])
end

#where_like_query(klass, query, query_params, collection) ⇒ Object



47
48
49
50
# File 'lib/vulgata/strategies/locale_column_translations.rb', line 47

def where_like_query klass, query, query_params, collection
  query = query % { table_name: klass.table_name }
  (collection ||= klass).where(query, query_params)
end