Sha256: 079857f420b0f9568a2713ccf639b9bfc619366a051a7be6a06a35f4ce62fada

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# encoding: utf-8
require "spec_helper"

class Donut < ActiveRecord::Base
  attr_accessor :class_name

  defaults :flavor => "cream",
           :name => "Cream",
           :maker => proc { "Dunkin Donuts" },
           :class_name => lambda {|donut| donut.class.name }

  defaults :quantity => 5
end

describe Defaults do
  before do
    Donut.delete_all
    @donut = create_donut
    @new_donut = Donut.new
  end

  it "raises when invalid argument is provided" do
    expect {
      Donut.defaults nil
    }.to raise_error(ArgumentError)
  end

  it "sets default values" do
    @new_donut.flavor.should == "cream"
    @new_donut.name.should == "Cream"
    @new_donut.maker.should == "Dunkin Donuts"
    @new_donut.class_name.should == "Donut"
    @new_donut.quantity.should == 5
  end

  it "sets default values only when have blank attributes" do
    @donut.flavor.should == "vanilla"
    @donut.quantity.should == 5
  end

  it "returns default value for an attribute" do
    @donut.default_for(:flavor).should == "cream"
    @donut.default_for(:maker).should == "Dunkin Donuts"
    @donut.default_for(:class_name).should == "Donut"
    @donut.default_for(:quantity).should == 5
  end

  it "does not set default values" do
    Donut.first.flavor.should == "vanilla"
  end

  private
  def create_donut(options={})
    Donut.create({
      :flavor => "vanilla",
      :name => "Vanilla Sky",
      :maker => "Mr. Baker"
    }.merge(options))
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
defaults-0.1.1 spec/defaults_spec.rb