diff --git a/app/controllers/prayers_controller.rb b/app/controllers/prayers_controller.rb index 5b1d539..f041197 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.complete! 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/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/app/views/prayers/index.html.erb b/app/views/prayers/index.html.erb index 65d579e..c314ce2 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.body %>
diff --git a/config/routes.rb b/config/routes.rb index 22c7a2a..c4bdc98 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 + resources :prayers, except: :destroy do + post '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 diff --git a/test/controllers/prayers_controller_test.rb b/test/controllers/prayers_controller_test.rb index 451b10a..5fa677c 100644 --- a/test/controllers/prayers_controller_test.rb +++ b/test/controllers/prayers_controller_test.rb @@ -25,17 +25,25 @@ 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 - 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 + post(:complete, + id: posts(:prayer).id, + prayer: {}, + user_id: posts(:prayer).person_id + ) + assert_redirected_to prayers_path + 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..8cd7380 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 + @p = posts(:second_prayer) + @p.complete! + assert @p.type = "Praise" + end end