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

Ticket #3352: hardened_scaffold.diff

File hardened_scaffold.diff, 6.3 kB (added by nils@alumni.rice.edu, 3 years ago)
  • railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb

    old new  
    5757    assert_not_nil assigns(:<%= singular_name %>) 
    5858  end 
    5959 
    60   def test_create 
     60  def test_create<%= suffix %>_using_post 
    6161    num_<%= plural_name %> = <%= model_name %>.count 
    6262 
    6363    post :create<%= suffix %>, :<%= singular_name %> => {} 
     
    6868    assert_equal num_<%= plural_name %> + 1, <%= model_name %>.count 
    6969  end 
    7070 
     71  def test_create<%= suffix %>_using_get 
     72    num_<%= plural_name %> = <%= model_name %>.count 
     73 
     74    get :create<%= suffix %>, :<%= singular_name %> => {} 
     75 
     76    assert_response :success 
     77    assert_template 'new<%= suffix %>' 
     78 
     79    assert_equal num_<%= plural_name %>, <%= model_name %>.count 
     80  end 
     81 
    7182  def test_edit<%= suffix %> 
    7283    get :edit<%= suffix %>, :id => 1 
    7384 
     
    7889    assert assigns(:<%= singular_name %>).valid? 
    7990  end 
    8091 
    81   def test_update<%= suffix %> 
     92  def test_update<%= suffix %>_using_post 
    8293    post :update<%= suffix %>, :id => 1 
    8394    assert_response :redirect 
    8495    assert_redirected_to :action => 'show<%= suffix %>', :id => 1 
    8596  end 
    8697 
    87   def test_destroy<%= suffix %> 
     98  def test_update<%= suffix %>_using_get 
     99    get :update<%= suffix %>, :id => 1 
     100    assert_response :success 
     101    assert_template 'edit<%= suffix %>' 
     102  end 
     103 
     104  def test_destroy<%= suffix %>_using_post 
    88105    assert_not_nil <%= model_name %>.find(1) 
    89106 
    90107    post :destroy, :id => 1 
     
    95112      <%= model_name %>.find(1) 
    96113    } 
    97114  end 
     115 
     116  def test_destroy<%= suffix %>_using_get 
     117    assert_not_nil <%= model_name %>.find(1) 
     118 
     119    get :destroy, :id => 1 
     120    assert_response :redirect 
     121    assert_redirected_to :action => 'edit<%= suffix %>' 
     122 
     123    assert_not_nil <%= model_name %>.find(1) 
     124  end 
    98125end 
  • 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  
    1212 
    1313<% end -%> 
    1414  def list<%= suffix %> 
    15     @<%= singular_name %>_pages, @<%= plural_name %> = paginate :<%= plural_name %>, :per_page => 10 
     15    @<%= singular_name %>_pages, @<%= plural_name %> = paginate(:<%= plural_name %>, :per_page => 10) 
    1616  end 
    1717 
    1818  def show<%= suffix %> 
     
    2525 
    2626  def create<%= suffix %> 
    2727    @<%= 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 %>' 
     28    if request.post? 
     29      if @<%= singular_name %>.save 
     30        flash[:notice] = '<%= model_name %> was successfully created.' 
     31        redirect_to :action => 'list<%= suffix %>' 
     32      else 
     33        render :action => 'new<%= suffix %>' 
     34      end 
    3135    else 
     36      flash[:notice] = '<%= model_name %> must be created using HTTP POST.' 
    3237      render :action => 'new<%= suffix %>' 
    3338    end 
    3439  end 
     
    3944 
    4045  def update 
    4146    @<%= 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 %> 
     47    if request.post? 
     48      if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>]) 
     49        flash[:notice] = '<%= model_name %> was successfully updated.' 
     50        redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %> 
     51      else 
     52        render :action => 'edit<%= suffix %>' 
     53      end 
    4554    else 
     55      flash[:notice] = '<%= model_name %> must be updated using HTTP POST.' 
    4656      render :action => 'edit<%= suffix %>' 
    4757    end 
    4858  end 
    4959 
    5060  def destroy<%= suffix %> 
    51     <%= model_name %>.find(params[:id]).destroy 
    52     redirect_to :action => 'list<%= suffix %>' 
     61    if request.post? 
     62      <%= model_name %>.find(params[:id]).destroy 
     63      flash[:notice] = '<%= model_name %> was successfully destroyed.' 
     64      redirect_to :action => 'list<%= suffix %>' 
     65    else 
     66      flash[:notice] = 'Click Destroy to destroy <%= model_name %>.' 
     67      redirect_to :action => 'edit<%= suffix %>', :id => params[:id] 
     68    end 
    5369  end 
    5470end 
  • 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>