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

Model Attribute Default Proc

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.3.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      The default proc for a model's field should be instance_exec'd on the model object (after all the mass-assignments are mass-assigned).

      class Pet
      include Mongoid::Document

      field :ferocious,
      :type => Boolean,
      :default => false

      field :name,
      :type => String,
      :default => proc

      { ferocious? ? "Jaws" : "Sparky" }

      attr_accessible :ferocious, :name
      end

      describe Pet do
      context "tame" do
      subject

      { Pet.new }

      it

      { should_not be_ferocious }

      its(:name)

      { should == "Sparky" }

      end
      context "ferocious" do
      subject

      { Pet.new(:ferocious => true) }

      it

      { should be_ferocious }

      its(:name)

      { should == "Jaws" }

      end
      end

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

              Created:
              Updated:
              Resolved: