From 002d7b2c2d9e0e83e6aafff622629d54143cb4bd Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Tue, 1 Dec 2015 14:53:41 -0800 Subject: [PATCH 01/10] I'm not a big fan of destroying things anymore --- config/routes.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 22c7a2a..4042854 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,10 +3,11 @@ # Main page allows sign in, sign up, and try root 'logins#root' # User has prayers and praises. Must log on to edit - posts_actions = [:index, :show, :create, :edit, :update] - resource :user, :only => [:create, :new, :edit, :show, :update] - resources :prayers, :only => posts_actions - resources :praises, :only => posts_actions + resource :user, :only => [:create, :new, :edit, :show, :update] + resources :prayers, except: :destroy do + patch 'complete', on: :member + end + resources :praises, except: :destroy # Login authenticates or displays, hacking in /login as the edit path by calling it show resource :login, :only => [:new, :create, :show] # Log out with safe post action From a0e67b67a72c689c0edb65b87e9f3506ce1c2f3e Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Tue, 1 Dec 2015 14:59:56 -0800 Subject: [PATCH 02/10] complete prayers --- app/controllers/prayers_controller.rb | 13 ++++++++++--- test/controllers/prayers_controller_test.rb | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/controllers/prayers_controller.rb b/app/controllers/prayers_controller.rb index 5b1d539..9ef2313 100644 --- a/app/controllers/prayers_controller.rb +++ b/app/controllers/prayers_controller.rb @@ -18,19 +18,26 @@ def create end end def edit - @prayer = @user.prayers.find_by_id(params[:id]) || - Prayer.new({person_id: @user.id}) + @prayer = find_user_prayer || Prayer.new({person_id: @user.id}) end def update - @prayer = @user.prayers.find_by_id(params[:id]) + @prayer = find_user_prayer @prayer.update!(prayer_param) if @prayer.present? redirect_to index end + def complete + @prayer = find_user_prayer + @prayer.becomes(Praise).update!(type: "Praise") if @prayer.present? + redirect_to index + end private def assign_current_user @user = current_user.becomes(User) end + def find_user_prayer + @user.prayers.find_by_id(params[:id]) + end def prayer_param params.require(:prayer).permit(:body, :title) end diff --git a/test/controllers/prayers_controller_test.rb b/test/controllers/prayers_controller_test.rb index 451b10a..bf4bde8 100644 --- a/test/controllers/prayers_controller_test.rb +++ b/test/controllers/prayers_controller_test.rb @@ -28,14 +28,24 @@ class PrayersControllerTest < ActionController::TestCase id: posts(:prayer).id, prayer: {title: "new", body: "new body"}, user_id: posts(:prayer).person_id - ) + ) assert_redirected_to prayers_path end - test "should find Prayer outside user context through show" do get(:show, {id: posts(:prayer).id}) assert_response :success end + test "should remove prayer when complete" do + assert_difference('Prayer.count') do + patch(:complete, + id: posts(:prayer).id, + nil, + user_id: posts(:prayer).person_id + ) + end + assert_redirected_to prayers_path + end + end From d094599e9ec25fa7620275bd450ac5d931fae9d1 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Tue, 1 Dec 2015 15:07:31 -0800 Subject: [PATCH 03/10] hows unicode work here? --- app/views/prayers/index.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/prayers/index.html.erb b/app/views/prayers/index.html.erb index 65d579e..26fdf0d 100644 --- a/app/views/prayers/index.html.erb +++ b/app/views/prayers/index.html.erb @@ -3,6 +3,7 @@ <% end %> <%= render 'new' %> <% @prayers.each do |prayer| %> + <%= button_to "\u2714", complete_prayer_path(@prayer) %>

<%= prayer.title %>

<%= prayer.body %>

