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