Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3352: hardened_scaffold.2.diff

File hardened_scaffold.2.diff, 8.5 kB (added by nils@alumni.rice.edu, 3 years ago)

Updated patch

  • railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb

    old new  
    3838    assert_not_nil assigns(:<%= plural_name %>) 
    3939  end 
    4040 
    41   def test_show<%= suffix %> 
     41  def test_show<%= suffix %>_with_id 
    4242    get :show<%= suffix %>, :id => 1 
    4343 
    4444    assert_response :success 
    45     assert_template 'show
     45    assert_template 'show<%= suffix %>
    4646 
    4747    assert_not_nil assigns(:<%= singular_name %>) 
    4848    assert assigns(:<%= singular_name %>).valid? 
    4949  end 
    5050 
     51  def test_show<%= suffix %>_without_id 
     52    get :show<%= suffix %> 
     53 
     54    assert_response :redirect 
     55    assert_redirected_to :action => 'list<%= suffix %>' 
     56  end 
     57 
    5158  def test_new<%= suffix %> 
    5259    get :new<%= suffix %> 
    5360 
     
    5764    assert_not_nil assigns(:<%= singular_name %>) 
    5865  end 
    5966 
    60   def test_create 
     67  def test_create<%= suffix %>_using_post 
    6168    num_<%= plural_name %> = <%= model_name %>.count 
    6269 
    6370    post :create<%= suffix %>, :<%= singular_name %> => {} 
     
    6875    assert_equal num_<%= plural_name %> + 1, <%= model_name %>.count 
    6976  end 
    7077 
    71   def test_edit<%= suffix %> 
     78  def test_create<%= suffix %>_using_get 
     79    num_<%= plural_name %> = <%= model_name %>.count 
     80 
     81    get :create<%= suffix %>, :<%= singular_name %> => {} 
     82 
     83    assert_response :success 
     84    assert_template 'new<%= suffix %>' 
     85 
     86    assert_equal num_<%= plural_name %>, <%= model_name %>.count 
     87  end 
     88 
     89  def test_edit<%= suffix %>_with_id 
    7290    get :edit<%= suffix %>, :id => 1 
    7391 
    7492    assert_response :success 
     
    7896    assert assigns(:<%= singular_name %>).valid? 
    7997  end 
    8098 
    81   def test_update<%= suffix %> 
     99  def test_edit<%= suffix %>_without_id 
     100    get :edit<%= suffix %> 
     101 
     102    assert_response :redirect 
     103    assert_redirected_to :action => 'list<%= suffix %>' 
     104  end 
     105 
     106  def test_update<%= suffix %>_with_id_using_post 
    82107    post :update<%= suffix %>, :id => 1 
    83108    assert_response :redirect 
    84109    assert_redirected_to :action => 'show<%= suffix %>', :id => 1 
    85110  end 
    86111 
    87   def test_destroy<%= suffix %> 
     112  def test_update<%= suffix %>_with_id_using_get 
     113    get :update<%= suffix %>, :id => 1 
     114    assert_response :success 
     115    assert_template 'edit<%= suffix %>' 
     116  end 
     117 
     118  def test_update<%= suffix %>_without_id_using_post 
     119    post :update<%= suffix %> 
     120    assert_response :redirect 
     121    assert_redirected_to :action => 'list<%= suffix %>' 
     122  end 
     123 
     124  def test_update<%= suffix %>_without_id_using_get 
     125    get :update<%= suffix %> 
     126    assert_response :redirect 
     127    assert_redirected_to :action => 'list<%= suffix %>' 
     128  end 
     129 
     130  def test_destroy<%= suffix %>_with_id_using_post 
    88131    assert_not_nil <%= model_name %>.find(1) 
    89132 
    90133    post :destroy, :id => 1 
     
    95138      <%= model_name %>.find(1) 
    96139    } 
    97140  end 
     141 
     142  def test_destroy<%= suffix %>_with_id_using_get 
     143    assert_not_nil <%= model_name %>.find(1) 
     144 
     145    get :destroy<%= suffix %>, :id => 1 
     146    assert_response :redirect 
     147    assert_redirected_to :action => 'edit<%= suffix %>' 
     148 
     149    assert_not_nil <%= model_name %>.find(1) 
     150  end 
     151 
     152  def test_destroy<%= suffix %>_without_id_using_post 
     153    assert_not_nil <%= model_name %>.find(1) 
     154 
     155    post :destroy<%= suffix %> 
     156    assert_response :redirect 
     157    assert_redirected_to :action => 'list<%= suffix %>' 
     158 
     159    assert_not_nil <%= model_name %>.find(1) 
     160  end 
     161 
     162  def test_destroy<%= suffix %>_without_id_using_get 
     163    assert_not_nil <%= model_name %>.find(1) 
     164 
     165    get :destroy<%= suffix %> 
     166    assert_response :redirect 
     167    assert_redirected_to :action => 'list<%= suffix %>' 
     168 
     169    assert_not_nil <%= model_name %>.find(1) 
     170  end 
    98171end 
  • railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml

    old new  
    44  <%%= render :partial => 'form' %> 
    55  <%%= submit_tag 'Edit' %> 
    66<%%= end_form_tag %> 
     7<%%= button_to 'Destroy', { :action => 'destroy<%= suffix %>', :id => @<%= singular_name %> }, :confirm => 'Are you sure you want to destroy <%= singular_name %>?' %> 
    78 
    89<%%= link_to 'Show', :action => 'show<%= suffix %>', :id => @<%= singular_name %> %> | 
    910<%%= link_to 'Back', :action => 'list<%= suffix %>' %> 
  • railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb

    old new  
    11class <%= controller_class_name %>Controller < ApplicationController 
     2  verify :only => [ :show<%= suffix %>, :edit<%= suffix %>, :update<%= suffix %>, :destroy<%= suffix %> ], 
     3         :params => :id, 
     4         :add_flash => { :notice => '<%= model_name %> ID missing.' }, 
     5         :redirect_to => { :action => 'list<%= suffix %>' } 
     6 
    27<% unless suffix -%> 
    38  def index 
    49    list 
     
    1217 
    1318<% end -%> 
    1419  def list<%= suffix %> 
    15     @<%= singular_name %>_pages, @<%= plural_name %> = paginate :<%= plural_name %>, :per_page => 10 
     20    @<%= singular_name %>_pages, @<%= plural_name %> = paginate(:<%= plural_name %>, :per_page => 10) 
    1621  end 
    1722 
    1823  def show<%= suffix %> 
     
    2530 
    2631  def create<%= suffix %> 
    2732    @<%= singular_name %> = <%= model_name %>.new(params[:<%= singular_name %>]) 
    28     if @<%= singular_name %>.save 
    29       flash[:notice] = '<%= model_name %> was successfully created.' 
    30       redirect_to :action => 'list<%= suffix %>' 
     33    if request.post? 
     34      if @<%= singular_name %>.save 
     35        flash[:notice] = '<%= model_name %> was successfully created.' 
     36        redirect_to :action => 'list<%= suffix %>' 
     37      else 
     38        render :action => 'new<%= suffix %>' 
     39      end 
    3140    else 
     41      flash[:notice] = '<%= model_name %> must be created using HTTP POST.' 
    3242      render :action => 'new<%= suffix %>' 
    3343    end 
    3444  end 
     
    3949 
    4050  def update 
    4151    @<%= singular_name %> = <%= model_name %>.find(params[:id]) 
    42     if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>]) 
    43       flash[:notice] = '<%= model_name %> was successfully updated.' 
    44       redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %> 
     52    if request.post? 
     53      if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>]) 
     54        flash[:notice] = '<%= model_name %> was successfully updated.' 
     55        redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %> 
     56      else 
     57        render :action => 'edit<%= suffix %>' 
     58      end 
    4559    else 
     60      flash[:notice] = '<%= model_name %> must be updated using HTTP POST.' 
    4661      render :action => 'edit<%= suffix %>' 
    4762    end 
    4863  end 
    4964 
    5065  def destroy<%= suffix %> 
    51     <%= model_name %>.find(params[:id]).destroy 
    52     redirect_to :action => 'list<%= suffix %>' 
     66    if request.post? 
     67      <%= model_name %>.find(params[:id]).destroy 
     68      flash[:notice] = '<%= model_name %> was successfully destroyed.' 
     69      redirect_to :action => 'list<%= suffix %>' 
     70    else 
     71      flash[:notice] = 'Click Destroy to destroy <%= model_name %>.' 
     72      redirect_to :action => 'edit<%= suffix %>', :id => params[:id] 
     73    end 
    5374  end 
    5475end 
  • railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml

    old new  
    1414  <%% end %> 
    1515    <td><%%= link_to 'Show', :action => 'show<%= suffix %>', :id => <%= singular_name %> %></td> 
    1616    <td><%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => <%= singular_name %> %></td> 
    17     <td><%%= link_to 'Destroy', { :action => 'destroy<%= suffix %>', :id => <%= singular_name %> }, :confirm => 'Are you sure?' %></td> 
     17    <td><%%= link_to 'Destroy', { :action => 'destroy<%= suffix %>', :id => <%= singular_name %> }, :post => true, :confirm => 'Are you sure you want to delete <%= singular_name %>?' %></td> 
    1818  </tr> 
    1919<%% end %> 
    2020</table>