Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with migration on empty database #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions acts_as_nested_interval.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["[email protected]"]
s.homepage = "https://github.com/clyfe/acts_as_nested_interval"
s.authors = ["Nicolae Claudius", "Pythonic", "Grzegorz Łuszczek"]
s.email = ["[email protected]"]
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."

Expand Down
28 changes: 15 additions & 13 deletions lib/acts_as_nested_interval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions lib/acts_as_nested_interval/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -176,14 +178,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
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_nested_interval/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActsAsNestedInterval
VERSION = "0.1.0"
VERSION = "0.1.1"
end