3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/acts_as_simple_translatable/class_methods.rb', line 3
def acts_as_simple_translatable_on(*fields)
scope :with_translations, -> { includes(:translations) }
has_many :translations, as: :translatable, dependent: :destroy
accepts_nested_attributes_for :translations, reject_if: :all_blank, allow_destroy: :true
@locale_fields = fields
class << self
attr_accessor :locale_fields
end
fields.each do |field|
define_method "#{field}" do
content = (I18n.locale == I18n.default_locale) ? super() : locale_translations[field]
content.present? ? content : super()
end
define_method "#{field}_original" do
self[field]
end
define_method "#{field}?" do
!send("#{field}").blank?
end
end
define_method :locale_translations do
unless @locale_translations
@locale_translations = {}
translations.select { |t| t.locale == I18n.locale.to_s }.each do |translation|
@locale_translations ||= {}
@locale_translations[translation.translatable_field.to_sym] = translation.content
end
end
@locale_translations
end
end
|