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

Fixes for Rails 4 #26

Open
wants to merge 3 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: 5 additions & 1 deletion lib/mongoid_nested_set/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def acts_as_nested_set(options = {})

attr_accessor :skip_before_destroy

if accessible_attributes.blank?
# Rails 4 no longer uses "attr_protected" and "attr_accessible" for
# mass assignment control by default, but this methods can be restored
# by installing 'protected_attributes' gem. So here we just check
# accessible_attributes method presence
if respond_to?(:accessible_attributes) && accessible_attributes.blank?
attr_protected left_field_name.intern, right_field_name.intern
end

Expand Down
32 changes: 26 additions & 6 deletions lib/mongoid_nested_set/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def move_to(target, position)
).update_all("$inc" => { right_field_name => -width })
end

self.set(parent_field_name, new_parent)
self.mongoid_set(parent_field_name, new_parent)
self.reload_nested_set
self.update_self_and_descendants_depth

Expand Down Expand Up @@ -188,7 +188,7 @@ def update_depth
def update_self_and_descendants_depth
if depth?
scope_class.each_with_level(self_and_descendants) do |node, level|
node.with(:safe => true).set(:depth, level) unless node.depth == level
node.with(:safe => true).mongoid_set(:depth, level) unless node.depth == level
end
self.reload
end
Expand All @@ -214,17 +214,37 @@ def destroy_descendants
# update lefts and rights for remaining nodes
diff = right - left + 1

scope_class.with(:safe => true).where(
c = scope_class.with(:safe => true).where(
nested_set_scope.where(left_field_name.to_sym.gt => right).selector
).inc(left_field_name, -diff)
)
mongoid_inc(c, left_field_name, -diff)

scope_class.with(:safe => true).where(
c = scope_class.with(:safe => true).where(
nested_set_scope.where(right_field_name.to_sym.gt => right).selector
).inc(right_field_name, -diff)
)
mongoid_inc(c, right_field_name, -diff)

# Don't allow multiple calls to destroy to corrupt the set
self.skip_before_destroy = true
end

# Compatibility wrapper for mongoid set method, should works with 3.x.x and
# 4.x.x versions
def mongoid_set(field, value)
if method(:set).arity == 1
set({field => value})
else
set(field, value)
end
end

def mongoid_inc(criteria, field, value)
if criteria.method(:inc).arity == 2
criteria.inc(field, value)
else
criteria.inc({field => value})
end
end

end
end