Skip to content

Commit

Permalink
Merge pull request #9 from AndyGauge/feature/prayer-tests
Browse files Browse the repository at this point in the history
Feature/prayer tests
  • Loading branch information
AndyGauge committed Dec 7, 2015
2 parents eecdbce + 0a030a3 commit 498919f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
13 changes: 10 additions & 3 deletions app/controllers/prayers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/prayer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Prayer < Post
validates :title, presence: true

def complete!
self.update!(type: "Praise")
end
end
1 change: 1 addition & 0 deletions app/views/prayers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<% end %>
<%= render 'new' %>
<% @prayers.each do |prayer| %>
<%= button_to "\u2714", complete_prayer_path(prayer) %>
<div class="pb-prayer-wrap">
<h3><a href="<%= edit_prayer_path(prayer) %>"><%= prayer.title %></a></h3>
<p><%= prayer.body %></p>
Expand Down
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions test/controllers/prayers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/fixtures/posts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions test/models/prayer_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 498919f

Please sign in to comment.