Created
November 6, 2015 10:03
-
-
Save sld/ea9f51aa6106912c088b to your computer and use it in GitHub Desktop.
Globalize with after_add and after_remove callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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