Class: Vulgata::Strategies::Globalize

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

Class Method Summary collapse

Class Method Details

.save_translation(translation_state) ⇒ Object

saves a single translation



6
7
8
9
10
11
12
# File 'lib/vulgata/strategies/globalize.rb', line 6

def self.save_translation translation_state
  resource = translation_state.item
  ::Globalize.with_locale(translation_state.locale.to_sym) do
    resource.attributes = translation_state.translation_data
    resource.save(validate: false)
  end
end

.scope_by_source_items(items) ⇒ Object



14
15
16
# File 'lib/vulgata/strategies/globalize.rb', line 14

def self.scope_by_source_items items
  items
end

.source_translation(instance) ⇒ Object

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



19
20
21
# File 'lib/vulgata/strategies/globalize.rb', line 19

def self.source_translation instance
  instance.translations.order(created_at: :asc, id: :asc).first
end

.translated_attribute_names(klass) ⇒ Object

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



40
41
42
# File 'lib/vulgata/strategies/globalize.rb', line 40

def self.translated_attribute_names klass
  klass.translated_attribute_names
end

.translation_data(translation_state) ⇒ Object

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



29
30
31
32
33
34
35
36
37
# File 'lib/vulgata/strategies/globalize.rb', line 29

def self.translation_data translation_state
  if translation_data_with_meta translation_state
    ::Globalize.with_locale(translation_state.locale.to_sym) do
      return translation_state.item.translated_attributes.with_indifferent_access
    end
  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/globalize.rb', line 53

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

.translation_data_with_meta(translation_state) ⇒ Object

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



24
25
26
# File 'lib/vulgata/strategies/globalize.rb', line 24

def self.translation_data_with_meta translation_state
  translation_state.item.translations.find_by(locale: translation_state.locale)
end

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

query the translation, used when searching the query_string is not used in the first line as a raw string for security reasons (sql injections) the collection might be n filtered query on the class (for already filtered queries)



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

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