Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fguillen/530978 to your computer and use it in GitHub Desktop.
Save fguillen/530978 to your computer and use it in GitHub Desktop.
From 4b8ac1aa47900037d31896e3564568510b3fd879 Mon Sep 17 00:00:00 2001
From: Fernando Guillen <fguillen.mail@gmail.com>
Date: Tue, 17 Aug 2010 19:07:55 +0200
Subject: [PATCH] Alert if trying to create an empty Quote
---
public/stylesheets/screen.css | 11 ++++++++++-
quotes.rb | 31 +++++++++++++++++++++++++++----
spec/adding_quotes_spec.rb | 17 +++++++++++++++++
spec/amending_quotes_spec.rb | 17 +++++++++++++++++
templates/layout.mustache | 1 +
5 files changed, 72 insertions(+), 5 deletions(-)
diff --git a/public/stylesheets/screen.css b/public/stylesheets/screen.css
index 73f16ce..16de2cd 100644
--- a/public/stylesheets/screen.css
+++ b/public/stylesheets/screen.css
@@ -363,4 +363,13 @@ form#openid_signin {
form#openid_signin label, form#openid_signin input {
margin-right:0.5em;
-}
\ No newline at end of file
+}
+
+div.alert{
+ background-color:white;
+ margin:0 0 1em 4.0em;
+ padding:0.8em 2.5em;
+ font-size:3.5em;
+ color:#686F7E;
+ text-align: right;
+}
diff --git a/quotes.rb b/quotes.rb
index 982efb6..de8253c 100644
--- a/quotes.rb
+++ b/quotes.rb
@@ -51,13 +51,24 @@ module Quotes
end
post "/quotes" do
- Quote.create!(params.merge(:user => @current_user))
- redirect "/#{@current_user[:nickname]}"
+ begin
+ Quote.create!(params.merge(:user => @current_user))
+ redirect "/#{@current_user[:nickname]}"
+ rescue Exception => e
+ @alert = e.message
+ mustache :new_quote
+ end
end
put "/quotes/:id" do |id|
- Quote.update(params.merge(:user => @current_user, :id => id))
- redirect "/"
+ begin
+ Quote.update(params.merge(:user => @current_user, :id => id))
+ redirect "/"
+ rescue Exception => e
+ @quote = Quote.by_id(id)
+ @alert = e.message
+ mustache :edit_quote
+ end
end
get '/authors' do
@@ -132,6 +143,14 @@ module Quotes
def anonymous?
@current_user.blank?
end
+
+ def alert?
+ !@alert.blank?
+ end
+
+ def alert
+ @alert
+ end
end
class Login < Layout
@@ -256,6 +275,8 @@ module Quotes
class Quote < Model
def self.create!(params)
+ raise "It is not very usefull a Quote without quote." if params[:text].empty?
+
params.merge! :id => redis.incr("quotes:quotes:last_id")
params[:html_text] = RedCloth.new(params[:text]).to_html
data = encode(params)
@@ -286,6 +307,8 @@ module Quotes
end
def self.update(params)
+ raise "It is not very usefull a Quote without quote." if params[:text].empty?
+
params[:html_text] = RedCloth.new(params[:text]).to_html
data = encode(params)
raise unless params[:user] == by_id(params[:id])[:user]
diff --git a/spec/adding_quotes_spec.rb b/spec/adding_quotes_spec.rb
index b226a1e..d24a959 100644
--- a/spec/adding_quotes_spec.rb
+++ b/spec/adding_quotes_spec.rb
@@ -84,6 +84,23 @@ feature "Adding quotes" do
end
+ scenario "Creating quotes empty should return error" do
+ user = create_user :nickname => "jdoe"
+ login_as user
+
+ visit "/"
+
+ click_link "Add new quote"
+
+ within(:css, "#new_quote") do
+ fill_in "Quote", :with => ""
+ click_button "Add quote!"
+ end
+
+ page.should have_css( "div.alert" )
+ end
+
+
context "only JS" do
before { Capybara.current_driver = :culerity }
diff --git a/spec/amending_quotes_spec.rb b/spec/amending_quotes_spec.rb
index dfb2f04..3482e98 100644
--- a/spec/amending_quotes_spec.rb
+++ b/spec/amending_quotes_spec.rb
@@ -87,4 +87,21 @@ feature "Amending quotes" do
page.should_not have_css("a", :text => "Amend")
end
+ scenario "Amend quotes empty should return error" do
+ create_quote :user => @user, :text => "Wrong text", :author => "Wrong author"
+
+ login_as @user
+ visit "/"
+
+ within(:css, ".quote") do
+ click_link "Amend"
+ end
+
+ within(:css, "#edit_quote") do
+ fill_in "Quote", :with => ""
+ click_button "Amend quote"
+ end
+
+ page.should have_css( "div.alert" )
+ end
end
\ No newline at end of file
diff --git a/templates/layout.mustache b/templates/layout.mustache
index b9dcf38..a9ef71d 100644
--- a/templates/layout.mustache
+++ b/templates/layout.mustache
@@ -35,6 +35,7 @@
</div>
</div>
<div id="content">
+ {{# alert? }}<div class="alert">{{alert}}</div>{{/ alert? }}
{{{yield}}}
</div>
<div id="footer">
--
1.7.2.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment