Sha256: e907bf77b69a73183e369005e83c8ac96522d60078ab4bfe8acc85ade73a8f08

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class Book < ActiveRecord::Base
  attr_protected :edition

  default_values price: 0, edition: 1, released_at: lambda { Time.now }
end

class Book2 < ActiveRecord::Base
  self.table_name = :books

  default_values price: 0 do
    t = Time.now
    {:released_at => t }
  end
end

describe "ArDefaultValues" do
  subject { @sample }
  before do
    @now = Time.at(Time.now.to_i)
    Time.stub(:now).and_return(@now)
  end
  context :without_attributes do
    before do
      @sample = Book.new
    end
    its(:title) { should be_nil }
    its(:price) { should == 0 }
    its(:edition) { should == 1 }
    its(:released_at) { should == @now }
  end
  context :with_attributes do
    before do
      @sample = Book.new(:price => 100)
    end
    its(:title) { should be_nil }
    its(:price) { should == 100 }
    its(:edition) { should == 1 }
    its(:released_at) { should == @now }
  end
  context :with_block do
    before do
      @sample = Book.new do |t|
        t.title = 'foo'
      end
    end
    its(:title) { should == 'foo' }
    its(:price) { should == 0 }
    its(:edition) { should == 1 }
    its(:released_at) { should == @now }
  end

  context :consider_attr_protected do
    before do
      @sample = Book2.new(:edition => 3)
    end
    its(:title) { should be_nil }
    its(:price) { should == 0 }
    its(:edition) { should == 3 }
    its(:released_at) { should == @now }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar_default_values-0.2.0 spec/ar_default_values_spec.rb