Skip to content

Instantly share code, notes, and snippets.

@sld
Created November 6, 2015 10:03
Show Gist options
  • Save sld/ea9f51aa6106912c088b to your computer and use it in GitHub Desktop.
Save sld/ea9f51aa6106912c088b to your computer and use it in GitHub Desktop.
Globalize with after_add and after_remove callbacks
module GlobalizeWithCallbacks
extend ActiveSupport::Concern
class_methods do
# It is Globalize's #translates method with #after_add and #after_remove
# callbacks on :translations table. It may be used to run elasticsearch
# multilingual indexing.
#
# Define #after_translation_add and #after_translation_remove methods in
# the included class
def translates_with_callbacks(*attr_names)
translates(*attr_names)
options = attr_names.extract_options!
has_many :translations, :class_name => translation_class.name,
:foreign_key => options[:foreign_key],
:dependent => :destroy,
:extend => Globalize::ActiveRecord::HasManyExtensions,
:autosave => true,
:after_add => :after_translation_add,
:after_remove => :after_translation_remove
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment