Skip to content

Commit

Permalink
Fixed it for Rails 3.2 and broke it for 2.x, changed version 1.2.6 an…
Browse files Browse the repository at this point in the history
…d activerecord dependency to >= 3.0.0
  • Loading branch information
Stefan Kroes committed Mar 27, 2012
1 parent f99359f commit b5fd912
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'http://rubygems.org'

gemspec

group :test do
gem 'sqlite3'
end
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
PATH
remote: .
specs:
ancestry (1.2.5)
activerecord (>= 2.2.2)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.2)
activesupport (= 3.2.2)
builder (~> 3.0.0)
activerecord (3.2.2)
activemodel (= 3.2.2)
activesupport (= 3.2.2)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.2)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
i18n (0.6.0)
multi_json (1.2.0)
sqlite3 (1.3.5)
tzinfo (0.3.32)

PLATFORMS
ruby

DEPENDENCIES
ancestry!
sqlite3
12 changes: 1 addition & 11 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rcov/rcovtask'
require 'rdoc/task'

desc 'Default: run unit tests.'
task :default => :test
Expand All @@ -13,15 +12,6 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end

desc 'Determine test code coverage for the ancestry plugin.'
Rcov::RcovTask.new(:coverage) do |t|
t.libs << 'lib'
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
t.output_dir = "test/coverage"
t.rcov_opts << "-x /gems/,/library/"
end

desc 'Generate documentation for the ancestry plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'doc'
Expand Down
4 changes: 2 additions & 2 deletions ancestry.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Gem::Specification.new do |s|
s.description = 'Organise ActiveRecord model into a tree structure'
s.summary = 'Ancestry allows the records of a ActiveRecord model to be organised in a tree structure, using a single, intuitively formatted database column. It exposes all the standard tree structure relations (ancestors, parent, root, children, siblings, descendants) and all of them can be fetched in a single sql query. Additional features are named_scopes, integrity checking, integrity restoration, arrangement of (sub)tree into hashes and different strategies for dealing with orphaned records.'

s.version = '1.2.5'
s.version = '1.2.6'

s.author = 'Stefan Kroes'
s.email = 's.a.kroes@gmail.com'
Expand All @@ -22,5 +22,5 @@ Gem::Specification.new do |s|
'README.rdoc'
]

s.add_dependency 'activerecord', '>= 2.2.2'
s.add_dependency 'activerecord', '>= 3.0.0'
end
4 changes: 2 additions & 2 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def has_ancestry options = {}
send scope_method, :descendants_of, lambda { |object| {:conditions => to_node(object).descendant_conditions} }
send scope_method, :subtree_of, lambda { |object| {:conditions => to_node(object).subtree_conditions} }
send scope_method, :siblings_of, lambda { |object| {:conditions => to_node(object).sibling_conditions} }
send scope_method, :ordered_by_ancestry, :order => "(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}"
send scope_method, :ordered_by_ancestry_and, lambda { |order| {:order => "(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}, #{order}"} }
send scope_method, :ordered_by_ancestry, reorder("(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}")
send scope_method, :ordered_by_ancestry_and, lambda { |order| reorder("(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}, #{order}") }

# Update descendants with new ancestry before save
before_save :update_descendants_with_new_ancestry
Expand Down
26 changes: 17 additions & 9 deletions test/environment.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
require 'rubygems'
Gem.activate 'activerecord', ENV['ar'] || '3.0.0'

if ENV['ar'].nil?
gem 'activerecord'
else
gem 'activerecord', ENV['ar']
end

require 'active_record'
require 'active_support/test_case'
require 'test/unit'
require 'ancestry'

class AncestryTestDatabase
def self.setup
ActiveRecord::Base.logger
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('log/test.log')
ActiveRecord::Base.establish_connection YAML.load(File.open(File.join(File.dirname(__FILE__), 'database.yml')).read)[ENV['db'] || 'sqlite3']
end

def self.with_model options = {}
depth = options.delete(:depth) || 0
width = options.delete(:width) || 0
extra_columns = options.delete(:extra_columns)
depth = options.delete(:depth) || 0
width = options.delete(:width) || 0
extra_columns = options.delete(:extra_columns)
primary_key_type = options.delete(:primary_key_type) || :default

ActiveRecord::Base.connection.create_table 'test_nodes', :id => (primary_key_type == :default) do |table|
Expand All @@ -32,9 +38,10 @@ def self.with_model options = {}
const_set 'TestNode', model

if primary_key_type == :string
model.before_create { self.id = ActiveSupport::SecureRandom.hex(10) }
model.before_create { self.id = SecureRandom.hex(10) }
end
model.send :set_table_name, 'test_nodes'
model.table_name = 'test_nodes'
model.primary_key = :id if primary_key_type == :string
model.has_ancestry options unless options.delete(:skip_ancestry)

if depth > 0
Expand All @@ -43,8 +50,9 @@ def self.with_model options = {}
yield model
end
ensure
model.reset_column_information
ActiveRecord::Base.connection.drop_table 'test_nodes'
remove_const "TestNode"
remove_const 'TestNode'
end
end

Expand All @@ -62,6 +70,6 @@ def self.create_test_nodes model, depth, width, parent = nil

puts "\nRunning Ancestry test suite:"
puts " Ruby: #{RUBY_VERSION}"
puts " ActiveRecord: #{ENV['ar'] || '3.0.0'}"
puts " ActiveRecord: #{ActiveRecord::VERSION::STRING}"
puts " Database: #{ActiveRecord::Base.connection.adapter_name}\n\n"

10 changes: 10 additions & 0 deletions test/has_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,14 @@ def test_sort_by_ancestry
assert_equal [n1, n2, n4, n3, n5].map(&:id), arranged.map(&:id)
end
end

def test_arrangement_nesting
AncestryTestDatabase.with_model :extra_columns => {:name => :string} do |model|
model.send :default_scope, model.order('name')

model.create!(:name => 'Linux').children.create! :name => 'Debian'

assert_equal 1, model.arrange.count
end
end
end

0 comments on commit b5fd912

Please sign in to comment.