Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: collectiveidea/awesome_nested_set
base: master
...
head fork: PeopleAdmin/awesome_nested_set
compare: deploy
Checking mergeability… Don’t worry, you can still create the pull request.
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 3 contributors
Commits on Aug 16, 2012
@joshuaflanagan joshuaflanagan Update README with PA fork branch strategy a862c25
Commits on Aug 22, 2012
@joshuaflanagan joshuaflanagan Set a scope that defines the subset of records in the nested set
Some records in a table should never participate in the
nested set, and should not be considered one of the roots.

First, define a scope on the class to identify records
that should be included in the nested set.
Second, pass the name of the scope as the :subset on
the acts_as_nested_set class method call.
8bb109b
@joshuaflanagan joshuaflanagan Merge branch 'subset' into deploy 52ec222
Commits on Apr 15, 2015
@parkeryoung parkeryoung Merging our version with tag v2.1.6 9a7bd62
Commits on Apr 17, 2015
@kgolic kgolic Merge pull request #2 from PeopleAdmin/upgrade
Upgrade to v2.1.6
2c803a2
View
16 README.rdoc
@@ -1,3 +1,19 @@
+= PeopleAdmin fork
+
+PeopleAdmin maintains a forked copy of AwesomeNestedSet.
+
+The *master* branch will remain unchanged, so that it can be kept in synch with
+collectiveidea/awesome_nested_set.
+
+Added features are maintained in separate branches, so that they can be submitted
+as pull requests independently. For example, the *subset* branch contains all
+changes related to our proposed :subset option.
+
+Our Gemfile will refer to this *deploy* branch. All changes from *master* and
+our feature branches should be merged into *deploy*. No changes should be made
+directly to the *deploy* branch (other than the merge commits, and this
+Readme.rdoc).
+
= AwesomeNestedSet
Awesome Nested Set is an implementation of the nested set pattern for ActiveRecord models. It is replacement for acts_as_nested_set and BetterNestedSet, but more awesome.
View
24 lib/awesome_nested_set/awesome_nested_set.rb
@@ -28,6 +28,10 @@ module NestedSet #:nodoc:
# (if it hasn't been already) and use that as the foreign key restriction. You
# can also pass an array to scope by multiple attributes.
# Example: <tt>acts_as_nested_set :scope => [:notable_id, :notable_type]</tt>
+ # * +:subset+ - restricts the records for which the nested set will be
+ # maintained. Specified as a symbol that refers to an ActiveRecord scope
+ # defined in the class.
+ # Example: <tt>acts_as_nested_set :subset => :active</tt>
# * +:dependent+ - behavior for cascading destroy. If set to :destroy, all the
# child objects are destroyed alongside this object by calling their destroy
# method. If set to :delete_all (default), all the child objects are deleted
@@ -116,7 +120,7 @@ def root
end
def roots
- where(parent_column_name => nil).order(quoted_left_column_full_name)
+ nested_set_subset.where(parent_column_name => nil).order(quoted_left_column_name)
end
def leaves
@@ -129,6 +133,7 @@ def valid?
def left_and_rights_valid?
## AS clause not supported in Oracle in FROM clause for aliasing table name
+ nested_set_subset.
joins("LEFT OUTER JOIN #{quoted_table_name}" +
(connection.adapter_name.match(/Oracle/).nil? ? " AS " : " ") +
"parent ON " +
@@ -150,7 +155,8 @@ def no_duplicates_for_columns?
end.push(nil).join(", ")
[quoted_left_column_full_name, quoted_right_column_full_name].all? do |column|
# No duplicates
- select("#{scope_string}#{column}, COUNT(#{column})").
+ nested_set_subset.
+ select("#{scope_string}#{column}, COUNT(#{column})").
group("#{scope_string}#{column}").
having("COUNT(#{column}) > 1").
first.nil?
@@ -178,6 +184,14 @@ def each_root_valid?(roots_to_validate)
end
end
+ def nested_set_subset
+ subset = where(nil)
+ if acts_as_nested_set_options[:subset]
+ subset = send(acts_as_nested_set_options[:subset])
+ end
+ subset
+ end
+
# Rebuilds the left & rights if unset or invalid.
# Also very useful for converting from acts_as_tree.
def rebuild!(validate_nodes = true)
@@ -200,14 +214,14 @@ def rebuild!(validate_nodes = true)
# set left
node[left_column_name] = indices[scope.call(node)] += 1
# find
- where(["#{quoted_parent_column_full_name} = ? #{scope.call(node)}", node]).order("#{quoted_left_column_full_name}, #{quoted_right_column_full_name}, id").each{|n| set_left_and_rights.call(n) }
+ nested_set_subset.where(["#{quoted_parent_column_name} = ? #{scope.call(node)}", node]).order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").each{|n| set_left_and_rights.call(n) }
# set right
node[right_column_name] = indices[scope.call(node)] += 1
node.save!(:validate => validate_nodes)
end
# Find root node(s)
- root_nodes = where("#{quoted_parent_column_full_name} IS NULL").order("#{quoted_left_column_full_name}, #{quoted_right_column_full_name}, id").each do |root_node|
+ root_nodes = nested_set_subset.where("#{quoted_parent_column_name} IS NULL").order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").each do |root_node|
# setup index for this scope
indices[scope.call(root_node)] ||= 0
set_left_and_rights.call(root_node)
@@ -511,7 +525,7 @@ def nested_set_scope(options = {})
options[:conditions] = scopes.inject({}) do |conditions,attr|
conditions.merge attr => self[attr]
end unless scopes.empty?
- self.class.base_class.unscoped.scoped options
+ self.class.base_class.unscoped.nested_set_subset.scoped options
end
def store_new_parent
View
22 spec/awesome_nested_set_spec.rb
@@ -5,6 +5,13 @@
self.class.fixtures :categories, :departments, :notes, :things, :brokens
end
+ describe "active scope" do
+ it "should recognize active scope" do
+ Category.count.should == 8
+ Category.active.count.should == 6
+ end
+ end
+
describe "defaults" do
it "should have left_column_default" do
Default.acts_as_nested_set_options[:left_column].should == 'lft'
@@ -103,8 +110,8 @@
ScopedCategory.acts_as_nested_set_options[:scope].should == :organization_id
end
- it "roots_class_method" do
- Category.roots.should == Category.find_all_by_parent_id(nil)
+ it "roots_class_method respects subset option" do
+ Category.roots.should == Category.active.find_all_by_parent_id(nil)
end
it "root_class_method" do
@@ -121,7 +128,7 @@
end
it "root when not persisted and parent_column_name value is set" do
- last_category = Category.last
+ last_category = Category.active.last
Category.new(Default.parent_column_name => last_category.id).root.should == last_category.root
end
@@ -654,6 +661,15 @@
before_text.should == Category.root.to_text
end
+ it "rebuild ignores records excluded by the subset option" do
+ Category.rebuild!
+ Category.valid?.should be_true
+ categories(:ignored_top_1).lft.should be_nil
+ categories(:ignored_top_1).rgt.should be_nil
+ categories(:ignored_top_2).lft.should be_nil
+ categories(:ignored_top_2).rgt.should be_nil
+ end
+
it "move_possible_for_sibling" do
categories(:child_2).move_possible?(categories(:child_1)).should be_true
end
View
1  spec/db/schema.rb
@@ -7,6 +7,7 @@
t.column :rgt, :integer
t.column :depth, :integer
t.column :organization_id, :integer
+ t.column :inactive, :boolean
end
create_table :departments, :force => true do |t|
View
8 spec/fixtures/categories.yml
@@ -32,3 +32,11 @@ top_level_2:
name: Top Level 2
lft: 11
rgt: 12
+ignored_top_1:
+ id: 7
+ name: Ignored Top 1
+ inactive: true
+ignored_top_2:
+ id: 8
+ name: Ignored Top 2
+ inactive: true
View
6 spec/support/models.rb
@@ -9,7 +9,8 @@ class Default < ActiveRecord::Base
class ScopedCategory < ActiveRecord::Base
self.table_name = 'categories'
- acts_as_nested_set :scope => :organization
+ acts_as_nested_set :scope => :organization, :subset => :active
+ scope :active, where(:inactive => nil)
end
class OrderedCategory < ActiveRecord::Base
@@ -25,9 +26,10 @@ class RenamedColumns < ActiveRecord::Base
end
class Category < ActiveRecord::Base
- acts_as_nested_set
+ acts_as_nested_set :subset => :active
validates_presence_of :name
+ scope :active, where(:inactive => nil)
# Setup a callback that we can switch to true or false per-test
set_callback :move, :before, :custom_before_move

No commit comments for this range

Something went wrong with that request. Please try again.