Permalink
Browse files

initial dev; started easing in versioning

  • Loading branch information...
1 parent e0f49f1 commit c28db8edeeb4b914c46d05e861c683ce5504012a Joshua Harvey committed Jan 30, 2009
View
@@ -1,2 +1,3 @@
# Include hook code here
+require 'globalize2_versioning'
ActiveRecord::Base.send :include, Globalize::Model::ActiveRecord::Versioned
View
74 lib/globalize2_versioning.rb 100644 → 100755
@@ -1,60 +1,46 @@
module Globalize
module Model
- module ActiveRecord
- module Versioned
- def self.included(base)
- base.extend ActMethods
- end
-
- module ActMethods
- def translates(*attr_names)
- options = attr_names.extract_options!
- options[:translated_attributes] = attr_names
-
- # Only set up once per class
- unless included_modules.include? InstanceMethods
- class_inheritable_accessor :globalize_options
- include Globalize::Model::ActiveRecord::Translated::InstanceMethods
- include InstanceMethods
- extend Globalize::Model::ActiveRecord::Translated::ClassMethods
-
- proxy_class = Globalize::Model::ActiveRecord.create_proxy_class(self)
- has_many :globalize_translations, :class_name => proxy_class.name do
- def by_locales(locales)
- find :all, :conditions => { :locale => locales.map(&:to_s) }
- end
- end
-
- before_save :bump_version
- after_save :update_globalize_record
-
- def i18n_attr(attribute_name)
- self.name.underscore + "_translations.#{attribute_name}"
- end
- end
-
- self.globalize_options = options
- Globalize::Model::ActiveRecord.define_accessors(self, attr_names)
- end
+ class Adapter
+ def update_translations!
+ @stash.each do |locale, attrs|
+ translation = @record.globalize_translations.find_or_initialize_by_locale(locale.to_s)
+ attrs.each{|attr_name, value| translation[attr_name] = value }
+ translation.version = @record.version if @record.versioned?
+ translation.save!
end
-
- module ClassMethods
+ end
+ end
+
+ module ActiveRecord
+
+ # Hook this into Globalize2's translates method
+ module Translated::Callbacks
+ def globalize2_versioning
+ if globalize_options[:versioned].blank?
+ define_method :'versioned?', lambda { false }
+ else
+ include Versioned::InstanceMethods
+ before_save :bump_version
+ end
end
-
+ end
+
+ module Versioned
module InstanceMethods
+ def versioned?; true end
+
def bump_version
- self.version = next_version if new_record? || save_version?
+ self.version = next_version if save_version?
end
def next_version
- ( globalize_translations.calculate(:max, :version) || 0 ) + 1
+ ( globalize_translations.maximum(:version) || 0 ) + 1
end
# Checks whether a new version should be saved or not.
def save_version?
- ( globalize_options[:versioned] & changed ).length > 0
- end
-
+ new_record? || ( globalize_options[:versioned].map {|k| k.to_s } & changed ).length > 0
+ end
end
end
end
@@ -1,3 +1,4 @@
+=begin
require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
require 'active_record'
require 'globalize/model/active_record'
@@ -70,4 +71,5 @@ def setup
end
end
-end
+end
+=end
@@ -18,7 +18,7 @@
# Load Post model
require File.join( File.dirname(__FILE__), '..', 'data', 'post' )
-class TranslatedTest < ActiveSupport::TestCase
+class VersionTranslatedTest < ActiveSupport::TestCase
def setup
I18n.locale = :'en-US'
I18n.fallbacks.clear
View
@@ -26,18 +26,43 @@ def setup
reset_db!
end
+ test 'versioned? method' do
+ post = Post.new
+ assert !post.versioned?
+ section = Section.new
+ assert section.versioned?
+ end
+
test 'new record version' do
+ section = Section.create :content => 'foo'
+ assert_equal 1, section.version
+ end
+
+ test 'subsequent version' do
+ section = Section.create :content => 'foo'
+ assert_equal 1, section.version
+ section.content = 'bar'
+ assert section.save
+ assert_equal 2, section.version
+ section.update_attribute(:content, 'baz')
+ assert_equal 3, section.version
end
+ test 'save_version? on new record' do
+ section = Section.new :content => 'foo'
+ assert section.save_version?
+ end
+
test 'save_version?' do
section = Section.create :content => 'foo'
assert !section.save_version?
section.title = 'bar'
assert !section.save_version?
section.content = 'baz'
- assert !section.save_version?
+ assert section.save_version?
end
+=begin
test 'revert_to' do
flunk
end
@@ -57,4 +82,5 @@ def setup
test 'clone_versioned_model' do
flunk
end
+=end
end

0 comments on commit c28db8e

Please sign in to comment.