changeset 265:6cca1ab53337

Infinitely better error messages and notice.
author edogawaconan <me@myconan.net>
date Wed, 16 Oct 2013 02:56:30 +0900
parents fa4a1ba990ae
children c3080316b4dd
files app/assets/stylesheets/app_bootstrap.scss app/assets/stylesheets/pastes.css app/controllers/pastes_controller.rb app/models/paste.rb app/views/pastes/_form.html.erb app/views/pastes/new.html.erb app/views/pastes/show.html.erb
diffstat 7 files changed, 37 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/app/assets/stylesheets/app_bootstrap.scss	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/assets/stylesheets/app_bootstrap.scss	Wed Oct 16 02:56:30 2013 +0900
@@ -1,2 +1,7 @@
 @import 'twitter/bootstrap';
 @import 'sticky-footer';
+
+// Rails support.
+.field_with_errors {
+  @extend .has-error;
+}
--- a/app/assets/stylesheets/pastes.css	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/assets/stylesheets/pastes.css	Wed Oct 16 02:56:30 2013 +0900
@@ -15,3 +15,7 @@
 .edit_paste {
   margin-bottom: 20px;
 }
+
+#paste_key {
+  border-radius: 4px 0 0 4px;
+}
--- a/app/controllers/pastes_controller.rb	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/controllers/pastes_controller.rb	Wed Oct 16 02:56:30 2013 +0900
@@ -48,6 +48,7 @@
           format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
           format.json { render :json => @paste, :status => :created, :location => @paste }
         else
+          flash[:alert] = @paste.errors.full_messages.to_sentence
           format.html { render :action => "new" }
           format.json { render :json => @paste.errors, :status => :unprocessable_entity }
         end
@@ -65,14 +66,12 @@
 
   def destroy
     @paste = Paste.safe_find(params[:id])
-    if @paste.key == params[:paste][:key]
-      @paste.destroy
+    if @paste.safe_destroy(params[:paste][:key])
       expire_page :controller => 'pastes', :action => 'show', :id => @paste.id
-      flash[:notice] = "Paste ##{params[:id]} deleted"
-      redirect_to root_path
+      redirect_to root_path, :notice => "Paste ##{params[:id]} deleted"
     else
-      flash[:error] = 'Incorrect deletion key'
-      render :action => :show
+      flash[:alert] = @paste.errors.full_messages.to_sentence
+      render :show
     end
   end
 
--- a/app/models/paste.rb	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/models/paste.rb	Wed Oct 16 02:56:30 2013 +0900
@@ -6,7 +6,7 @@
   before_validation :set_paste_key
   before_validation :set_paste_secret
   validates :paste, :paste_hash, :key, :ip, :presence => true
-  validates :paste, :length => { :maximum => 1_000_000 }
+  validates :paste, :length => { :maximum => 1 }
 
   def to_param
     path
@@ -22,6 +22,15 @@
     end
   end
 
+  def safe_destroy(param_key)
+    if key == param_key
+      destroy
+    else
+      errors.add(:key, "is invalid")
+      false
+    end
+  end
+
   def path
     [id, secret.presence].compact.join("-")
   end
--- a/app/views/pastes/_form.html.erb	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/views/pastes/_form.html.erb	Wed Oct 16 02:56:30 2013 +0900
@@ -1,16 +1,4 @@
 <%= form_for @paste do |f| %>
-  <% if @paste.errors.any? %>
-    <div class="alert alert-error" id="error_explanation">
-      <h5><%= pluralize(@paste.errors.count, "error") %> prohibited this paste from being saved:</h5>
-
-      <ul>
-      <% @paste.errors.full_messages.each do |msg| %>
-        <li><%= msg %></li>
-      <% end %>
-      </ul>
-    </div>
-  <% end %>
-
   <div class="form-group">
     <%= f.text_area :paste, :rows => 10, :class => "form-control" %>
   </div>
--- a/app/views/pastes/new.html.erb	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/views/pastes/new.html.erb	Wed Oct 16 02:56:30 2013 +0900
@@ -1,8 +1,14 @@
 <% provide :title, 'New Paste' %>
 
-<% if flash[:notice] %>
+<% if notice %>
   <div class="alert alert-info">
-    <%= flash[:notice] %>
+    <%= notice %>
+  </div>
+<% end %>
+
+<% if alert %>
+  <div class="alert alert-danger">
+    <%= alert %>
   </div>
 <% end %>
 
--- a/app/views/pastes/show.html.erb	Wed Oct 16 02:19:36 2013 +0900
+++ b/app/views/pastes/show.html.erb	Wed Oct 16 02:56:30 2013 +0900
@@ -1,8 +1,8 @@
 <% provide :title, "Paste ##{@paste.id}" %>
 
-<% if flash[:error] %>
-  <div class="alert alert-error">
-    <%= flash[:error] %>
+<% if alert %>
+  <div class="alert alert-danger">
+    <%= alert %>
   </div>
 <% end %>
 
@@ -31,9 +31,10 @@
 </div>
 
 <%= form_for @paste, :method => :delete do |f| %>
+
   <div class="row"><div class="col-md-4">
     <div class="input-group">
-      <%= f.text_field :key, :value => nil, :placeholder => 'Deletion key', :class => "form-control", :autofocus => !flash[:error].blank? %>
+      <%= f.text_field :key, :value => nil, :placeholder => 'Deletion key', :class => "form-control", :autofocus => f.object.errors.any? %>
       <span class="input-group-btn">
         <%= f.submit 'Delete this paste', :class => 'btn btn-danger' %>
       </span>