Module: SemanticGap::DateTimeForm::Spec::ClassMethods

Defined in:
lib/semanticgap_date_time_form/spec.rb

Instance Method Summary collapse

Instance Method Details

#it_has_datetime_fields_for(attr, options = Hash.new) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/semanticgap_date_time_form/spec.rb', line 9

def it_has_datetime_fields_for(attr, options = Hash.new)
  attr_fields = "#{attr}_fields"

  describe "\##{attr_fields}" do
    context 'first call' do
      before(:each) do
        subject.send("#{attr}=", Time.now)
      end

      it "creates a datetime form initialized with the value @#{attr} and the options #{options.inspect}" do
        SemanticGap::DateTimeForm::Form.should_receive(:new).
          with(subject.send(attr), options)

        subject.send(attr_fields)
      end

      it "returns the form" do
        SemanticGap::DateTimeForm::Form.should_receive(:new).and_return(:form)
        subject.send(attr_fields).should == :form
      end
    end

    context 'subsequent calls' do
      before(:each) do
        @form = subject.send(attr_fields)
      end

      it "does not create a new datetime form" do
        SemanticGap::DateTimeForm::Form.should_not_receive(:new)
        subject.send(attr_fields)
      end

      it "returns the form" do
        subject.send(attr_fields).should == @form
      end
    end
  end

  describe "\##{attr_fields}=" do
    it "updates the fields' attributes with the value" do
      @params = { :year => 2010, :month => 5, :day => 6 }
      subject.send(attr_fields).should_receive(:attributes=).with(@params)

      subject.send("#{attr_fields}=", @params)
    end

    context 'with valid fields' do
      it "updates @#{attr} to the fields' datetime" do
        @params = { :year => 2010, :month => 5, :day => 6, :hour => 12, :minute => 0, :ampm => 'PM' }
        
        lambda { subject.send("#{attr_fields}=", @params) }.
          should change(subject, attr).to(DateTime.new(2010, 5, 6, 12, 0))
      end
    end

    context 'with invalid fields' do
      before(:each) do
        subject.send("#{attr}=", Time.now)
      end

      it "updates @#{attr} to nil" do
        lambda { subject.send("#{attr_fields}=", { :month => 13 } ) }.
          should change(subject, attr).to(nil)
      end
    end
  end

  describe '#valid?' do
    context 'when #{attr_fields} are valid' do
      before(:each) do
        subject.send(attr_fields).stub!(:valid?).and_return(true)
      end

      it "does NOT add errors for #{attr}" do
        subject.should_not have(:any).errors_on(attr)
      end
    end

    context 'when #{attr_fields} are invalid' do
      before(:each) do
        subject.send(attr_fields).stub!(:valid?).and_return(false)
      end

      it "adds errors for :published_at" do
        subject.should have_at_least(1).error_on(attr)
      end
    end
  end

  describe '#save!' do
    context "when #{attr} is nil" do
      before(:each) do
        subject.send("#{attr}=", nil)
      end

      it "updates #{attr} with the #{attr_fields} datetime" do
        @time = DateTime.now
        subject.send(attr_fields).should_receive(:to_datetime).and_return(@time)
        subject.send(attr_fields).stub!(:valid?).and_return(true)

        lambda { subject.save! }.should change(subject, attr).to(@time)
      end
    end

    context "when #{attr} is not nil" do
      before(:each) do
        subject.send("#{attr}=", Time.now)
      end

      it "does NOT change #{attr}" do
        lambda { subject.save! }.should_not change(subject, attr)
      end
    end
  end
end