Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2440

Scoped role is not propagated to nested documents when using mass assignment

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.7
    • 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)

      { Building.new }

      context "in default role" do
      it "should not allow assignment to addres" do
      building.assign_attributes( { address:

      { city: "Failure" }

      } )
      building.address.should be_nil
      end
      end

      context "in admin role" do
      it "should allow assignment to address" do
      building.assign_attributes( { address:

      { city: "Success" }

      }, as: :admin )
      building.address.should_not be_nil
      end

      it "should allow assignment to address.city" do
      building.assign_attributes( { address:

      { city: "Success" }

      }, 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!

            Assignee:
            Unassigned Unassigned
            Reporter:
            phallguy phallguy
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: