Skip to content

Commit

Permalink
Ensure that nested_set queries respect the model's default_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
oesgalha authored and danielmorrison committed May 27, 2016
1 parent ae76086 commit b5be38a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
3.1.0
* Ensure that nested_set queries respect the model's default_scope. [oesgalha](https://github.com/oesgalha)

3.0.5
* Fix .all_roots_valid? method when model is ordered by default

Expand Down
6 changes: 3 additions & 3 deletions lib/awesome_nested_set/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def nested_set_scope(options = {})
end
end

self.class.base_class.unscoped.nested_set_scope options
self.class.base_class.nested_set_scope options
end

# Separate an other `nested_set_scope` for unscoped model
Expand All @@ -154,7 +154,7 @@ def nested_set_scope(options = {})
# And class level `nested_set_scope` seems just for query `root` `child` .. etc
# I think we don't have to provide unscoped `nested_set_scope` in class level.
def nested_set_scope_without_default_scope(*args)
self.class.unscoped do
self.class.base_class.unscoped do
nested_set_scope(*args)
end
end
Expand Down Expand Up @@ -182,7 +182,7 @@ def has_depth_column?
end

def right_most_node
@right_most_node ||= nested_set_scope(
@right_most_node ||= nested_set_scope_without_default_scope(
:order => "#{quoted_right_column_full_name} desc"
).first
end
Expand Down
16 changes: 16 additions & 0 deletions spec/awesome_nested_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,7 @@ def check_structure(entries, structure)
it "should have correct #lft & #rgt" do
parent = DefaultScopedModel.find(6)

DefaultScopedModel.send(:default_scopes=, [])
DefaultScopedModel.send(:default_scope, Proc.new { parent.reload.self_and_descendants })

children = parent.children.create(name: 'Helloworld')
Expand All @@ -1312,6 +1313,21 @@ class DefaultScopedModel
end
expect(DefaultScopedModel.all_roots_valid?).to be_truthy
end

it "should respect the default_scope" do
DefaultScopedModel.send(:default_scopes=, [])
DefaultScopedModel.send(:default_scope, -> { DefaultScopedModel.where(draft: false) })

no_parents = DefaultScopedModel.find(1)

expect(no_parents.self_and_ancestors.count).to eq(1)

no_parents.draft = true
no_parents.save

other_root = DefaultScopedModel.create!(name: 'Another root')
expect(other_root.self_and_ancestors.count).to eq(1)
end
end
end
end
1 change: 1 addition & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
t.column :lft, :integer
t.column :rgt, :integer
t.column :depth, :integer
t.column :draft, :boolean, default: false
end

create_table :categories, :force => true do |t|
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/default_scoped_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@ top_level:
name: Top Level
lft: 1
rgt: 4
draft: false
child_1:
id: 2
name: Child 1
parent_id: 1
lft: 2
rgt: 3
draft: false
top_level_2:
id: 6
name: Top Level 2
lft: 5
rgt: 6
draft: false
top_level_3:
id: 7
name: Top Level 3
lft: 7
rgt: 8
draft: false
old_root:
id: 8
name: Top Level
lft: 1
rgt: 4
draft: true

0 comments on commit b5be38a

Please sign in to comment.