Module: ThoughtBot::Shoulda::Controller::HTML::ClassMethods

Defined in:
lib/shoulda/controller/formats/html.rb

Instance Method Summary collapse

Instance Method Details

#controller_name_from_classObject



12
13
14
# File 'lib/shoulda/controller/formats/html.rb', line 12

def controller_name_from_class
  self.name.gsub(/Test/, '')
end

#make_create_html_tests(res) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/shoulda/controller/formats/html.rb', line 137

def make_create_html_tests(res)
  context "on POST to #{controller_name_from_class}#create with #{res.create.params.inspect}" do
    setup do
      record = get_existing_record(res) rescue nil
      parent_params = make_parent_params(res, record)
      @count = res.klass.count
      post :create, parent_params.merge(res.object => res.create.params)
    end
        
    if res.denied.actions.include?(:create)
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash
      should_not_assign_to res.object

      should "not create new record" do
        assert_equal @count, res.klass.count
      end          
    else
      should_assign_to res.object
      should_set_the_flash_to res.create.flash
      if res.create.redirect.is_a? Symbol
        should_respond_with res.create.redirect
      else
        should_redirect_to res.create.redirect
      end

      should "not have errors on @#{res.object}" do
        assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"            
      end
    end      
  end
end

#make_destroy_html_tests(res) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/shoulda/controller/formats/html.rb', line 105

def make_destroy_html_tests(res)
  context "on DELETE to #{controller_name_from_class}#destroy" do
    setup do
      @record = get_existing_record(res)
      parent_params = make_parent_params(res, @record)
      delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
    end
        
    if res.denied.actions.include?(:destroy)
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash

      should "not destroy record" do
        assert_nothing_raised { assert @record.reload }
      end
    else
      should_set_the_flash_to res.destroy.flash
      if res.destroy.redirect.is_a? Symbol
        should_respond_with res.destroy.redirect
      else
        should_redirect_to res.destroy.redirect
      end

      should "destroy record" do
        assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
          @record.reload
        end
      end
    end
  end
end

#make_edit_html_tests(res) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shoulda/controller/formats/html.rb', line 37

def make_edit_html_tests(res)
  context "on GET to #{controller_name_from_class}#edit" do
    setup do
      @record = get_existing_record(res)
      parent_params = make_parent_params(res, @record)
      get :edit, parent_params.merge({ res.identifier => @record.to_param })          
    end
        
    if res.denied.actions.include?(:edit)
      should_not_assign_to res.object
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash
    else
      should_assign_to res.object                    
      should_respond_with :success
      should_render_template :edit
      should_not_set_the_flash
      should_render_a_form
      should "set @#{res.object} to requested instance" do
        assert_equal @record, assigns(res.object)
      end
    end
  end
end

#make_index_html_tests(res) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shoulda/controller/formats/html.rb', line 62

def make_index_html_tests(res)
  context "on GET to #{controller_name_from_class}#index" do
    setup do
      record = get_existing_record(res) rescue nil
      parent_params = make_parent_params(res, record)
      get(:index, parent_params)          
    end

    if res.denied.actions.include?(:index)
      should_not_assign_to res.object.to_s.pluralize
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash          
    else
      should_respond_with :success
      should_assign_to res.object.to_s.pluralize
      should_render_template :index
      should_not_set_the_flash
    end
  end
end

#make_new_html_tests(res) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/shoulda/controller/formats/html.rb', line 83

def make_new_html_tests(res)
  context "on GET to #{controller_name_from_class}#new" do
    setup do
      record = get_existing_record(res) rescue nil
      parent_params = make_parent_params(res, record)
      get(:new, parent_params)          
    end

    if res.denied.actions.include?(:new)
      should_not_assign_to res.object
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash
    else
      should_respond_with :success
      should_assign_to res.object
      should_not_set_the_flash
      should_render_template :new
      should_render_a_form
    end
  end
end

#make_show_html_tests(res) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/shoulda/controller/formats/html.rb', line 16

def make_show_html_tests(res)
  context "on GET to #{controller_name_from_class}#show" do
    setup do
      record = get_existing_record(res)
      parent_params = make_parent_params(res, record)
      get :show, parent_params.merge({ res.identifier => record.to_param })          
    end

    if res.denied.actions.include?(:show)
      should_not_assign_to res.object
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash
    else
      should_assign_to res.object          
      should_respond_with :success
      should_render_template :show
      should_not_set_the_flash
    end
  end
end

#make_update_html_tests(res) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/shoulda/controller/formats/html.rb', line 170

def make_update_html_tests(res)
  context "on PUT to #{controller_name_from_class}#update with #{res.create.params.inspect}" do
    setup do
      @record = get_existing_record(res)
      parent_params = make_parent_params(res, @record)
      put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
    end

    if res.denied.actions.include?(:update)
      should_not_assign_to res.object
      should_redirect_to res.denied.redirect
      should_set_the_flash_to res.denied.flash
    else
      should_assign_to res.object
      should_set_the_flash_to(res.update.flash)
      if res.update.redirect.is_a? Symbol
        should_respond_with res.update.redirect
      else
        should_redirect_to res.update.redirect
      end
      
      should "not have errors on @#{res.object}" do
        assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
      end
    end
  end
end