# HG changeset patch # User edogawaconan # Date 1381859790 -32400 # Node ID 6cca1ab5333718e6a0a04386d68b6f4e15ad7f2a # Parent fa4a1ba990ae529a2bd58b72ab59801009a1e653 Infinitely better error messages and notice. diff -r fa4a1ba990ae -r 6cca1ab53337 app/assets/stylesheets/app_bootstrap.scss --- 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; +} diff -r fa4a1ba990ae -r 6cca1ab53337 app/assets/stylesheets/pastes.css --- 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; +} diff -r fa4a1ba990ae -r 6cca1ab53337 app/controllers/pastes_controller.rb --- 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 diff -r fa4a1ba990ae -r 6cca1ab53337 app/models/paste.rb --- 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 diff -r fa4a1ba990ae -r 6cca1ab53337 app/views/pastes/_form.html.erb --- 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? %> -
-
<%= pluralize(@paste.errors.count, "error") %> prohibited this paste from being saved:
- - -
- <% end %> -
<%= f.text_area :paste, :rows => 10, :class => "form-control" %>
diff -r fa4a1ba990ae -r 6cca1ab53337 app/views/pastes/new.html.erb --- 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 %>
- <%= flash[:notice] %> + <%= notice %> +
+<% end %> + +<% if alert %> +
+ <%= alert %>
<% end %> diff -r fa4a1ba990ae -r 6cca1ab53337 app/views/pastes/show.html.erb --- 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] %> -
- <%= flash[:error] %> +<% if alert %> +
+ <%= alert %>
<% end %> @@ -31,9 +31,10 @@
<%= form_for @paste, :method => :delete do |f| %> +
- <%= 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? %> <%= f.submit 'Delete this paste', :class => 'btn btn-danger' %>