From f7e07790277ed6367b6a0edab2aaac0a8c6602bb Mon Sep 17 00:00:00 2001 From: Phil Vargas Date: Mon, 24 Feb 2014 08:43:00 -0800 Subject: [PATCH 1/5] Update README.md --- README.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c3325d4..26e74ba 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,19 @@ -### Dolphin Flash Cards -Created by Chris, Phil, Anne, Armando, Jaime and Mason +Restful Dolphins Flashcards +=========================== + + Created by Chris, Phil, Anne, Armando, Jaime and Mason ### Features: -1. etc -2. etc -4. etc -5. etc -6. +1. Flash Cards Quiz +2. Supports Multiple Decks +3. Tracking of Number of Correct and Incorrect Guesses +4. User Login Authentication +5. New User Register +6. Arabic, French and Chemistry sample decks + +**Try on Heroku:** +http://obscure-wave-1778.herokuapp.com/ + **Trello at:** https://trello.com/b/cU6Dl8uK/dolphin-restful-cards From 8df601b810abf63830e89c37319ba4c80e281fd9 Mon Sep 17 00:00:00 2001 From: & Date: Thu, 27 Feb 2014 19:36:17 -0800 Subject: [PATCH 2/5] add user create/log in error catching --- app/controllers/user.rb | 18 ++++++++++++------ app/helpers/user.rb | 12 +++++------- app/views/_error.erb | 5 +++++ app/views/_invalid_login.erb | 5 +++++ app/views/create_user.erb | 2 ++ app/views/index.erb | 3 ++- db/seeds.rb | 18 +++++++++--------- public/css/application.css | 9 ++++++++- spec/deck_controller_spec.rb | 4 ++-- 9 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 app/views/_error.erb create mode 100644 app/views/_invalid_login.erb diff --git a/app/controllers/user.rb b/app/controllers/user.rb index c85b52d..eb2bf36 100644 --- a/app/controllers/user.rb +++ b/app/controllers/user.rb @@ -3,9 +3,13 @@ end post '/users' do - new_user = User.create params - session[:id] = new_user.id - redirect '/decks' + @user = User.create params + if @user.valid? + session[:id] = new_user.id + redirect '/decks' + else + erb :create_user + end end @@ -14,17 +18,19 @@ end get '/users/stats/:deck_id' do - p params @user = User.find(session[:id]) @deck = Deck.find(params[:deck_id].to_i) erb :show_stats end post '/login' do - if auth_user(params) + auth_user(params) + p params + if loggedin? redirect '/decks' else - redirect to '/' + @failed_loggin = true + erb :index end end diff --git a/app/helpers/user.rb b/app/helpers/user.rb index 194e143..e80d65f 100644 --- a/app/helpers/user.rb +++ b/app/helpers/user.rb @@ -1,15 +1,13 @@ helpers do def loggedin? - !!session["id"] + !!session[:id] end def auth_user(params) - @user = User.find_by_username(params[:username]) - if @user && @user.password == params[:password] - session[:id] = @user.id - true - else - false + user = User.find_by_username(params[:username]) + if user && user.password == params[:password] + session[:id] = user.id + user end end end \ No newline at end of file diff --git a/app/views/_error.erb b/app/views/_error.erb new file mode 100644 index 0000000..0b42566 --- /dev/null +++ b/app/views/_error.erb @@ -0,0 +1,5 @@ +<% if user && user.errors.messages[param] %> + <% user.errors.messages[param].each do |error| %> +

<%= error %>

+ <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/_invalid_login.erb b/app/views/_invalid_login.erb new file mode 100644 index 0000000..e13f13f --- /dev/null +++ b/app/views/_invalid_login.erb @@ -0,0 +1,5 @@ +<% if user %> + <% user.errors.messages[param].each do |error| %> +

<%= error %>

+ <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/create_user.erb b/app/views/create_user.erb index 5fe1a7e..b6cf307 100644 --- a/app/views/create_user.erb +++ b/app/views/create_user.erb @@ -3,7 +3,9 @@
+ <%= erb :'_error', locals: {user: @user, param: :username}%>

Username:


+ <%= erb :'_error', locals: {user: @user, param: :password}%>

Password:


diff --git a/app/views/index.erb b/app/views/index.erb index e263568..5b6a5a0 100644 --- a/app/views/index.erb +++ b/app/views/index.erb @@ -1,7 +1,8 @@

Welcome to Dophin Flashcards!

