From b58632468a641329e699e001e975d54d12c5af6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=81uszczek?= Date: Fri, 9 Aug 2013 00:10:51 +0200 Subject: [PATCH 1/4] Fix problem with migration on empty database --- lib/acts_as_nested_interval.rb | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/acts_as_nested_interval.rb b/lib/acts_as_nested_interval.rb index 13dfb10..ee31e7e 100644 --- a/lib/acts_as_nested_interval.rb +++ b/lib/acts_as_nested_interval.rb @@ -39,20 +39,22 @@ def acts_as_nested_interval(options = {}) dependent: nested_interval_dependent scope :roots, -> { where(nested_interval_foreign_key => nil) } - if columns_hash["rgt"] - scope :preorder, -> { order('rgt DESC, lftp ASC') } - elsif columns_hash["rgtp"] && columns_hash["rgtq"] - scope :preorder, -> { order('1.0 * rgtp / rgtq DESC, lftp ASC') } - else - scope :preorder, -> { order('nested_interval_rgt(lftp, lftq) DESC, lftp ASC') } - end + if self.table_exists? # Fix problem with migrating without table + if columns_hash["rgt"] + scope :preorder, -> { order('rgt DESC, lftp ASC') } + elsif columns_hash["rgtp"] && columns_hash["rgtq"] + scope :preorder, -> { order('1.0 * rgtp / rgtq DESC, lftp ASC') } + else + scope :preorder, -> { order('nested_interval_rgt(lftp, lftq) DESC, lftp ASC') } + end - before_create :create_nested_interval - before_destroy :destroy_nested_interval - before_update :update_nested_interval - - include ActsAsNestedInterval::InstanceMethods - extend ActsAsNestedInterval::ClassMethods + before_create :create_nested_interval + before_destroy :destroy_nested_interval + before_update :update_nested_interval + + include ActsAsNestedInterval::InstanceMethods + extend ActsAsNestedInterval::ClassMethods + end end end From c23f2d54aa17c39a33d7484f85d5dbe77b4de26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=81uszczek?= Date: Sat, 12 Apr 2014 16:36:43 +0200 Subject: [PATCH 2/4] Fix depth method to works with new record without calculated attributes --- .../instance_methods.rb | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/acts_as_nested_interval/instance_methods.rb b/lib/acts_as_nested_interval/instance_methods.rb index 69d3383..d3ae799 100644 --- a/lib/acts_as_nested_interval/instance_methods.rb +++ b/lib/acts_as_nested_interval/instance_methods.rb @@ -176,14 +176,22 @@ def ancestors # Returns depth by counting ancestors up to 0 / 1. def depth - n = 0 - p, q = lftp, lftq - while p != 0 - x = p.inverse(q) - p, q = (x * p - 1) / q, x - n += 1 + if new_record? + if parent_id.nil? + return 0 + else + return parent.depth + 1 + end + else + n = 0 + p, q = lftp, lftq + while p != 0 + x = p.inverse(q) + p, q = (x * p - 1) / q, x + n += 1 + end + return n end - n end def lft; 1.0 * lftp / lftq end From 581a19c43696c4fd0b1c7d49bd9404af85721f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=81uszczek?= Date: Sat, 12 Apr 2014 16:46:55 +0200 Subject: [PATCH 3/4] Fir problem with attr_readonly for parent foreign key --- lib/acts_as_nested_interval/instance_methods.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/acts_as_nested_interval/instance_methods.rb b/lib/acts_as_nested_interval/instance_methods.rb index d3ae799..d485f38 100644 --- a/lib/acts_as_nested_interval/instance_methods.rb +++ b/lib/acts_as_nested_interval/instance_methods.rb @@ -111,7 +111,9 @@ def update_nested_interval end end + # Rewrite method def update_nested_interval_move + return if self.class.readonly_attributes.include?(nested_interval_foreign_key.to_sym) # Fix issue #9 begin db_self = self.class.find(id) db_parent = self.class.find(read_attribute(nested_interval_foreign_key)) From 25701bd186b78c89afdcb8dbd79cc1e63c7f263a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=81uszczek?= Date: Tue, 15 Apr 2014 21:15:52 +0200 Subject: [PATCH 4/4] Update gemspec --- acts_as_nested_interval.gemspec | 6 +++--- lib/acts_as_nested_interval/version.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/acts_as_nested_interval.gemspec b/acts_as_nested_interval.gemspec index 5368ec1..6b86a07 100644 --- a/acts_as_nested_interval.gemspec +++ b/acts_as_nested_interval.gemspec @@ -7,9 +7,9 @@ require "acts_as_nested_interval/version" Gem::Specification.new do |s| s.name = "acts_as_nested_interval" s.version = ActsAsNestedInterval::VERSION - s.authors = ["Nicolae Claudius", "Pythonic"] - s.email = ["nicolae_claudius@yahoo.com"] - s.homepage = "https://github.com/clyfe/acts_as_nested_interval" + s.authors = ["Nicolae Claudius", "Pythonic", "Grzegorz Ɓuszczek"] + s.email = ["grzegorz@piklus.pl"] + s.homepage = "https://github.com/grzlus/acts_as_nested_interval" s.summary = "Encode Trees in RDBMS using nested interval method." s.description = "Encode Trees in RDBMS using nested interval method for powerful querying and speedy inserts." diff --git a/lib/acts_as_nested_interval/version.rb b/lib/acts_as_nested_interval/version.rb index aafd16b..f825fc1 100644 --- a/lib/acts_as_nested_interval/version.rb +++ b/lib/acts_as_nested_interval/version.rb @@ -1,3 +1,3 @@ module ActsAsNestedInterval - VERSION = "0.1.0" + VERSION = "0.1.1" end