##// END OF EJS Templates
Merged Rails 2.1 compatibility branch....
Jean-Philippe Lang -
r1609:7cdd88a6ce36
parent child
Show More
@@ -0,0 +1,17
1
2 ActiveRecord::Errors.default_error_messages = {
3 :inclusion => "activerecord_error_inclusion",
4 :exclusion => "activerecord_error_exclusion",
5 :invalid => "activerecord_error_invalid",
6 :confirmation => "activerecord_error_confirmation",
7 :accepted => "activerecord_error_accepted",
8 :empty => "activerecord_error_empty",
9 :blank => "activerecord_error_blank",
10 :too_long => "activerecord_error_too_long",
11 :too_short => "activerecord_error_too_short",
12 :wrong_length => "activerecord_error_wrong_length",
13 :taken => "activerecord_error_taken",
14 :not_a_number => "activerecord_error_not_a_number"
15 }
16
17 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
@@ -0,0 +1,4
1 # Add new mime types for use in respond_to blocks:
2
3 Mime::SET << Mime::CSV unless Mime::SET.include?(Mime::CSV)
4 Mime::Type.register 'application/pdf', :pdf
@@ -0,0 +1,7
1 GLoc.set_config :default_language => :en
2 GLoc.clear_strings
3 GLoc.set_kcode
4 GLoc.load_localized_strings
5 GLoc.set_config(:raise_string_not_found_errors => false)
6
7 require 'redmine'
@@ -0,0 +1,3
1 #!/usr/bin/env ruby
2 require File.dirname(__FILE__) + '/../config/boot'
3 require 'commands/dbconsole'
@@ -0,0 +1,3
1 #!/usr/bin/env ruby
2 require File.dirname(__FILE__) + '/../../config/boot'
3 require 'commands/performance/request'
@@ -0,0 +1,3
1 #!/usr/bin/env ruby
2 require File.dirname(__FILE__) + '/../../config/boot'
3 require 'commands/process/inspector'
@@ -52,7 +52,7 class ProjectsController < ApplicationController
52 respond_to do |format|
52 respond_to do |format|
53 format.html {
53 format.html {
54 @project_tree = projects.group_by {|p| p.parent || p}
54 @project_tree = projects.group_by {|p| p.parent || p}
55 @project_tree.each_key {|p| @project_tree[p] -= [p]}
55 @project_tree.keys.each {|p| @project_tree[p] -= [p]}
56 }
56 }
57 format.atom {
57 format.atom {
58 render_feed(projects.sort_by(&:created_on).reverse.slice(0, Setting.feeds_limit.to_i),
58 render_feed(projects.sort_by(&:created_on).reverse.slice(0, Setting.feeds_limit.to_i),
@@ -22,6 +22,10 module RepositoriesHelper
22 txt.to_s[0,8]
22 txt.to_s[0,8]
23 end
23 end
24
24
25 def to_path_param(path)
26 path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
27 end
28
25 def to_utf8(str)
29 def to_utf8(str)
26 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
30 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
27 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
31 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
@@ -42,8 +42,10 class UserPreference < ActiveRecord::Base
42 if attribute_present? attr_name
42 if attribute_present? attr_name
43 super
43 super
44 else
44 else
45 self.others ||= {}
45 h = read_attribute(:others).dup || {}
46 self.others.store attr_name, value
46 h.update(attr_name => value)
47 write_attribute(:others, h)
48 value
47 end
49 end
48 end
50 end
49
51
@@ -4,14 +4,14
4 <tr id="<%= tr_id %>" class="<%= params[:parent_id] %> entry <%= entry.kind %>">
4 <tr id="<%= tr_id %>" class="<%= params[:parent_id] %> entry <%= entry.kind %>">
5 <td style="padding-left: <%=18 * depth%>px;" class="filename">
5 <td style="padding-left: <%=18 * depth%>px;" class="filename">
6 <% if entry.is_dir? %>
6 <% if entry.is_dir? %>
7 <span class="expander" onclick="<%= remote_function :url => {:action => 'browse', :id => @project, :path => entry.path, :rev => @rev, :depth => (depth + 1), :parent_id => tr_id},
7 <span class="expander" onclick="<%= remote_function :url => {:action => 'browse', :id => @project, :path => to_path_param(entry.path), :rev => @rev, :depth => (depth + 1), :parent_id => tr_id},
8 :update => { :success => tr_id },
8 :update => { :success => tr_id },
9 :position => :after,
9 :position => :after,
10 :success => "scmEntryLoaded('#{tr_id}')",
10 :success => "scmEntryLoaded('#{tr_id}')",
11 :condition => "scmEntryClick('#{tr_id}')"%>">&nbsp</span>
11 :condition => "scmEntryClick('#{tr_id}')"%>">&nbsp</span>
12 <% end %>
12 <% end %>
13 <%= link_to h(entry.name),
13 <%= link_to h(entry.name),
14 {:action => (entry.is_dir? ? 'browse' : 'changes'), :id => @project, :path => entry.path, :rev => @rev},
14 {:action => (entry.is_dir? ? 'browse' : 'changes'), :id => @project, :path => to_path_param(entry.path), :rev => @rev},
15 :class => (entry.is_dir? ? 'icon icon-folder' : 'icon icon-file')%>
15 :class => (entry.is_dir? ? 'icon icon-folder' : 'icon icon-file')%>
16 </td>
16 </td>
17 <td class="size"><%= (entry.size ? number_to_human_size(entry.size) : "?") unless entry.is_dir? %></td>
17 <td class="size"><%= (entry.size ? number_to_human_size(entry.size) : "?") unless entry.is_dir? %></td>
@@ -10,10 +10,10 dirs.each do |dir|
10 link_path << '/' unless link_path.empty?
10 link_path << '/' unless link_path.empty?
11 link_path << "#{dir}"
11 link_path << "#{dir}"
12 %>
12 %>
13 / <%= link_to h(dir), :action => 'browse', :id => @project, :path => link_path, :rev => @rev %>
13 / <%= link_to h(dir), :action => 'browse', :id => @project, :path => to_path_param(link_path), :rev => @rev %>
14 <% end %>
14 <% end %>
15 <% if filename %>
15 <% if filename %>
16 / <%= link_to h(filename), :action => 'changes', :id => @project, :path => "#{link_path}/#{filename}", :rev => @rev %>
16 / <%= link_to h(filename), :action => 'changes', :id => @project, :path => to_path_param("#{link_path}/#{filename}"), :rev => @rev %>
17 <% end %>
17 <% end %>
18
18
19 <%= "@ #{revision}" if revision %>
19 <%= "@ #{revision}" if revision %>
@@ -1,4 +1,4
1 <% form_tag({:controller => 'repositories', :action => 'diff', :id => @project, :path => path}, :method => :get) do %>
1 <% form_tag({:controller => 'repositories', :action => 'diff', :id => @project, :path => to_path_param(path)}, :method => :get) do %>
2 <table class="list changesets">
2 <table class="list changesets">
3 <thead><tr>
3 <thead><tr>
4 <th>#</th>
4 <th>#</th>
@@ -4,12 +4,12
4
4
5 <p>
5 <p>
6 <% if @repository.supports_cat? %>
6 <% if @repository.supports_cat? %>
7 <%= link_to l(:button_view), {:action => 'entry', :id => @project, :path => @path, :rev => @rev } %> |
7 <%= link_to l(:button_view), {:action => 'entry', :id => @project, :path => to_path_param(@path), :rev => @rev } %> |
8 <% end %>
8 <% end %>
9 <% if @repository.supports_annotate? %>
9 <% if @repository.supports_annotate? %>
10 <%= link_to l(:button_annotate), {:action => 'annotate', :id => @project, :path => @path, :rev => @rev } %> |
10 <%= link_to l(:button_annotate), {:action => 'annotate', :id => @project, :path => to_path_param(@path), :rev => @rev } %> |
11 <% end %>
11 <% end %>
12 <%= link_to(l(:button_download), {:action => 'entry', :id => @project, :path => @path, :rev => @rev, :format => 'raw' }) if @repository.supports_cat? %>
12 <%= link_to(l(:button_download), {:action => 'entry', :id => @project, :path => to_path_param(@path), :rev => @rev, :format => 'raw' }) if @repository.supports_cat? %>
13 <%= "(#{number_to_human_size(@entry.size)})" if @entry.size %>
13 <%= "(#{number_to_human_size(@entry.size)})" if @entry.size %>
14 </p>
14 </p>
15
15
@@ -55,7 +55,7
55 <%= "(#{change.revision})" unless change.revision.blank? %></td>
55 <%= "(#{change.revision})" unless change.revision.blank? %></td>
56 <td align="right">
56 <td align="right">
57 <% if change.action == "M" %>
57 <% if change.action == "M" %>
58 <%= link_to l(:label_view_diff), :action => 'diff', :id => @project, :path => without_leading_slash(change.relative_path), :rev => @changeset.revision %>
58 <%= link_to l(:label_view_diff), :action => 'diff', :id => @project, :path => to_path_param(change.relative_path), :rev => @changeset.revision %>
59 <% end %>
59 <% end %>
60 </td>
60 </td>
61 </tr>
61 </tr>
@@ -1,19 +1,109
1 # Don't change this file. Configuration is done in config/environment.rb and config/environments/*.rb
1 # Don't change this file!
2 # Configure your app in config/environment.rb and config/environments/*.rb
2
3
3 unless defined?(RAILS_ROOT)
4 RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
4 root_path = File.join(File.dirname(__FILE__), '..')
5
5 unless RUBY_PLATFORM =~ /mswin32/
6 module Rails
6 require 'pathname'
7 class << self
7 root_path = Pathname.new(root_path).cleanpath(true).to_s
8 def boot!
9 unless booted?
10 preinitialize
11 pick_boot.run
12 end
13 end
14
15 def booted?
16 defined? Rails::Initializer
17 end
18
19 def pick_boot
20 (vendor_rails? ? VendorBoot : GemBoot).new
21 end
22
23 def vendor_rails?
24 File.exist?("#{RAILS_ROOT}/vendor/rails")
25 end
26
27 def preinitialize
28 load(preinitializer_path) if File.exist?(preinitializer_path)
29 end
30
31 def preinitializer_path
32 "#{RAILS_ROOT}/config/preinitializer.rb"
33 end
8 end
34 end
9 RAILS_ROOT = root_path
10 end
11
35
12 if File.directory?("#{RAILS_ROOT}/vendor/rails")
36 class Boot
13 require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
37 def run
14 else
38 load_initializer
15 require 'rubygems'
39 Rails::Initializer.run(:set_load_path)
16 require 'initializer'
40 end
41 end
42
43 class VendorBoot < Boot
44 def load_initializer
45 require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46 Rails::Initializer.run(:install_gem_spec_stubs)
47 end
48 end
49
50 class GemBoot < Boot
51 def load_initializer
52 self.class.load_rubygems
53 load_rails_gem
54 require 'initializer'
55 end
56
57 def load_rails_gem
58 if version = self.class.gem_version
59 gem 'rails', version
60 else
61 gem 'rails'
62 end
63 rescue Gem::LoadError => load_error
64 $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
65 exit 1
66 end
67
68 class << self
69 def rubygems_version
70 Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
71 end
72
73 def gem_version
74 if defined? RAILS_GEM_VERSION
75 RAILS_GEM_VERSION
76 elsif ENV.include?('RAILS_GEM_VERSION')
77 ENV['RAILS_GEM_VERSION']
78 else
79 parse_gem_version(read_environment_rb)
80 end
81 end
82
83 def load_rubygems
84 require 'rubygems'
85
86 unless rubygems_version >= '0.9.4'
87 $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
88 exit 1
89 end
90
91 rescue LoadError
92 $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
93 exit 1
94 end
95
96 def parse_gem_version(text)
97 $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
98 end
99
100 private
101 def read_environment_rb
102 File.read("#{RAILS_ROOT}/config/environment.rb")
103 end
104 end
105 end
17 end
106 end
18
107
19 Rails::Initializer.run(:set_load_path)
108 # All that for this:
109 Rails.boot!
@@ -5,7 +5,7
5 # ENV['RAILS_ENV'] ||= 'production'
5 # ENV['RAILS_ENV'] ||= 'production'
6
6
7 # Specifies gem version of Rails to use when vendor/rails is not present
7 # Specifies gem version of Rails to use when vendor/rails is not present
8 RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION
8 RAILS_GEM_VERSION = '2.1.0' unless defined? RAILS_GEM_VERSION
9
9
10 # Bootstrap the Rails environment, frameworks, and default configuration
10 # Bootstrap the Rails environment, frameworks, and default configuration
11 require File.join(File.dirname(__FILE__), 'boot')
11 require File.join(File.dirname(__FILE__), 'boot')
@@ -71,32 +71,3 Rails::Initializer.run do |config|
71 config.action_mailer.delivery_method = :smtp
71 config.action_mailer.delivery_method = :smtp
72
72
73 end
73 end
74
75 ActiveRecord::Errors.default_error_messages = {
76 :inclusion => "activerecord_error_inclusion",
77 :exclusion => "activerecord_error_exclusion",
78 :invalid => "activerecord_error_invalid",
79 :confirmation => "activerecord_error_confirmation",
80 :accepted => "activerecord_error_accepted",
81 :empty => "activerecord_error_empty",
82 :blank => "activerecord_error_blank",
83 :too_long => "activerecord_error_too_long",
84 :too_short => "activerecord_error_too_short",
85 :wrong_length => "activerecord_error_wrong_length",
86 :taken => "activerecord_error_taken",
87 :not_a_number => "activerecord_error_not_a_number"
88 }
89
90 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
91
92 Mime::SET << Mime::CSV unless Mime::SET.include?(Mime::CSV)
93 Mime::Type.register 'application/pdf', :pdf
94
95 GLoc.set_config :default_language => :en
96 GLoc.clear_strings
97 GLoc.set_kcode
98 GLoc.load_localized_strings
99 GLoc.set_config(:raise_string_not_found_errors => false)
100
101 require 'redmine'
102
@@ -1,7 +1,7
1 class AddEnumerationsPosition < ActiveRecord::Migration
1 class AddEnumerationsPosition < ActiveRecord::Migration
2 def self.up
2 def self.up
3 add_column(:enumerations, :position, :integer, :default => 1) unless Enumeration.column_names.include?('position')
3 add_column(:enumerations, :position, :integer, :default => 1) unless Enumeration.column_names.include?('position')
4 Enumeration.find(:all).group_by(&:opt).each_value do |enums|
4 Enumeration.find(:all).group_by(&:opt).each do |opt, enums|
5 enums.each_with_index do |enum, i|
5 enums.each_with_index do |enum, i|
6 # do not call model callbacks
6 # do not call model callbacks
7 Enumeration.update_all "position = #{i+1}", {:id => enum.id}
7 Enumeration.update_all "position = #{i+1}", {:id => enum.id}
@@ -1,7 +1,7
1 class AddCustomFieldsPosition < ActiveRecord::Migration
1 class AddCustomFieldsPosition < ActiveRecord::Migration
2 def self.up
2 def self.up
3 add_column(:custom_fields, :position, :integer, :default => 1)
3 add_column(:custom_fields, :position, :integer, :default => 1)
4 CustomField.find(:all).group_by(&:type).each_value do |fields|
4 CustomField.find(:all).group_by(&:type).each do |t, fields|
5 fields.each_with_index do |field, i|
5 fields.each_with_index do |field, i|
6 # do not call model callbacks
6 # do not call model callbacks
7 CustomField.update_all "position = #{i+1}", {:id => field.id}
7 CustomField.update_all "position = #{i+1}", {:id => field.id}
@@ -22,7 +22,7 class TabularFormBuilder < ActionView::Helpers::FormBuilder
22
22
23 def initialize(object_name, object, template, options, proc)
23 def initialize(object_name, object, template, options, proc)
24 set_language_if_valid options.delete(:lang)
24 set_language_if_valid options.delete(:lang)
25 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
25 super
26 end
26 end
27
27
28 (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
28 (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
@@ -38,7 +38,7 class ProjectsControllerTest < Test::Unit::TestCase
38 assert_template 'index'
38 assert_template 'index'
39 assert_not_nil assigns(:project_tree)
39 assert_not_nil assigns(:project_tree)
40 # Root project as hash key
40 # Root project as hash key
41 assert assigns(:project_tree).has_key?(Project.find(1))
41 assert assigns(:project_tree).keys.include?(Project.find(1))
42 # Subproject in corresponding value
42 # Subproject in corresponding value
43 assert assigns(:project_tree)[Project.find(1)].include?(Project.find(3))
43 assert assigns(:project_tree)[Project.find(1)].include?(Project.find(3))
44 end
44 end
@@ -20,8 +20,11 require "#{File.dirname(__FILE__)}/../test_helper"
20 class IssuesTest < ActionController::IntegrationTest
20 class IssuesTest < ActionController::IntegrationTest
21 fixtures :projects,
21 fixtures :projects,
22 :users,
22 :users,
23 :roles,
24 :members,
23 :trackers,
25 :trackers,
24 :projects_trackers,
26 :projects_trackers,
27 :enabled_modules,
25 :issue_statuses,
28 :issue_statuses,
26 :issues,
29 :issues,
27 :enumerations,
30 :enumerations,
@@ -65,20 +65,3 class Test::Unit::TestCase
65 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
65 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
66 end
66 end
67 end
67 end
68
69
70 # ActionController::TestUploadedFile bug
71 # see http://dev.rubyonrails.org/ticket/4635
72 class String
73 def original_filename
74 "testfile.txt"
75 end
76
77 def content_type
78 "text/plain"
79 end
80
81 def read
82 self.to_s
83 end
84 end
@@ -26,7 +26,7 class RoleTest < Test::Unit::TestCase
26
26
27 target = Role.new(:name => 'Target')
27 target = Role.new(:name => 'Target')
28 assert target.save
28 assert target.save
29 assert target.workflows.copy(source)
29 target.workflows.copy(source)
30 target.reload
30 target.reload
31 assert_equal 90, target.workflows.size
31 assert_equal 90, target.workflows.size
32 end
32 end
@@ -26,7 +26,7 class TrackerTest < Test::Unit::TestCase
26
26
27 target = Tracker.new(:name => 'Target')
27 target = Tracker.new(:name => 'Target')
28 assert target.save
28 assert target.save
29 assert target.workflows.copy(source)
29 target.workflows.copy(source)
30 target.reload
30 target.reload
31 assert_equal 89, target.workflows.size
31 assert_equal 89, target.workflows.size
32 end
32 end
@@ -1,182 +1,182
1 require 'rubygems'
1 require 'rubygems'
2
2
3 Gem::manage_gems
3 Gem::manage_gems
4
4
5 require 'rake/rdoctask'
5 require 'rake/rdoctask'
6 require 'rake/packagetask'
6 require 'rake/packagetask'
7 require 'rake/gempackagetask'
7 require 'rake/gempackagetask'
8 require 'rake/testtask'
8 require 'rake/testtask'
9 require 'rake/contrib/rubyforgepublisher'
9 require 'rake/contrib/rubyforgepublisher'
10
10
11 PKG_NAME = 'acts_as_versioned'
11 PKG_NAME = 'acts_as_versioned'
12 PKG_VERSION = '0.3.1'
12 PKG_VERSION = '0.3.1'
13 PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
13 PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
14 PROD_HOST = "technoweenie@bidwell.textdrive.com"
14 PROD_HOST = "technoweenie@bidwell.textdrive.com"
15 RUBY_FORGE_PROJECT = 'ar-versioned'
15 RUBY_FORGE_PROJECT = 'ar-versioned'
16 RUBY_FORGE_USER = 'technoweenie'
16 RUBY_FORGE_USER = 'technoweenie'
17
17
18 desc 'Default: run unit tests.'
18 desc 'Default: run unit tests.'
19 task :default => :test
19 task :default => :test
20
20
21 desc 'Test the calculations plugin.'
21 desc 'Test the calculations plugin.'
22 Rake::TestTask.new(:test) do |t|
22 Rake::TestTask.new(:test) do |t|
23 t.libs << 'lib'
23 t.libs << 'lib'
24 t.pattern = 'test/**/*_test.rb'
24 t.pattern = 'test/**/*_test.rb'
25 t.verbose = true
25 t.verbose = true
26 end
26 end
27
27
28 desc 'Generate documentation for the calculations plugin.'
28 desc 'Generate documentation for the calculations plugin.'
29 Rake::RDocTask.new(:rdoc) do |rdoc|
29 Rake::RDocTask.new(:rdoc) do |rdoc|
30 rdoc.rdoc_dir = 'rdoc'
30 rdoc.rdoc_dir = 'rdoc'
31 rdoc.title = "#{PKG_NAME} -- Simple versioning with active record models"
31 rdoc.title = "#{PKG_NAME} -- Simple versioning with active record models"
32 rdoc.options << '--line-numbers --inline-source'
32 rdoc.options << '--line-numbers --inline-source'
33 rdoc.rdoc_files.include('README', 'CHANGELOG', 'RUNNING_UNIT_TESTS')
33 rdoc.rdoc_files.include('README', 'CHANGELOG', 'RUNNING_UNIT_TESTS')
34 rdoc.rdoc_files.include('lib/**/*.rb')
34 rdoc.rdoc_files.include('lib/**/*.rb')
35 end
35 end
36
36
37 spec = Gem::Specification.new do |s|
37 spec = Gem::Specification.new do |s|
38 s.name = PKG_NAME
38 s.name = PKG_NAME
39 s.version = PKG_VERSION
39 s.version = PKG_VERSION
40 s.platform = Gem::Platform::RUBY
40 s.platform = Gem::Platform::RUBY
41 s.summary = "Simple versioning with active record models"
41 s.summary = "Simple versioning with active record models"
42 s.files = FileList["{lib,test}/**/*"].to_a + %w(README MIT-LICENSE CHANGELOG RUNNING_UNIT_TESTS)
42 s.files = FileList["{lib,test}/**/*"].to_a + %w(README MIT-LICENSE CHANGELOG RUNNING_UNIT_TESTS)
43 s.files.delete "acts_as_versioned_plugin.sqlite.db"
43 s.files.delete "acts_as_versioned_plugin.sqlite.db"
44 s.files.delete "acts_as_versioned_plugin.sqlite3.db"
44 s.files.delete "acts_as_versioned_plugin.sqlite3.db"
45 s.files.delete "test/debug.log"
45 s.files.delete "test/debug.log"
46 s.require_path = 'lib'
46 s.require_path = 'lib'
47 s.autorequire = 'acts_as_versioned'
47 s.autorequire = 'acts_as_versioned'
48 s.has_rdoc = true
48 s.has_rdoc = true
49 s.test_files = Dir['test/**/*_test.rb']
49 s.test_files = Dir['test/**/*_test.rb']
50 s.add_dependency 'activerecord', '>= 1.10.1'
50 s.add_dependency 'activerecord', '>= 1.10.1'
51 s.add_dependency 'activesupport', '>= 1.1.1'
51 s.add_dependency 'activesupport', '>= 1.1.1'
52 s.author = "Rick Olson"
52 s.author = "Rick Olson"
53 s.email = "technoweenie@gmail.com"
53 s.email = "technoweenie@gmail.com"
54 s.homepage = "http://techno-weenie.net"
54 s.homepage = "http://techno-weenie.net"
55 end
55 end
56
56
57 Rake::GemPackageTask.new(spec) do |pkg|
57 Rake::GemPackageTask.new(spec) do |pkg|
58 pkg.need_tar = true
58 pkg.need_tar = true
59 end
59 end
60
60
61 desc "Publish the API documentation"
61 desc "Publish the API documentation"
62 task :pdoc => [:rdoc] do
62 task :pdoc => [:rdoc] do
63 Rake::RubyForgePublisher.new(RUBY_FORGE_PROJECT, RUBY_FORGE_USER).upload
63 Rake::RubyForgePublisher.new(RUBY_FORGE_PROJECT, RUBY_FORGE_USER).upload
64 end
64 end
65
65
66 desc 'Publish the gem and API docs'
66 desc 'Publish the gem and API docs'
67 task :publish => [:pdoc, :rubyforge_upload]
67 task :publish => [:pdoc, :rubyforge_upload]
68
68
69 desc "Publish the release files to RubyForge."
69 desc "Publish the release files to RubyForge."
70 task :rubyforge_upload => :package do
70 task :rubyforge_upload => :package do
71 files = %w(gem tgz).map { |ext| "pkg/#{PKG_FILE_NAME}.#{ext}" }
71 files = %w(gem tgz).map { |ext| "pkg/#{PKG_FILE_NAME}.#{ext}" }
72
72
73 if RUBY_FORGE_PROJECT then
73 if RUBY_FORGE_PROJECT then
74 require 'net/http'
74 require 'net/http'
75 require 'open-uri'
75 require 'open-uri'
76
76
77 project_uri = "http://rubyforge.org/projects/#{RUBY_FORGE_PROJECT}/"
77 project_uri = "http://rubyforge.org/projects/#{RUBY_FORGE_PROJECT}/"
78 project_data = open(project_uri) { |data| data.read }
78 project_data = open(project_uri) { |data| data.read }
79 group_id = project_data[/[?&]group_id=(\d+)/, 1]
79 group_id = project_data[/[?&]group_id=(\d+)/, 1]
80 raise "Couldn't get group id" unless group_id
80 raise "Couldn't get group id" unless group_id
81
81
82 # This echos password to shell which is a bit sucky
82 # This echos password to shell which is a bit sucky
83 if ENV["RUBY_FORGE_PASSWORD"]
83 if ENV["RUBY_FORGE_PASSWORD"]
84 password = ENV["RUBY_FORGE_PASSWORD"]
84 password = ENV["RUBY_FORGE_PASSWORD"]
85 else
85 else
86 print "#{RUBY_FORGE_USER}@rubyforge.org's password: "
86 print "#{RUBY_FORGE_USER}@rubyforge.org's password: "
87 password = STDIN.gets.chomp
87 password = STDIN.gets.chomp
88 end
88 end
89
89
90 login_response = Net::HTTP.start("rubyforge.org", 80) do |http|
90 login_response = Net::HTTP.start("rubyforge.org", 80) do |http|
91 data = [
91 data = [
92 "login=1",
92 "login=1",
93 "form_loginname=#{RUBY_FORGE_USER}",
93 "form_loginname=#{RUBY_FORGE_USER}",
94 "form_pw=#{password}"
94 "form_pw=#{password}"
95 ].join("&")
95 ].join("&")
96 http.post("/account/login.php", data)
96 http.post("/account/login.php", data)
97 end
97 end
98
98
99 cookie = login_response["set-cookie"]
99 cookie = login_response["set-cookie"]
100 raise "Login failed" unless cookie
100 raise "Login failed" unless cookie
101 headers = { "Cookie" => cookie }
101 headers = { "Cookie" => cookie }
102
102
103 release_uri = "http://rubyforge.org/frs/admin/?group_id=#{group_id}"
103 release_uri = "http://rubyforge.org/frs/admin/?group_id=#{group_id}"
104 release_data = open(release_uri, headers) { |data| data.read }
104 release_data = open(release_uri, headers) { |data| data.read }
105 package_id = release_data[/[?&]package_id=(\d+)/, 1]
105 package_id = release_data[/[?&]package_id=(\d+)/, 1]
106 raise "Couldn't get package id" unless package_id
106 raise "Couldn't get package id" unless package_id
107
107
108 first_file = true
108 first_file = true
109 release_id = ""
109 release_id = ""
110
110
111 files.each do |filename|
111 files.each do |filename|
112 basename = File.basename(filename)
112 basename = File.basename(filename)
113 file_ext = File.extname(filename)
113 file_ext = File.extname(filename)
114 file_data = File.open(filename, "rb") { |file| file.read }
114 file_data = File.open(filename, "rb") { |file| file.read }
115
115
116 puts "Releasing #{basename}..."
116 puts "Releasing #{basename}..."
117
117
118 release_response = Net::HTTP.start("rubyforge.org", 80) do |http|
118 release_response = Net::HTTP.start("rubyforge.org", 80) do |http|
119 release_date = Time.now.strftime("%Y-%m-%d %H:%M")
119 release_date = Time.now.strftime("%Y-%m-%d %H:%M")
120 type_map = {
120 type_map = {
121 ".zip" => "3000",
121 ".zip" => "3000",
122 ".tgz" => "3110",
122 ".tgz" => "3110",
123 ".gz" => "3110",
123 ".gz" => "3110",
124 ".gem" => "1400"
124 ".gem" => "1400"
125 }; type_map.default = "9999"
125 }; type_map.default = "9999"
126 type = type_map[file_ext]
126 type = type_map[file_ext]
127 boundary = "rubyqMY6QN9bp6e4kS21H4y0zxcvoor"
127 boundary = "rubyqMY6QN9bp6e4kS21H4y0zxcvoor"
128
128
129 query_hash = if first_file then
129 query_hash = if first_file then
130 {
130 {
131 "group_id" => group_id,
131 "group_id" => group_id,
132 "package_id" => package_id,
132 "package_id" => package_id,
133 "release_name" => PKG_FILE_NAME,
133 "release_name" => PKG_FILE_NAME,
134 "release_date" => release_date,
134 "release_date" => release_date,
135 "type_id" => type,
135 "type_id" => type,
136 "processor_id" => "8000", # Any
136 "processor_id" => "8000", # Any
137 "release_notes" => "",
137 "release_notes" => "",
138 "release_changes" => "",
138 "release_changes" => "",
139 "preformatted" => "1",
139 "preformatted" => "1",
140 "submit" => "1"
140 "submit" => "1"
141 }
141 }
142 else
142 else
143 {
143 {
144 "group_id" => group_id,
144 "group_id" => group_id,
145 "release_id" => release_id,
145 "release_id" => release_id,
146 "package_id" => package_id,
146 "package_id" => package_id,
147 "step2" => "1",
147 "step2" => "1",
148 "type_id" => type,
148 "type_id" => type,
149 "processor_id" => "8000", # Any
149 "processor_id" => "8000", # Any
150 "submit" => "Add This File"
150 "submit" => "Add This File"
151 }
151 }
152 end
152 end
153
153
154 query = "?" + query_hash.map do |(name, value)|
154 query = "?" + query_hash.map do |(name, value)|
155 [name, URI.encode(value)].join("=")
155 [name, URI.encode(value)].join("=")
156 end.join("&")
156 end.join("&")
157
157
158 data = [
158 data = [
159 "--" + boundary,
159 "--" + boundary,
160 "Content-Disposition: form-data; name=\"userfile\"; filename=\"#{basename}\"",
160 "Content-Disposition: form-data; name=\"userfile\"; filename=\"#{basename}\"",
161 "Content-Type: application/octet-stream",
161 "Content-Type: application/octet-stream",
162 "Content-Transfer-Encoding: binary",
162 "Content-Transfer-Encoding: binary",
163 "", file_data, ""
163 "", file_data, ""
164 ].join("\x0D\x0A")
164 ].join("\x0D\x0A")
165
165
166 release_headers = headers.merge(
166 release_headers = headers.merge(
167 "Content-Type" => "multipart/form-data; boundary=#{boundary}"
167 "Content-Type" => "multipart/form-data; boundary=#{boundary}"
168 )
168 )
169
169
170 target = first_file ? "/frs/admin/qrs.php" : "/frs/admin/editrelease.php"
170 target = first_file ? "/frs/admin/qrs.php" : "/frs/admin/editrelease.php"
171 http.post(target + query, data, release_headers)
171 http.post(target + query, data, release_headers)
172 end
172 end
173
173
174 if first_file then
174 if first_file then
175 release_id = release_response.body[/release_id=(\d+)/, 1]
175 release_id = release_response.body[/release_id=(\d+)/, 1]
176 raise("Couldn't get release id") unless release_id
176 raise("Couldn't get release id") unless release_id
177 end
177 end
178
178
179 first_file = false
179 first_file = false
180 end
180 end
181 end
181 end
182 end No newline at end of file
182 end
@@ -22,7 +22,7
22 module ActiveRecord #:nodoc:
22 module ActiveRecord #:nodoc:
23 module Acts #:nodoc:
23 module Acts #:nodoc:
24 # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a
24 # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a
25 # versioned table ready and that your model has a version field. This works with optimisic locking if the lock_version
25 # versioned table ready and that your model has a version field. This works with optimistic locking if the lock_version
26 # column is present as well.
26 # column is present as well.
27 #
27 #
28 # The class for the versioned model is derived the first time it is seen. Therefore, if you change your database schema you have to restart
28 # The class for the versioned model is derived the first time it is seen. Therefore, if you change your database schema you have to restart
@@ -49,9 +49,24 module ActiveRecord #:nodoc:
49 # page.revert_to(page.versions.last) # using versioned instance
49 # page.revert_to(page.versions.last) # using versioned instance
50 # page.title # => 'hello world'
50 # page.title # => 'hello world'
51 #
51 #
52 # page.versions.earliest # efficient query to find the first version
53 # page.versions.latest # efficient query to find the most recently created version
54 #
55 #
56 # Simple Queries to page between versions
57 #
58 # page.versions.before(version)
59 # page.versions.after(version)
60 #
61 # Access the previous/next versions from the versioned model itself
62 #
63 # version = page.versions.latest
64 # version.previous # go back one version
65 # version.next # go forward one version
66 #
52 # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
67 # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
53 module Versioned
68 module Versioned
54 CALLBACKS = [:set_new_version, :save_version_on_create, :save_version?, :clear_changed_attributes]
69 CALLBACKS = [:set_new_version, :save_version_on_create, :save_version?, :clear_altered_attributes]
55 def self.included(base) # :nodoc:
70 def self.included(base) # :nodoc:
56 base.extend ClassMethods
71 base.extend ClassMethods
57 end
72 end
@@ -80,7 +95,7 module ActiveRecord #:nodoc:
80 # end
95 # end
81 #
96 #
82 # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
97 # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
83 # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
98 # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
84 # Use this instead if you want to write your own attribute setters (and ignore if_changed):
99 # Use this instead if you want to write your own attribute setters (and ignore if_changed):
85 #
100 #
86 # def name=(new_name)
101 # def name=(new_name)
@@ -133,7 +148,7 module ActiveRecord #:nodoc:
133 # # that create_table does
148 # # that create_table does
134 # Post.create_versioned_table
149 # Post.create_versioned_table
135 # end
150 # end
136 #
151 #
137 # def self.down
152 # def self.down
138 # Post.drop_versioned_table
153 # Post.drop_versioned_table
139 # end
154 # end
@@ -157,11 +172,11 module ActiveRecord #:nodoc:
157 return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
172 return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
158
173
159 send :include, ActiveRecord::Acts::Versioned::ActMethods
174 send :include, ActiveRecord::Acts::Versioned::ActMethods
160
175
161 cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
176 cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
162 :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
177 :version_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
163 :version_association_options
178 :version_association_options
164
179
165 # legacy
180 # legacy
166 alias_method :non_versioned_fields, :non_versioned_columns
181 alias_method :non_versioned_fields, :non_versioned_columns
167 alias_method :non_versioned_fields=, :non_versioned_columns=
182 alias_method :non_versioned_fields=, :non_versioned_columns=
@@ -171,7 +186,7 module ActiveRecord #:nodoc:
171 alias_method :non_versioned_fields=, :non_versioned_columns=
186 alias_method :non_versioned_fields=, :non_versioned_columns=
172 end
187 end
173
188
174 send :attr_accessor, :changed_attributes
189 send :attr_accessor, :altered_attributes
175
190
176 self.versioned_class_name = options[:class_name] || "Version"
191 self.versioned_class_name = options[:class_name] || "Version"
177 self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
192 self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
@@ -184,8 +199,7 module ActiveRecord #:nodoc:
184 self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
199 self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
185 self.version_association_options = {
200 self.version_association_options = {
186 :class_name => "#{self.to_s}::#{versioned_class_name}",
201 :class_name => "#{self.to_s}::#{versioned_class_name}",
187 :foreign_key => "#{versioned_foreign_key}",
202 :foreign_key => versioned_foreign_key,
188 :order => 'version',
189 :dependent => :delete_all
203 :dependent => :delete_all
190 }.merge(options[:association_options] || {})
204 }.merge(options[:association_options] || {})
191
205
@@ -194,20 +208,30 module ActiveRecord #:nodoc:
194 silence_warnings do
208 silence_warnings do
195 self.const_set(extension_module_name, Module.new(&extension))
209 self.const_set(extension_module_name, Module.new(&extension))
196 end
210 end
197
211
198 options[:extend] = self.const_get(extension_module_name)
212 options[:extend] = self.const_get(extension_module_name)
199 end
213 end
200
214
201 class_eval do
215 class_eval do
202 has_many :versions, version_association_options
216 has_many :versions, version_association_options do
217 # finds earliest version of this record
218 def earliest
219 @earliest ||= find(:first, :order => 'version')
220 end
221
222 # find latest version of this record
223 def latest
224 @latest ||= find(:first, :order => 'version desc')
225 end
226 end
203 before_save :set_new_version
227 before_save :set_new_version
204 after_create :save_version_on_create
228 after_create :save_version_on_create
205 after_update :save_version
229 after_update :save_version
206 after_save :clear_old_versions
230 after_save :clear_old_versions
207 after_save :clear_changed_attributes
231 after_save :clear_altered_attributes
208
232
209 unless options[:if_changed].nil?
233 unless options[:if_changed].nil?
210 self.track_changed_attributes = true
234 self.track_altered_attributes = true
211 options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
235 options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
212 options[:if_changed].each do |attr_name|
236 options[:if_changed].each do |attr_name|
213 define_method("#{attr_name}=") do |value|
237 define_method("#{attr_name}=") do |value|
@@ -215,15 +239,40 module ActiveRecord #:nodoc:
215 end
239 end
216 end
240 end
217 end
241 end
218
242
219 include options[:extend] if options[:extend].is_a?(Module)
243 include options[:extend] if options[:extend].is_a?(Module)
220 end
244 end
221
245
222 # create the dynamic versioned model
246 # create the dynamic versioned model
223 const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
247 const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
224 def self.reloadable? ; false ; end
248 def self.reloadable? ; false ; end
249 # find first version before the given version
250 def self.before(version)
251 find :first, :order => 'version desc',
252 :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version]
253 end
254
255 # find first version after the given version.
256 def self.after(version)
257 find :first, :order => 'version',
258 :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version]
259 end
260
261 def previous
262 self.class.before(self)
263 end
264
265 def next
266 self.class.after(self)
267 end
268
269 def versions_count
270 page.version
271 end
225 end
272 end
226
273
274 versioned_class.cattr_accessor :original_class
275 versioned_class.original_class = self
227 versioned_class.set_table_name versioned_table_name
276 versioned_class.set_table_name versioned_table_name
228 versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
277 versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
229 :class_name => "::#{self.to_s}",
278 :class_name => "::#{self.to_s}",
@@ -232,17 +281,22 module ActiveRecord #:nodoc:
232 versioned_class.set_sequence_name version_sequence_name if version_sequence_name
281 versioned_class.set_sequence_name version_sequence_name if version_sequence_name
233 end
282 end
234 end
283 end
235
284
236 module ActMethods
285 module ActMethods
237 def self.included(base) # :nodoc:
286 def self.included(base) # :nodoc:
238 base.extend ClassMethods
287 base.extend ClassMethods
239 end
288 end
240
289
290 # Finds a specific version of this record
291 def find_version(version = nil)
292 self.class.find_version(id, version)
293 end
294
241 # Saves a version of the model if applicable
295 # Saves a version of the model if applicable
242 def save_version
296 def save_version
243 save_version_on_create if save_version?
297 save_version_on_create if save_version?
244 end
298 end
245
299
246 # Saves a version of the model in the versioned table. This is called in the after_save callback by default
300 # Saves a version of the model in the versioned table. This is called in the after_save callback by default
247 def save_version_on_create
301 def save_version_on_create
248 rev = self.class.versioned_class.new
302 rev = self.class.versioned_class.new
@@ -263,16 +317,8 module ActiveRecord #:nodoc:
263 end
317 end
264 end
318 end
265
319
266 # Finds a specific version of this model.
320 def versions_count
267 def find_version(version)
321 version
268 return version if version.is_a?(self.class.versioned_class)
269 return nil if version.is_a?(ActiveRecord::Base)
270 find_versions(:conditions => ['version = ?', version], :limit => 1).first
271 end
272
273 # Finds versions of this model. Takes an options hash like <tt>find</tt>
274 def find_versions(options = {})
275 versions.find(:all, options)
276 end
322 end
277
323
278 # Reverts a model to a given version. Takes either a version number or an instance of the versioned model
324 # Reverts a model to a given version. Takes either a version number or an instance of the versioned model
@@ -280,14 +326,14 module ActiveRecord #:nodoc:
280 if version.is_a?(self.class.versioned_class)
326 if version.is_a?(self.class.versioned_class)
281 return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
327 return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
282 else
328 else
283 return false unless version = find_version(version)
329 return false unless version = versions.find_by_version(version)
284 end
330 end
285 self.clone_versioned_model(version, self)
331 self.clone_versioned_model(version, self)
286 self.send("#{self.class.version_column}=", version.version)
332 self.send("#{self.class.version_column}=", version.version)
287 true
333 true
288 end
334 end
289
335
290 # Reverts a model to a given version and saves the model.
336 # Reverts a model to a given version and saves the model.
291 # Takes either a version number or an instance of the versioned model
337 # Takes either a version number or an instance of the versioned model
292 def revert_to!(version)
338 def revert_to!(version)
293 revert_to(version) ? save_without_revision : false
339 revert_to(version) ? save_without_revision : false
@@ -313,36 +359,36 module ActiveRecord #:nodoc:
313 def versioned_attributes
359 def versioned_attributes
314 self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
360 self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
315 end
361 end
316
362
317 # If called with no parameters, gets whether the current model has changed and needs to be versioned.
363 # If called with no parameters, gets whether the current model has changed and needs to be versioned.
318 # If called with a single parameter, gets whether the parameter has changed.
364 # If called with a single parameter, gets whether the parameter has changed.
319 def changed?(attr_name = nil)
365 def changed?(attr_name = nil)
320 attr_name.nil? ?
366 attr_name.nil? ?
321 (!self.class.track_changed_attributes || (changed_attributes && changed_attributes.length > 0)) :
367 (!self.class.track_altered_attributes || (altered_attributes && altered_attributes.length > 0)) :
322 (changed_attributes && changed_attributes.include?(attr_name.to_s))
368 (altered_attributes && altered_attributes.include?(attr_name.to_s))
323 end
369 end
324
370
325 # keep old dirty? method
371 # keep old dirty? method
326 alias_method :dirty?, :changed?
372 alias_method :dirty?, :changed?
327
373
328 # Clones a model. Used when saving a new version or reverting a model's version.
374 # Clones a model. Used when saving a new version or reverting a model's version.
329 def clone_versioned_model(orig_model, new_model)
375 def clone_versioned_model(orig_model, new_model)
330 self.versioned_attributes.each do |key|
376 self.versioned_attributes.each do |key|
331 new_model.send("#{key}=", orig_model.attributes[key]) if orig_model.has_attribute?(key)
377 new_model.send("#{key}=", orig_model.send(key)) if orig_model.has_attribute?(key)
332 end
378 end
333
379
334 if orig_model.is_a?(self.class.versioned_class)
380 if orig_model.is_a?(self.class.versioned_class)
335 new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
381 new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
336 elsif new_model.is_a?(self.class.versioned_class)
382 elsif new_model.is_a?(self.class.versioned_class)
337 new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
383 new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
338 end
384 end
339 end
385 end
340
386
341 # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
387 # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
342 def save_version?
388 def save_version?
343 version_condition_met? && changed?
389 version_condition_met? && changed?
344 end
390 end
345
391
346 # Checks condition set in the :if option to check whether a revision should be created or not. Override this for
392 # Checks condition set in the :if option to check whether a revision should be created or not. Override this for
347 # custom version condition checking.
393 # custom version condition checking.
348 def version_condition_met?
394 def version_condition_met?
@@ -353,7 +399,7 module ActiveRecord #:nodoc:
353 version_condition.call(self)
399 version_condition.call(self)
354 else
400 else
355 version_condition
401 version_condition
356 end
402 end
357 end
403 end
358
404
359 # Executes the block with the versioning callbacks disabled.
405 # Executes the block with the versioning callbacks disabled.
@@ -378,43 +424,45 module ActiveRecord #:nodoc:
378
424
379 def empty_callback() end #:nodoc:
425 def empty_callback() end #:nodoc:
380
426
381 protected
427 protected
382 # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
428 # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
383 def set_new_version
429 def set_new_version
384 self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?)
430 self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?)
385 end
431 end
386
432
387 # Gets the next available version for the current record, or 1 for a new record
433 # Gets the next available version for the current record, or 1 for a new record
388 def next_version
434 def next_version
389 return 1 if new_record?
435 return 1 if new_record?
390 (versions.calculate(:max, :version) || 0) + 1
436 (versions.calculate(:max, :version) || 0) + 1
391 end
437 end
392
438
393 # clears current changed attributes. Called after save.
439 # clears current changed attributes. Called after save.
394 def clear_changed_attributes
440 def clear_altered_attributes
395 self.changed_attributes = []
441 self.altered_attributes = []
396 end
442 end
397
443
398 def write_changed_attribute(attr_name, attr_value)
444 def write_changed_attribute(attr_name, attr_value)
399 # Convert to db type for comparison. Avoids failing Float<=>String comparisons.
445 # Convert to db type for comparison. Avoids failing Float<=>String comparisons.
400 attr_value_for_db = self.class.columns_hash[attr_name.to_s].type_cast(attr_value)
446 attr_value_for_db = self.class.columns_hash[attr_name.to_s].type_cast(attr_value)
401 (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) || self.send(attr_name) == attr_value_for_db
447 (self.altered_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) || self.send(attr_name) == attr_value_for_db
402 write_attribute(attr_name, attr_value_for_db)
448 write_attribute(attr_name, attr_value_for_db)
403 end
449 end
404
450
405 private
406 CALLBACKS.each do |attr_name|
407 alias_method "orig_#{attr_name}".to_sym, attr_name
408 end
409
410 module ClassMethods
451 module ClassMethods
411 # Finds a specific version of a specific row of this model
452 # Finds a specific version of a specific row of this model
412 def find_version(id, version)
453 def find_version(id, version = nil)
413 find_versions(id,
454 return find(id) unless version
414 :conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version],
455
415 :limit => 1).first
456 conditions = ["#{versioned_foreign_key} = ? AND version = ?", id, version]
457 options = { :conditions => conditions, :limit => 1 }
458
459 if result = find_versions(id, options).first
460 result
461 else
462 raise RecordNotFound, "Couldn't find #{name} with ID=#{id} and VERSION=#{version}"
463 end
416 end
464 end
417
465
418 # Finds versions of a specific model. Takes an options hash like <tt>find</tt>
466 # Finds versions of a specific model. Takes an options hash like <tt>find</tt>
419 def find_versions(id, options = {})
467 def find_versions(id, options = {})
420 versioned_class.find :all, {
468 versioned_class.find :all, {
@@ -426,7 +474,7 module ActiveRecord #:nodoc:
426 def versioned_columns
474 def versioned_columns
427 self.columns.select { |c| !non_versioned_columns.include?(c.name) }
475 self.columns.select { |c| !non_versioned_columns.include?(c.name) }
428 end
476 end
429
477
430 # Returns an instance of the dynamic versioned model
478 # Returns an instance of the dynamic versioned model
431 def versioned_class
479 def versioned_class
432 const_get versioned_class_name
480 const_get versioned_class_name
@@ -438,36 +486,40 module ActiveRecord #:nodoc:
438 if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
486 if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
439 self.connection.add_column table_name, :version, :integer
487 self.connection.add_column table_name, :version, :integer
440 end
488 end
441
489
442 self.connection.create_table(versioned_table_name, create_table_options) do |t|
490 self.connection.create_table(versioned_table_name, create_table_options) do |t|
443 t.column versioned_foreign_key, :integer
491 t.column versioned_foreign_key, :integer
444 t.column :version, :integer
492 t.column :version, :integer
445 end
493 end
446
494
447 updated_col = nil
495 updated_col = nil
448 self.versioned_columns.each do |col|
496 self.versioned_columns.each do |col|
449 updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
497 updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
450 self.connection.add_column versioned_table_name, col.name, col.type,
498 self.connection.add_column versioned_table_name, col.name, col.type,
451 :limit => col.limit,
499 :limit => col.limit,
452 :default => col.default
500 :default => col.default,
501 :scale => col.scale,
502 :precision => col.precision
453 end
503 end
454
504
455 if type_col = self.columns_hash[inheritance_column]
505 if type_col = self.columns_hash[inheritance_column]
456 self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
506 self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
457 :limit => type_col.limit,
507 :limit => type_col.limit,
458 :default => type_col.default
508 :default => type_col.default,
509 :scale => type_col.scale,
510 :precision => type_col.precision
459 end
511 end
460
512
461 if updated_col.nil?
513 if updated_col.nil?
462 self.connection.add_column versioned_table_name, :updated_at, :timestamp
514 self.connection.add_column versioned_table_name, :updated_at, :timestamp
463 end
515 end
464 end
516 end
465
517
466 # Rake migration task to drop the versioned table
518 # Rake migration task to drop the versioned table
467 def drop_versioned_table
519 def drop_versioned_table
468 self.connection.drop_table versioned_table_name
520 self.connection.drop_table versioned_table_name
469 end
521 end
470
522
471 # Executes the block with the versioning callbacks disabled.
523 # Executes the block with the versioning callbacks disabled.
472 #
524 #
473 # Foo.without_revision do
525 # Foo.without_revision do
@@ -476,17 +528,18 module ActiveRecord #:nodoc:
476 #
528 #
477 def without_revision(&block)
529 def without_revision(&block)
478 class_eval do
530 class_eval do
479 CALLBACKS.each do |attr_name|
531 CALLBACKS.each do |attr_name|
532 alias_method "orig_#{attr_name}".to_sym, attr_name
480 alias_method attr_name, :empty_callback
533 alias_method attr_name, :empty_callback
481 end
534 end
482 end
535 end
483 result = block.call
536 block.call
537 ensure
484 class_eval do
538 class_eval do
485 CALLBACKS.each do |attr_name|
539 CALLBACKS.each do |attr_name|
486 alias_method attr_name, "orig_#{attr_name}".to_sym
540 alias_method attr_name, "orig_#{attr_name}".to_sym
487 end
541 end
488 end
542 end
489 result
490 end
543 end
491
544
492 # Turns off optimistic locking for the duration of the block
545 # Turns off optimistic locking for the duration of the block
@@ -501,7 +554,7 module ActiveRecord #:nodoc:
501 result = block.call
554 result = block.call
502 ActiveRecord::Base.lock_optimistically = true if current
555 ActiveRecord::Base.lock_optimistically = true if current
503 result
556 result
504 end
557 end
505 end
558 end
506 end
559 end
507 end
560 end
@@ -1,12 +1,21
1 $:.unshift(File.dirname(__FILE__) + '/../../../rails/activesupport/lib')
2 $:.unshift(File.dirname(__FILE__) + '/../../../rails/activerecord/lib')
1 $:.unshift(File.dirname(__FILE__) + '/../lib')
3 $:.unshift(File.dirname(__FILE__) + '/../lib')
2
3 require 'test/unit'
4 require 'test/unit'
4 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
5 begin
5 require 'active_record/fixtures'
6 require 'active_support'
7 require 'active_record'
8 require 'active_record/fixtures'
9 rescue LoadError
10 require 'rubygems'
11 retry
12 end
13 require 'acts_as_versioned'
6
14
7 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
15 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
8 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
16 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
9 ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
17 ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
18 ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
10
19
11 load(File.dirname(__FILE__) + "/schema.rb")
20 load(File.dirname(__FILE__) + "/schema.rb")
12
21
@@ -19,17 +28,9 if ENV['DB'] == 'postgresql'
19 end
28 end
20
29
21 Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
30 Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
22 $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
31 $:.unshift(Test::Unit::TestCase.fixture_path)
23
32
24 class Test::Unit::TestCase #:nodoc:
33 class Test::Unit::TestCase #:nodoc:
25 def create_fixtures(*table_names)
26 if block_given?
27 Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
28 else
29 Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
30 end
31 end
32
33 # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
34 # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
34 self.use_transactional_fixtures = true
35 self.use_transactional_fixtures = true
35
36
@@ -1,6 +1,6
1 class Widget < ActiveRecord::Base
1 class Widget < ActiveRecord::Base
2 acts_as_versioned :sequence_name => 'widgets_seq', :association_options => {
2 acts_as_versioned :sequence_name => 'widgets_seq', :association_options => {
3 :dependent => nil, :order => 'version desc'
3 :dependent => :nullify, :order => 'version desc'
4 }
4 }
5 non_versioned_columns << 'foo'
5 non_versioned_columns << 'foo'
6 end No newline at end of file
6 end
@@ -9,9 +9,14 if ActiveRecord::Base.connection.supports_migrations?
9 class MigrationTest < Test::Unit::TestCase
9 class MigrationTest < Test::Unit::TestCase
10 self.use_transactional_fixtures = false
10 self.use_transactional_fixtures = false
11 def teardown
11 def teardown
12 ActiveRecord::Base.connection.initialize_schema_information
12 if ActiveRecord::Base.connection.respond_to?(:initialize_schema_information)
13 ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0"
13 ActiveRecord::Base.connection.initialize_schema_information
14
14 ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0"
15 else
16 ActiveRecord::Base.connection.initialize_schema_migrations_table
17 ActiveRecord::Base.connection.assume_migrated_upto_version(0)
18 end
19
15 Thing.connection.drop_table "things" rescue nil
20 Thing.connection.drop_table "things" rescue nil
16 Thing.connection.drop_table "thing_versions" rescue nil
21 Thing.connection.drop_table "thing_versions" rescue nil
17 Thing.reset_column_information
22 Thing.reset_column_information
@@ -21,8 +26,17 if ActiveRecord::Base.connection.supports_migrations?
21 assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' }
26 assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' }
22 # take 'er up
27 # take 'er up
23 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
28 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
24 t = Thing.create :title => 'blah blah'
29 t = Thing.create :title => 'blah blah', :price => 123.45, :type => 'Thing'
25 assert_equal 1, t.versions.size
30 assert_equal 1, t.versions.size
31
32 # check that the price column has remembered its value correctly
33 assert_equal t.price, t.versions.first.price
34 assert_equal t.title, t.versions.first.title
35 assert_equal t[:type], t.versions.first[:type]
36
37 # make sure that the precision of the price column has been preserved
38 assert_equal 7, Thing::Version.columns.find{|c| c.name == "price"}.precision
39 assert_equal 2, Thing::Version.columns.find{|c| c.name == "price"}.scale
26
40
27 # now lets take 'er back down
41 # now lets take 'er back down
28 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')
42 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')
@@ -4,9 +4,10 require File.join(File.dirname(__FILE__), 'fixtures/widget')
4
4
5 class VersionedTest < Test::Unit::TestCase
5 class VersionedTest < Test::Unit::TestCase
6 fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions, :authors, :landmarks, :landmark_versions
6 fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions, :authors, :landmarks, :landmark_versions
7 set_fixture_class :page_versions => Page::Version
7
8
8 def test_saves_versioned_copy
9 def test_saves_versioned_copy
9 p = Page.create :title => 'first title', :body => 'first body'
10 p = Page.create! :title => 'first title', :body => 'first body'
10 assert !p.new_record?
11 assert !p.new_record?
11 assert_equal 1, p.versions.size
12 assert_equal 1, p.versions.size
12 assert_equal 1, p.version
13 assert_equal 1, p.version
@@ -16,13 +17,13 class VersionedTest < Test::Unit::TestCase
16 def test_saves_without_revision
17 def test_saves_without_revision
17 p = pages(:welcome)
18 p = pages(:welcome)
18 old_versions = p.versions.count
19 old_versions = p.versions.count
19
20
20 p.save_without_revision
21 p.save_without_revision
21
22
22 p.without_revision do
23 p.without_revision do
23 p.update_attributes :title => 'changed'
24 p.update_attributes :title => 'changed'
24 end
25 end
25
26
26 assert_equal old_versions, p.versions.count
27 assert_equal old_versions, p.versions.count
27 end
28 end
28
29
@@ -30,7 +31,7 class VersionedTest < Test::Unit::TestCase
30 p = pages(:welcome)
31 p = pages(:welcome)
31 assert_equal 24, p.version
32 assert_equal 24, p.version
32 assert_equal 'Welcome to the weblog', p.title
33 assert_equal 'Welcome to the weblog', p.title
33
34
34 assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23"
35 assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23"
35 assert_equal 23, p.version
36 assert_equal 23, p.version
36 assert_equal 'Welcome to the weblg', p.title
37 assert_equal 'Welcome to the weblg', p.title
@@ -57,56 +58,56 class VersionedTest < Test::Unit::TestCase
57 p = pages(:welcome)
58 p = pages(:welcome)
58 assert_equal 24, p.version
59 assert_equal 24, p.version
59 assert_equal 'Welcome to the weblog', p.title
60 assert_equal 'Welcome to the weblog', p.title
60
61
61 assert p.revert_to!(p.versions.first), "Couldn't revert to 23"
62 assert p.revert_to!(p.versions.first), "Couldn't revert to 23"
62 assert_equal 23, p.version
63 assert_equal 23, p.version
63 assert_equal 'Welcome to the weblg', p.title
64 assert_equal 'Welcome to the weblg', p.title
64 end
65 end
65
66
66 def test_rollback_fails_with_invalid_revision
67 def test_rollback_fails_with_invalid_revision
67 p = locked_pages(:welcome)
68 p = locked_pages(:welcome)
68 assert !p.revert_to!(locked_pages(:thinking))
69 assert !p.revert_to!(locked_pages(:thinking))
69 end
70 end
70
71
71 def test_saves_versioned_copy_with_options
72 def test_saves_versioned_copy_with_options
72 p = LockedPage.create :title => 'first title'
73 p = LockedPage.create! :title => 'first title'
73 assert !p.new_record?
74 assert !p.new_record?
74 assert_equal 1, p.versions.size
75 assert_equal 1, p.versions.size
75 assert_instance_of LockedPage.versioned_class, p.versions.first
76 assert_instance_of LockedPage.versioned_class, p.versions.first
76 end
77 end
77
78
78 def test_rollback_with_version_number_with_options
79 def test_rollback_with_version_number_with_options
79 p = locked_pages(:welcome)
80 p = locked_pages(:welcome)
80 assert_equal 'Welcome to the weblog', p.title
81 assert_equal 'Welcome to the weblog', p.title
81 assert_equal 'LockedPage', p.versions.first.version_type
82 assert_equal 'LockedPage', p.versions.first.version_type
82
83
83 assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23"
84 assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23"
84 assert_equal 'Welcome to the weblg', p.title
85 assert_equal 'Welcome to the weblg', p.title
85 assert_equal 'LockedPage', p.versions.first.version_type
86 assert_equal 'LockedPage', p.versions.first.version_type
86 end
87 end
87
88
88 def test_rollback_with_version_class_with_options
89 def test_rollback_with_version_class_with_options
89 p = locked_pages(:welcome)
90 p = locked_pages(:welcome)
90 assert_equal 'Welcome to the weblog', p.title
91 assert_equal 'Welcome to the weblog', p.title
91 assert_equal 'LockedPage', p.versions.first.version_type
92 assert_equal 'LockedPage', p.versions.first.version_type
92
93
93 assert p.revert_to!(p.versions.first), "Couldn't revert to 1"
94 assert p.revert_to!(p.versions.first), "Couldn't revert to 1"
94 assert_equal 'Welcome to the weblg', p.title
95 assert_equal 'Welcome to the weblg', p.title
95 assert_equal 'LockedPage', p.versions.first.version_type
96 assert_equal 'LockedPage', p.versions.first.version_type
96 end
97 end
97
98
98 def test_saves_versioned_copy_with_sti
99 def test_saves_versioned_copy_with_sti
99 p = SpecialLockedPage.create :title => 'first title'
100 p = SpecialLockedPage.create! :title => 'first title'
100 assert !p.new_record?
101 assert !p.new_record?
101 assert_equal 1, p.versions.size
102 assert_equal 1, p.versions.size
102 assert_instance_of LockedPage.versioned_class, p.versions.first
103 assert_instance_of LockedPage.versioned_class, p.versions.first
103 assert_equal 'SpecialLockedPage', p.versions.first.version_type
104 assert_equal 'SpecialLockedPage', p.versions.first.version_type
104 end
105 end
105
106
106 def test_rollback_with_version_number_with_sti
107 def test_rollback_with_version_number_with_sti
107 p = locked_pages(:thinking)
108 p = locked_pages(:thinking)
108 assert_equal 'So I was thinking', p.title
109 assert_equal 'So I was thinking', p.title
109
110
110 assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1"
111 assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1"
111 assert_equal 'So I was thinking!!!', p.title
112 assert_equal 'So I was thinking!!!', p.title
112 assert_equal 'SpecialLockedPage', p.versions.first.version_type
113 assert_equal 'SpecialLockedPage', p.versions.first.version_type
@@ -115,11 +116,11 class VersionedTest < Test::Unit::TestCase
115 def test_lock_version_works_with_versioning
116 def test_lock_version_works_with_versioning
116 p = locked_pages(:thinking)
117 p = locked_pages(:thinking)
117 p2 = LockedPage.find(p.id)
118 p2 = LockedPage.find(p.id)
118
119
119 p.title = 'fresh title'
120 p.title = 'fresh title'
120 p.save
121 p.save
121 assert_equal 2, p.versions.size # limit!
122 assert_equal 2, p.versions.size # limit!
122
123
123 assert_raises(ActiveRecord::StaleObjectError) do
124 assert_raises(ActiveRecord::StaleObjectError) do
124 p2.title = 'stale title'
125 p2.title = 'stale title'
125 p2.save
126 p2.save
@@ -127,15 +128,15 class VersionedTest < Test::Unit::TestCase
127 end
128 end
128
129
129 def test_version_if_condition
130 def test_version_if_condition
130 p = Page.create :title => "title"
131 p = Page.create! :title => "title"
131 assert_equal 1, p.version
132 assert_equal 1, p.version
132
133
133 Page.feeling_good = false
134 Page.feeling_good = false
134 p.save
135 p.save
135 assert_equal 1, p.version
136 assert_equal 1, p.version
136 Page.feeling_good = true
137 Page.feeling_good = true
137 end
138 end
138
139
139 def test_version_if_condition2
140 def test_version_if_condition2
140 # set new if condition
141 # set new if condition
141 Page.class_eval do
142 Page.class_eval do
@@ -143,46 +144,46 class VersionedTest < Test::Unit::TestCase
143 alias_method :old_feeling_good, :feeling_good?
144 alias_method :old_feeling_good, :feeling_good?
144 alias_method :feeling_good?, :new_feeling_good
145 alias_method :feeling_good?, :new_feeling_good
145 end
146 end
146
147
147 p = Page.create :title => "title"
148 p = Page.create! :title => "title"
148 assert_equal 1, p.version # version does not increment
149 assert_equal 1, p.version # version does not increment
149 assert_equal 1, p.versions(true).size
150 assert_equal 1, p.versions(true).size
150
151
151 p.update_attributes(:title => 'new title')
152 p.update_attributes(:title => 'new title')
152 assert_equal 1, p.version # version does not increment
153 assert_equal 1, p.version # version does not increment
153 assert_equal 1, p.versions(true).size
154 assert_equal 1, p.versions(true).size
154
155
155 p.update_attributes(:title => 'a title')
156 p.update_attributes(:title => 'a title')
156 assert_equal 2, p.version
157 assert_equal 2, p.version
157 assert_equal 2, p.versions(true).size
158 assert_equal 2, p.versions(true).size
158
159
159 # reset original if condition
160 # reset original if condition
160 Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
161 Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
161 end
162 end
162
163
163 def test_version_if_condition_with_block
164 def test_version_if_condition_with_block
164 # set new if condition
165 # set new if condition
165 old_condition = Page.version_condition
166 old_condition = Page.version_condition
166 Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }
167 Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }
167
168
168 p = Page.create :title => "title"
169 p = Page.create! :title => "title"
169 assert_equal 1, p.version # version does not increment
170 assert_equal 1, p.version # version does not increment
170 assert_equal 1, p.versions(true).size
171 assert_equal 1, p.versions(true).size
171
172
172 p.update_attributes(:title => 'a title')
173 p.update_attributes(:title => 'a title')
173 assert_equal 1, p.version # version does not increment
174 assert_equal 1, p.version # version does not increment
174 assert_equal 1, p.versions(true).size
175 assert_equal 1, p.versions(true).size
175
176
176 p.update_attributes(:title => 'b title')
177 p.update_attributes(:title => 'b title')
177 assert_equal 2, p.version
178 assert_equal 2, p.version
178 assert_equal 2, p.versions(true).size
179 assert_equal 2, p.versions(true).size
179
180
180 # reset original if condition
181 # reset original if condition
181 Page.version_condition = old_condition
182 Page.version_condition = old_condition
182 end
183 end
183
184
184 def test_version_no_limit
185 def test_version_no_limit
185 p = Page.create :title => "title", :body => 'first body'
186 p = Page.create! :title => "title", :body => 'first body'
186 p.save
187 p.save
187 p.save
188 p.save
188 5.times do |i|
189 5.times do |i|
@@ -191,7 +192,7 class VersionedTest < Test::Unit::TestCase
191 end
192 end
192
193
193 def test_version_max_limit
194 def test_version_max_limit
194 p = LockedPage.create :title => "title"
195 p = LockedPage.create! :title => "title"
195 p.update_attributes(:title => "title1")
196 p.update_attributes(:title => "title1")
196 p.update_attributes(:title => "title2")
197 p.update_attributes(:title => "title2")
197 5.times do |i|
198 5.times do |i|
@@ -199,31 +200,29 class VersionedTest < Test::Unit::TestCase
199 assert p.versions(true).size <= 2, "locked version can only store 2 versions"
200 assert p.versions(true).size <= 2, "locked version can only store 2 versions"
200 end
201 end
201 end
202 end
202
203
203 def test_track_changed_attributes_default_value
204 def test_track_altered_attributes_default_value
204 assert !Page.track_changed_attributes
205 assert !Page.track_altered_attributes
205 assert LockedPage.track_changed_attributes
206 assert LockedPage.track_altered_attributes
206 assert SpecialLockedPage.track_changed_attributes
207 assert SpecialLockedPage.track_altered_attributes
207 end
208 end
208
209
209 def test_version_order
210 def test_version_order
210 assert_equal 23, pages(:welcome).versions.first.version
211 assert_equal 23, pages(:welcome).versions.first.version
211 assert_equal 24, pages(:welcome).versions.last.version
212 assert_equal 24, pages(:welcome).versions.last.version
212 assert_equal 23, pages(:welcome).find_versions.first.version
213 assert_equal 24, pages(:welcome).find_versions.last.version
214 end
213 end
215
214
216 def test_track_changed_attributes
215 def test_track_altered_attributes
217 p = LockedPage.create :title => "title"
216 p = LockedPage.create! :title => "title"
218 assert_equal 1, p.lock_version
217 assert_equal 1, p.lock_version
219 assert_equal 1, p.versions(true).size
218 assert_equal 1, p.versions(true).size
220
219
221 p.title = 'title'
220 p.title = 'title'
222 assert !p.save_version?
221 assert !p.save_version?
223 p.save
222 p.save
224 assert_equal 2, p.lock_version # still increments version because of optimistic locking
223 assert_equal 2, p.lock_version # still increments version because of optimistic locking
225 assert_equal 1, p.versions(true).size
224 assert_equal 1, p.versions(true).size
226
225
227 p.title = 'updated title'
226 p.title = 'updated title'
228 assert p.save_version?
227 assert p.save_version?
229 p.save
228 p.save
@@ -236,27 +235,38 class VersionedTest < Test::Unit::TestCase
236 assert_equal 4, p.lock_version
235 assert_equal 4, p.lock_version
237 assert_equal 2, p.versions(true).size # version 1 deleted
236 assert_equal 2, p.versions(true).size # version 1 deleted
238 end
237 end
239
238
240 def assert_page_title(p, i, version_field = :version)
239 def assert_page_title(p, i, version_field = :version)
241 p.title = "title#{i}"
240 p.title = "title#{i}"
242 p.save
241 p.save
243 assert_equal "title#{i}", p.title
242 assert_equal "title#{i}", p.title
244 assert_equal (i+4), p.send(version_field)
243 assert_equal (i+4), p.send(version_field)
245 end
244 end
246
245
247 def test_find_versions
246 def test_find_versions
248 assert_equal 2, locked_pages(:welcome).versions.size
247 assert_equal 2, locked_pages(:welcome).versions.size
249 assert_equal 1, locked_pages(:welcome).find_versions(:conditions => ['title LIKE ?', '%weblog%']).length
248 assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).length
250 assert_equal 2, locked_pages(:welcome).find_versions(:conditions => ['title LIKE ?', '%web%']).length
249 assert_equal 2, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length
251 assert_equal 0, locked_pages(:thinking).find_versions(:conditions => ['title LIKE ?', '%web%']).length
250 assert_equal 0, locked_pages(:thinking).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length
252 assert_equal 2, locked_pages(:welcome).find_versions.length
251 assert_equal 2, locked_pages(:welcome).versions.length
252 end
253
254 def test_find_version
255 assert_equal page_versions(:welcome_1), Page.find_version(pages(:welcome).id, 23)
256 assert_equal page_versions(:welcome_2), Page.find_version(pages(:welcome).id, 24)
257 assert_equal pages(:welcome), Page.find_version(pages(:welcome).id)
258
259 assert_equal page_versions(:welcome_1), pages(:welcome).find_version(23)
260 assert_equal page_versions(:welcome_2), pages(:welcome).find_version(24)
261 assert_equal pages(:welcome), pages(:welcome).find_version
262
263 assert_raise(ActiveRecord::RecordNotFound) { Page.find_version(pages(:welcome).id, 1) }
264 assert_raise(ActiveRecord::RecordNotFound) { Page.find_version(0, 23) }
253 end
265 end
254
266
255 def test_with_sequence
267 def test_with_sequence
256 assert_equal 'widgets_seq', Widget.versioned_class.sequence_name
268 assert_equal 'widgets_seq', Widget.versioned_class.sequence_name
257 Widget.create :name => 'new widget'
269 3.times { Widget.create! :name => 'new widget' }
258 Widget.create :name => 'new widget'
259 Widget.create :name => 'new widget'
260 assert_equal 3, Widget.count
270 assert_equal 3, Widget.count
261 assert_equal 3, Widget.versioned_class.count
271 assert_equal 3, Widget.versioned_class.count
262 end
272 end
@@ -268,26 +278,26 class VersionedTest < Test::Unit::TestCase
268 def test_has_many_through_with_custom_association
278 def test_has_many_through_with_custom_association
269 assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors
279 assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors
270 end
280 end
271
281
272 def test_referential_integrity
282 def test_referential_integrity
273 pages(:welcome).destroy
283 pages(:welcome).destroy
274 assert_equal 0, Page.count
284 assert_equal 0, Page.count
275 assert_equal 0, Page::Version.count
285 assert_equal 0, Page::Version.count
276 end
286 end
277
287
278 def test_association_options
288 def test_association_options
279 association = Page.reflect_on_association(:versions)
289 association = Page.reflect_on_association(:versions)
280 options = association.options
290 options = association.options
281 assert_equal :delete_all, options[:dependent]
291 assert_equal :delete_all, options[:dependent]
282 assert_equal 'version', options[:order]
292 assert_equal 'version', options[:order]
283
293
284 association = Widget.reflect_on_association(:versions)
294 association = Widget.reflect_on_association(:versions)
285 options = association.options
295 options = association.options
286 assert_nil options[:dependent]
296 assert_equal :nullify, options[:dependent]
287 assert_equal 'version desc', options[:order]
297 assert_equal 'version desc', options[:order]
288 assert_equal 'widget_id', options[:foreign_key]
298 assert_equal 'widget_id', options[:foreign_key]
289
299
290 widget = Widget.create :name => 'new widget'
300 widget = Widget.create! :name => 'new widget'
291 assert_equal 1, Widget.count
301 assert_equal 1, Widget.count
292 assert_equal 1, Widget.versioned_class.count
302 assert_equal 1, Widget.versioned_class.count
293 widget.destroy
303 widget.destroy
@@ -300,14 +310,38 class VersionedTest < Test::Unit::TestCase
300 page_version = page.versions.last
310 page_version = page.versions.last
301 assert_equal page, page_version.page
311 assert_equal page, page_version.page
302 end
312 end
303
313
304 def test_unchanged_attributes
314 def test_unaltered_attributes
305 landmarks(:washington).attributes = landmarks(:washington).attributes
315 landmarks(:washington).attributes = landmarks(:washington).attributes.except("id")
306 assert !landmarks(:washington).changed?
316 assert !landmarks(:washington).changed?
307 end
317 end
308
318
309 def test_unchanged_string_attributes
319 def test_unchanged_string_attributes
310 landmarks(:washington).attributes = landmarks(:washington).attributes.inject({}) { |params, (key, value)| params.update key => value.to_s }
320 landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) }
311 assert !landmarks(:washington).changed?
321 assert !landmarks(:washington).changed?
312 end
322 end
313 end
323
324 def test_should_find_earliest_version
325 assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest
326 end
327
328 def test_should_find_latest_version
329 assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest
330 end
331
332 def test_should_find_previous_version
333 assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous
334 assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2))
335 end
336
337 def test_should_find_next_version
338 assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next
339 assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1))
340 end
341
342 def test_should_find_version_count
343 assert_equal 24, pages(:welcome).versions_count
344 assert_equal 24, page_versions(:welcome_1).versions_count
345 assert_equal 24, page_versions(:welcome_2).versions_count
346 end
347 end No newline at end of file
@@ -1,3 +1,9
1 require 'rfpdf'
1 require 'rfpdf'
2
2
3 ActionView::Base::register_template_handler 'rfpdf', RFPDF::View No newline at end of file
3 begin
4 ActionView::Template::register_template_handler 'rfpdf', RFPDF::View
5 rescue NameError
6 # Rails < 2.1
7 RFPDF::View.backward_compatibility_mode = true
8 ActionView::Base::register_template_handler 'rfpdf', RFPDF::View
9 end
@@ -30,6 +30,8
30 module RFPDF
30 module RFPDF
31
31
32 class View
32 class View
33 @@backward_compatibility_mode = false
34 cattr_accessor :backward_compatibility_mode
33
35
34 def initialize(action_view)
36 def initialize(action_view)
35 @action_view = action_view
37 @action_view = action_view
@@ -45,6 +47,14 module RFPDF
45 :temp_dir => "#{File.expand_path(RAILS_ROOT)}/tmp"
47 :temp_dir => "#{File.expand_path(RAILS_ROOT)}/tmp"
46 }.merge(@action_view.controller.instance_eval{ @options_for_rfpdf } || {}).with_indifferent_access
48 }.merge(@action_view.controller.instance_eval{ @options_for_rfpdf } || {}).with_indifferent_access
47 end
49 end
50
51 def self.compilable?
52 false
53 end
54
55 def compilable?
56 self.class.compilable?
57 end
48
58
49 def render(template, local_assigns = {})
59 def render(template, local_assigns = {})
50 @pdf_name = "Default.pdf" if @pdf_name.nil?
60 @pdf_name = "Default.pdf" if @pdf_name.nil?
@@ -66,7 +76,7 module RFPDF
66 local_assigns.each do |key,val|
76 local_assigns.each do |key,val|
67 class << self; self; end.send(:define_method,key){ val }
77 class << self; self; end.send(:define_method,key){ val }
68 end
78 end
69 ERB.new(template).result(binding)
79 ERB.new(@@backward_compatibility_mode == true ? template : template.source).result(binding)
70 end
80 end
71 end
81 end
72
82
General Comments 0
You need to be logged in to leave comments. Login now