From 5e99f9982458ca24d0206201b453bb4de756a295 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Tue, 1 Dec 2015 15:14:07 -0800 Subject: [PATCH 04/10] not sure what will be sent --- test/controllers/prayers_controller_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/controllers/prayers_controller_test.rb b/test/controllers/prayers_controller_test.rb index bf4bde8..b33885f 100644 --- a/test/controllers/prayers_controller_test.rb +++ b/test/controllers/prayers_controller_test.rb @@ -39,9 +39,9 @@ class PrayersControllerTest < ActionController::TestCase test "should remove prayer when complete" do assert_difference('Prayer.count') do - patch(:complete, + patch(:complete, id: posts(:prayer).id, - nil, + prayer: {}, user_id: posts(:prayer).person_id ) end From 65601054cc37145bfcd1992f6d6d1be6a36fbc55 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Tue, 1 Dec 2015 15:19:26 -0800 Subject: [PATCH 05/10] scope --- app/views/prayers/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/prayers/index.html.erb b/app/views/prayers/index.html.erb index 26fdf0d..c314ce2 100644 --- a/app/views/prayers/index.html.erb +++ b/app/views/prayers/index.html.erb @@ -3,7 +3,7 @@ <% end %> <%= render 'new' %> <% @prayers.each do |prayer| %> - <%= button_to "\u2714", complete_prayer_path(@prayer) %> + <%= button_to "\u2714", complete_prayer_path(prayer) %>

<%= prayer.title %>

<%= prayer.body %>

From cbb06cc17929e4971dec69e36e3dffee8b71acbf Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Tue, 1 Dec 2015 15:22:15 -0800 Subject: [PATCH 06/10] post action ok --- config/routes.rb | 2 +- test/controllers/prayers_controller_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 4042854..2938401 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,7 @@ # User has prayers and praises. Must log on to edit resource :user, :only => [:create, :new, :edit, :show, :update] resources :prayers, except: :destroy do - patch 'complete', on: :member + post 'complete', on: :member end resources :praises, except: :destroy # Login authenticates or displays, hacking in /login as the edit path by calling it show diff --git a/test/controllers/prayers_controller_test.rb b/test/controllers/prayers_controller_test.rb index b33885f..48355bf 100644 --- a/test/controllers/prayers_controller_test.rb +++ b/test/controllers/prayers_controller_test.rb @@ -38,8 +38,8 @@ class PrayersControllerTest < ActionController::TestCase end test "should remove prayer when complete" do - assert_difference('Prayer.count') do - patch(:complete, + assert_difference('Prayer.count', -1) do + post(:complete, id: posts(:prayer).id, prayer: {}, user_id: posts(:prayer).person_id From b50c43a62d3a2e6c57cb34d7b60ef8a05cd032a1 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Thu, 3 Dec 2015 15:11:00 -0800 Subject: [PATCH 07/10] move function to model --- app/controllers/prayers_controller.rb | 2 +- app/models/prayer.rb | 4 ++++ test/fixtures/posts.yml | 6 ++++++ test/models/prayer_test.rb | 9 ++++++--- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/prayers_controller.rb b/app/controllers/prayers_controller.rb index 9ef2313..f041197 100644 --- a/app/controllers/prayers_controller.rb +++ b/app/controllers/prayers_controller.rb @@ -27,7 +27,7 @@ def update end def complete @prayer = find_user_prayer - @prayer.becomes(Praise).update!(type: "Praise") if @prayer.present? + @prayer.complete! if @prayer.present? redirect_to index end diff --git a/app/models/prayer.rb b/app/models/prayer.rb index 57ef120..01c03b7 100644 --- a/app/models/prayer.rb +++ b/app/models/prayer.rb @@ -1,3 +1,7 @@ class Prayer < Post validates :title, presence: true + + def complete! + self.update!(type: "Praise") + end end diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml index 14ec664..c3fbb48 100644 --- a/test/fixtures/posts.yml +++ b/test/fixtures/posts.yml @@ -6,6 +6,12 @@ prayer: type: Prayer person: person_with_name +second_prayer: + title: MyString_too + body: MyText_too + type: Prayer + person: person_with_name + praise: title: MyString body: MyText diff --git a/test/models/prayer_test.rb b/test/models/prayer_test.rb index 1c00106..66699b7 100644 --- a/test/models/prayer_test.rb +++ b/test/models/prayer_test.rb @@ -1,7 +1,10 @@ require 'test_helper' class PrayerTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + test "should respond to complete!" do + @post = posts(:second_prayer) + @post.complete! + assert @post.is_a?(Praise) + end end From d220ee73dfe7efb6ebf3e03c69660f8cd80017fb Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Mon, 7 Dec 2015 11:36:15 -0800 Subject: [PATCH 08/10] test updates routing indent fix --- config/routes.rb | 2 +- test/controllers/prayers_controller_test.rb | 18 ++++++++---------- test/models/prayer_test.rb | 5 ++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 2938401..c4bdc98 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ # Main page allows sign in, sign up, and try root 'logins#root' # User has prayers and praises. Must log on to edit - resource :user, :only => [:create, :new, :edit, :show, :update] + resource :user, :only => [:create, :new, :edit, :show, :update] resources :prayers, except: :destroy do post 'complete', on: :member end diff --git a/test/controllers/prayers_controller_test.rb b/test/controllers/prayers_controller_test.rb index 48355bf..5fa677c 100644 --- a/test/controllers/prayers_controller_test.rb +++ b/test/controllers/prayers_controller_test.rb @@ -25,10 +25,10 @@ class PrayersControllerTest < ActionController::TestCase test "should update existing prayer" do patch(:update, - id: posts(:prayer).id, - prayer: {title: "new", body: "new body"}, - user_id: posts(:prayer).person_id - ) + id: posts(:prayer).id, + prayer: {title: "new", body: "new body"}, + user_id: posts(:prayer).person_id + ) assert_redirected_to prayers_path end @@ -38,13 +38,11 @@ class PrayersControllerTest < ActionController::TestCase end test "should remove prayer when complete" do - assert_difference('Prayer.count', -1) do - post(:complete, - id: posts(:prayer).id, - prayer: {}, - user_id: posts(:prayer).person_id + post(:complete, + id: posts(:prayer).id, + prayer: {}, + user_id: posts(:prayer).person_id ) - end assert_redirected_to prayers_path end diff --git a/test/models/prayer_test.rb b/test/models/prayer_test.rb index 66699b7..8b87f79 100644 --- a/test/models/prayer_test.rb +++ b/test/models/prayer_test.rb @@ -3,8 +3,7 @@ class PrayerTest < ActiveSupport::TestCase test "should respond to complete!" do - @post = posts(:second_prayer) - @post.complete! - assert @post.is_a?(Praise) + posts(:second_prayer).complete! + assert posts(:second_prayer).is_a?(Praise) end end From 2a1fc59d9072b4a13de9c6e5c4048fb331fdcd4e Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Mon, 7 Dec 2015 11:40:25 -0800 Subject: [PATCH 09/10] still failing --- test/models/prayer_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/models/prayer_test.rb b/test/models/prayer_test.rb index 8b87f79..248886d 100644 --- a/test/models/prayer_test.rb +++ b/test/models/prayer_test.rb @@ -3,7 +3,7 @@ class PrayerTest < ActiveSupport::TestCase test "should respond to complete!" do - posts(:second_prayer).complete! - assert posts(:second_prayer).is_a?(Praise) + @p = posts(:second_prayer).complete! + assert @p.type = "Praise" end end From 0a030a3788fc7cd57b888ed27adcd73a70df8304 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Mon, 7 Dec 2015 11:41:35 -0800 Subject: [PATCH 10/10] again --- test/models/prayer_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/models/prayer_test.rb b/test/models/prayer_test.rb index 248886d..8cd7380 100644 --- a/test/models/prayer_test.rb +++ b/test/models/prayer_test.rb @@ -3,7 +3,8 @@ class PrayerTest < ActiveSupport::TestCase test "should respond to complete!" do - @p = posts(:second_prayer).complete! + @p = posts(:second_prayer) + @p.complete! assert @p.type = "Praise" end end