-

Username:


+

<%= "Invalid Username or password" if @failed_loggin %>

+

Username:


Password:


diff --git a/db/seeds.rb b/db/seeds.rb index de07372..96aaab8 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -11,21 +11,21 @@ end -CARDS = [{question: "واحد", answer: "one", deck_id: 1}, - {question: "اثنين", answer: "two", deck_id: 1}, - {question: "ثلاثة", answer: "three", deck_id: 1}, - {question: "كيفك", answer: "how are you?", deck_id: 1}, - {question: "صباح الخير", answer: "good morning", deck_id: 1}, - {question: "مرحبا", answer: "hello", deck_id: 1}, - {question: "d'accord", answer: "OK", deck_id: 1}, +CARDS = [{question: "one", answer: "one", deck_id: 1}, + # {question: "اثنين", answer: "two", deck_id: 1}, + # {question: "ثلاثة", answer: "three", deck_id: 1}, + # {question: "كيفك", answer: "how are you?", deck_id: 1}, + # {question: "صباح الخير", answer: "good morning", deck_id: 1}, + # {question: "مرحبا", answer: "hello", deck_id: 1}, + {question: "d'accord", answer: "OK", deck_id: 2}, {question: "qui", answer: "who", deck_id: 2}, {question: "quand", answer: "when", deck_id: 2}, - {question: "où", answer: "where", deck_id: 2}, + {question: "ou", answer: "where", deck_id: 2}, {question: "pourquoi", answer: "why", deck_id: 2}, {question: "comment", answer: "how", deck_id: 2}, {question: "Parlez-vous anglais?", answer: "Do you speak English?", deck_id: 2}, {question: "et", answer: "and", deck_id: 2}, - {question: "s'il vous plaît", answer: "please", deck_id: 2}, + {question: "s'il vous plait", answer: "please", deck_id: 2}, {question: "merci ", answer: "thank you", deck_id: 2}, {question:"What is the Caesium Chloride crystal structure? (abbreviation)", answer:"BCC", deck_id: 3}, {question:"What is the Sodium Chloride (rock salt) crystal structure?", answer:"Cubic", deck_id: 3}, diff --git a/public/css/application.css b/public/css/application.css index 23b4c11..26cb9fa 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -140,7 +140,7 @@ ul li { } ul li a { - color: yellow; + color: black; text-decoration: none; } @@ -213,3 +213,10 @@ ul li a:visited { width: 500px; z-index: 1; } + +.error-msg{ + border-radius: 5px; + width:40%; + text-align: center; + background-color: rgb(250, 149, 149); +} \ No newline at end of file diff --git a/spec/deck_controller_spec.rb b/spec/deck_controller_spec.rb index ad5e343..21fdb13 100644 --- a/spec/deck_controller_spec.rb +++ b/spec/deck_controller_spec.rb @@ -2,12 +2,12 @@ describe "Deck controller" do + let(:first_deck){FactoryGirl.create(:deck)} + let(:first_user){FactoryGirl.create(:user)} before(:all) do FactoryGirl.create(:card) first_user.decks << first_deck end - let(:first_deck){FactoryGirl.create(:deck)} - let(:first_user){FactoryGirl.create(:user)} describe "answering a question correctly" do before(:each) do From 92f255c2c130e440a03460f005548b94112567a7 Mon Sep 17 00:00:00 2001 From: & Date: Thu, 27 Feb 2014 20:55:28 -0800 Subject: [PATCH 3/5] dat ajax on the deck page. feels good to be a lannister --- app/controllers/deck.rb | 2 +- app/controllers/user.rb | 2 +- app/views/create_user.erb | 5 +---- app/views/layout.erb | 5 +++++ app/views/show_decks.erb | 2 +- public/css/application.css | 12 ++++++++++++ public/js/application.js | 16 ++++++++++++++++ 7 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/controllers/deck.rb b/app/controllers/deck.rb index 9efc231..d7295f8 100644 --- a/app/controllers/deck.rb +++ b/app/controllers/deck.rb @@ -8,7 +8,7 @@ User.find(session[:id]).rounds.create(deck_id: deck.id) session[:cards] = deck.cards.shuffle @card = session[:cards].pop - erb :show_card + erb :show_card, layout: false end post '/decks/:deck_id/cards/:card_id' do diff --git a/app/controllers/user.rb b/app/controllers/user.rb index eb2bf36..057b14b 100644 --- a/app/controllers/user.rb +++ b/app/controllers/user.rb @@ -5,7 +5,7 @@ post '/users' do @user = User.create params if @user.valid? - session[:id] = new_user.id + session[:id] = @user.id redirect '/decks' else erb :create_user diff --git a/app/views/create_user.erb b/app/views/create_user.erb index b6cf307..151ec91 100644 --- a/app/views/create_user.erb +++ b/app/views/create_user.erb @@ -8,7 +8,4 @@ <%= erb :'_error', locals: {user: @user, param: :password}%>

Password:


- - - - + \ No newline at end of file diff --git a/app/views/layout.erb b/app/views/layout.erb index d95ae46..994c189 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -14,6 +14,11 @@
<%= yield %> +
+ +
+ +
diff --git a/app/views/show_decks.erb b/app/views/show_decks.erb index e4903b1..f9911aa 100644 --- a/app/views/show_decks.erb +++ b/app/views/show_decks.erb @@ -1,5 +1,5 @@ diff --git a/public/css/application.css b/public/css/application.css index 26cb9fa..5fa9c5c 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -219,4 +219,16 @@ ul li a:visited { width:40%; text-align: center; background-color: rgb(250, 149, 149); +} + +.Gir_cage { + margin: auto; + width: 350px; +} + +.GIR { + border: 1px solid black; + display: block; + position: absolute; + margin: 150px 35px 50px 0px; } \ No newline at end of file diff --git a/public/js/application.js b/public/js/application.js index 52e01e6..c92443d 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -4,4 +4,20 @@ $(document).ready(function() { // when we try to bind to them // See: http://docs.jquery.com/Tutorials:Introducing_$(document).ready() + + $(".choose-deck").on("click",function(event){ + event.preventDefault(); + var selectedDeck = $(this); + var deckId = selectedDeck.data('id'); + console.log(deckId); + $.ajax({ + type: "GET", + url: "/decks/"+deckId, + success: function(response){ + $('.container').html(response) + console.log("win") + } + }) + }) + }); From e0a613ebd955d94c88b525956bb3a2e6679cae95 Mon Sep 17 00:00:00 2001 From: & Date: Thu, 27 Feb 2014 21:40:14 -0800 Subject: [PATCH 4/5] Adds AJAX functionality like a boss. To some of the pages. --- app/controllers/deck.rb | 5 +++-- app/controllers/user.rb | 8 ++++---- app/views/show_card.erb | 2 +- public/js/application.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/app/controllers/deck.rb b/app/controllers/deck.rb index d7295f8..df47961 100644 --- a/app/controllers/deck.rb +++ b/app/controllers/deck.rb @@ -18,11 +18,12 @@ session.delete(:cards) @answer_eval = (params[:answer].downcase == @current_card.answer.downcase) assign_score(@answer_eval) + p "inside nil" redirect to("/users/stats/#{params[:deck_id]}") else @answer_eval = (params[:answer].downcase == @current_card.answer.downcase) assign_score(@answer_eval) - erb :show_card + erb :show_card, layout: false end end @@ -33,6 +34,6 @@ session.delete(:cards) redirect to("/users/stats/#{params[:deck_id]}") else - erb :show_card + erb :show_card, layout: false end end diff --git a/app/controllers/user.rb b/app/controllers/user.rb index 057b14b..d202e28 100644 --- a/app/controllers/user.rb +++ b/app/controllers/user.rb @@ -1,5 +1,5 @@ get '/users/new' do - erb :create_user + erb :create_user, layout: false end post '/users' do @@ -8,7 +8,7 @@ session[:id] = @user.id redirect '/decks' else - erb :create_user + erb :create_user, layout: false end end @@ -20,7 +20,7 @@ get '/users/stats/:deck_id' do @user = User.find(session[:id]) @deck = Deck.find(params[:deck_id].to_i) - erb :show_stats + erb :show_stats, layout: false end post '/login' do @@ -30,7 +30,7 @@ redirect '/decks' else @failed_loggin = true - erb :index + erb :index, layout: false end end diff --git a/app/views/show_card.erb b/app/views/show_card.erb index eeeaa02..ac95a43 100644 --- a/app/views/show_card.erb +++ b/app/views/show_card.erb @@ -5,7 +5,7 @@
-

Question: <%= @card.question %> +

Question: <%= @card.question %>

diff --git a/public/js/application.js b/public/js/application.js index c92443d..eb65b40 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -20,4 +20,31 @@ $(document).ready(function() { }) }) -}); + $(document).on("submit","form",function(event){ + event.preventDefault(); + $.ajax({ + type: "POST", + url: this.attributes.action.nodeValue, + data: $('form').serialize(), + success: function(response){ + $('.container').html(response) + console.log(response) + } + }) + }); +}) + +// debugger; +// event.preventDefault(); +// var selectedDeck = $(this); +// var deckId = selectedDeck.data('id'); +// console.log(deckId); +// $.ajax({ +// type: "POST", +// url: "/decks/1/cards/1", +// success: function(response){ +// // $('.container').html(response) +// console.log(response) +// } +// }) +// }) \ No newline at end of file From 704fa52fc90d81c0f145208db84b092e1887a5b5 Mon Sep 17 00:00:00 2001 From: & Date: Thu, 27 Feb 2014 23:09:51 -0800 Subject: [PATCH 5/5] Adds AJAX better than sliced bread? ![DOPHIN](http://media.giphy.com/media/zE6UFm2tTehk4/giphy.gif) --- app/views/layout.erb | 1 - app/views/show_decks.erb | 2 +- public/css/application.css | 17 +++++---- public/js/application.js | 74 +++++++++++++++++++------------------- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/app/views/layout.erb b/app/views/layout.erb index 994c189..8bb2fc5 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -14,7 +14,6 @@
<%= yield %> -
diff --git a/app/views/show_decks.erb b/app/views/show_decks.erb index f9911aa..139383d 100644 --- a/app/views/show_decks.erb +++ b/app/views/show_decks.erb @@ -1,5 +1,5 @@ diff --git a/public/css/application.css b/public/css/application.css index 5fa9c5c..a211d74 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -122,7 +122,8 @@ form { ul { list-style: none; - margin: 30px 10px auto; + margin: 5% auto; + } @@ -152,8 +153,6 @@ ul li a:visited { margin: 0 auto; text-align: center; width: 80%; -/* position: absolute; - left: 150px;*/ } .card h2 { @@ -223,12 +222,16 @@ ul li a:visited { .Gir_cage { margin: auto; - width: 350px; + + height: 20%; + position: relative; + top: 60%; + width: 40%; } .GIR { - border: 1px solid black; + border: 10px solid #D9CDCD; display: block; - position: absolute; - margin: 150px 35px 50px 0px; + margin: auto; + } \ No newline at end of file diff --git a/public/js/application.js b/public/js/application.js index eb65b40..19687d7 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -4,47 +4,49 @@ $(document).ready(function() { // when we try to bind to them // See: http://docs.jquery.com/Tutorials:Introducing_$(document).ready() + Dophin.addGuessListener() + Dophin.addDeckListener() - $(".choose-deck").on("click",function(event){ - event.preventDefault(); - var selectedDeck = $(this); - var deckId = selectedDeck.data('id'); - console.log(deckId); + +}); + + +var Dophin = { + + addGuessListener: function(){ + $(document).on("submit","form",function(event){ + event.preventDefault(); + Dophin.cardGuess(this); + }); + }, + + addDeckListener: function(){ + $(".choose-deck").on("click",function(event){ + event.preventDefault(); + console.log(this) + Dophin.deckChoice(this); + }) + }, + + deckChoice: function(){ $.ajax({ type: "GET", - url: "/decks/"+deckId, + url: this.deckChoice.arguments[0].attributes.href.nodeValue, success: function(response){ $('.container').html(response) - console.log("win") } }) - }) + }, - $(document).on("submit","form",function(event){ - event.preventDefault(); - $.ajax({ - type: "POST", - url: this.attributes.action.nodeValue, - data: $('form').serialize(), - success: function(response){ - $('.container').html(response) - console.log(response) - } - }) - }); -}) - -// debugger; -// event.preventDefault(); -// var selectedDeck = $(this); -// var deckId = selectedDeck.data('id'); -// console.log(deckId); -// $.ajax({ -// type: "POST", -// url: "/decks/1/cards/1", -// success: function(response){ -// // $('.container').html(response) -// console.log(response) -// } -// }) -// }) \ No newline at end of file + cardGuess: function(){ + // debugger + $.ajax({ + type: "POST", + url: this.cardGuess.arguments[0].attributes.action.nodeValue, + data: $('form').serialize(), + success: function(response){ + $('.container').html(response) + } + }) + } +} \ No newline at end of file