Module: RailsExtend::ActiveRecord::I18n

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails_extend/active_record/i18n.rb

Instance Method Summary collapse

Instance Method Details

#attributes_with_values_for_create(attribute_names) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rails_extend/active_record/i18n.rb', line 22

def attributes_with_values_for_create(attribute_names)
  r = super
  r.slice(*i18n_attributes).each do |key, v|
    r[key] = public_send "#{key}_before_type_cast"
  end
  r
end

#update_i18n_column ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rails_extend/active_record/i18n.rb', line 11

def update_i18n_column
  str = []
  self.changes.slice(*i18n_attributes).each do |key, _|
    value = self.public_send("#{key}_before_type_cast")
    str << "#{key} = #{key}::jsonb || '#{value.to_json}'::jsonb"
  end
  return if str.blank?
  s = str.join(', ')
  self.class.connection.execute "UPDATE #{self.class.table_name} SET #{s} WHERE id = #{self.id}"
end