Skip to content

Commit

Permalink
Update record checkers before save
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Jul 22, 2011
1 parent 382829c commit 061c2ae
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/globalize/active_record/accessors.rb
Expand Up @@ -6,12 +6,12 @@ def self.included(base)
translated_attribute_names.each do |attr_name|
Globalize.available_locales.each do |locale|
define_method :"#{attr_name}_#{locale}" do
read_attribute(attr_name, locale)
read_attribute(attr_name, {:locale => locale})
end

define_method :"#{attr_name}_#{locale}=" do |value|
changed_attributes[:"#{attr_name}_#{locale}"] = value unless value == read_attribute(attr_name, locale)
write_attribute(attr_name, value, locale)
write_attribute(attr_name, value, {:locale => locale})
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/globalize/active_record/act_macro.rb
Expand Up @@ -32,6 +32,7 @@ def translates(*attr_columns)
:dependent => :destroy,
:extend => HasManyExtensions

before_save :update_checkers!
after_create :save_translations!
after_update :save_translations!

Expand Down
4 changes: 4 additions & 0 deletions lib/globalize/active_record/adapter.rb
Expand Up @@ -48,6 +48,10 @@ def save_translations!
def reset
stash.clear
end

def all_blank?(locale, attrs)
[attrs].flatten.collect { |name| fetch_stash(locale, name).blank? }.all?
end

protected

Expand Down
8 changes: 7 additions & 1 deletion lib/globalize/active_record/instance_methods.rb
Expand Up @@ -141,7 +141,13 @@ def used_locales
locales.uniq!
locales
end


def update_checkers!
Globalize.available_locales.each do |locale|
self["is_locale_#{locale}"] = !globalize.all_blank?(locale, translated_attribute_names)
end
end

def save_translations!
globalize.save_translations!
@translation_caches = {}
Expand Down
2 changes: 2 additions & 0 deletions lib/globalize/active_record/translation.rb
@@ -1,6 +1,8 @@
module Globalize
module ActiveRecord
class Translation < ::ActiveRecord::Base
attr_accessible :locale

class << self
def with_locales(*locales)
# Avoid using "IN" with SQL queries when only using one locale.
Expand Down

0 comments on commit 061c2ae

Please sign in to comment.