-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
When using mass assignment with a scope, Mongoid correctly assigns the protected attributes based on the role provided. However nested documents always revert to the :default scope.
Example spec that demonstrates the failure
class Building
include Mongoid::Document
attr_accessible
attr_accessible :address, as: :admin
embeds_one :address
end
class Address
include Mongoid::Document
attr_accessible
attr_accessible :city, as: :admin
embedded_in :building
field :city, type: String
end
describe "Nested mass assignment" do
let(:building)
context "in default role" do
it "should not allow assignment to addres" do
building.assign_attributes( { address:
} )
building.address.should be_nil
end
end
context "in admin role" do
it "should allow assignment to address" do
building.assign_attributes( { address:
}, as: :admin )
building.address.should_not be_nil
end
it "should allow assignment to address.city" do
building.assign_attributes( { address:
}, as: :admin )
building.address.city.should == "Success"
end
end
end
It's possible I'm misunderstanding some design goals so I would propose one of two fixes:
1. If it should be passing the role, then updated the nested builders to take a scope and use that when building the nested documents.
2. Reject it as a feature, but document that roles are not used when building nested documents during mass assignent.
Thanks for all your hard work!