class Vulgata::Strategies::Globalize

Public Class Methods

save_translation(translation_state) click to toggle source

saves a single translation

# 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) click to toggle source
# File lib/vulgata/strategies/globalize.rb, line 14
def self.scope_by_source_items items
  items
end
source_translation(instance) click to toggle source

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

# 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) click to toggle source

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

# File lib/vulgata/strategies/globalize.rb, line 40
def self.translated_attribute_names klass
  klass.translated_attribute_names
end
translation_data(translation_state) click to toggle source

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

# 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) click to toggle source

checks if a translated attribute is changed

# 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) click to toggle source

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

# 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) click to toggle source

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)

# 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