Last active
December 24, 2015 13:59
-
-
Save mgidea/6809750 to your computer and use it in GitHub Desktop.
initial spec page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
feature "User edits ther review", %Q{ | |
As an author of a review | |
I want to be able to edit my review | |
} do | |
# Acceptance Criteria | |
# * Only the author of a review can edit a review | |
# * non-authors will not see a link to edit | |
scenario "author successfully edits review" do | |
user = FactoryGirl.create(:user) | |
new_gem = FactoryGirl.create(:ruby_gem) | |
review = FactoryGirl.create(:review, user: user, ruby_gem: new_gem) | |
user_signs_in(user) | |
visit ruby_gem_path(new_gem) | |
click_link "Edit" | |
fill_in "Title", with: "Terrible" | |
fill_in "Content", with: "Love this gem" | |
select 10, from: "Rating" | |
save_and_open_page | |
click_button "Update" | |
expect(review.title).to eql("Terrible") | |
end | |
def user_signs_in(user) | |
visit '/' | |
click_link 'Sign In' | |
fill_in "Email", with: user.email | |
fill_in "Password", with: user.password | |
click_button "Sign In" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :ruby_gem do | |
sequence(:name) {|n| "Gem #{n}" } | |
end | |
factory :user do | |
email "[email protected]" | |
password "password" | |
password_confirmation "password" | |
end | |
factory :review do | |
sequence(:title) {|n| "Perfect#{n}"} | |
content "It is a great gem" | |
rating 8 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Review < ActiveRecord::Base | |
attr_accessor :author | |
belongs_to :ruby_gem, | |
inverse_of: :reviews | |
belongs_to :user, | |
inverse_of: :reviews | |
validates_presence_of :title | |
validates_presence_of :rating | |
validates_presence_of :content | |
validates_presence_of :user | |
validates_presence_of :ruby_gem | |
validates_numericality_of :rating | |
validates_inclusion_of :rating, :in => 1..10 | |
def create_author(user, ruby_gem, review) | |
@author = Author.new(user, ruby_gem, review) | |
@review.save | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= simple_form_for [@ruby_gem, @ruby_gem.reviews.build] do |f| %> | |
<%= f.input :title %> | |
<%= f.input :content %> | |
<%= f.input :rating, collection: 1..10 %> | |
<%= f.submit 'Update' %> | |
<%- end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RubyGem < ActiveRecord::Base | |
#ask about where the dependent destory goes so if a | |
#gem is removed, the reviews are destroyed | |
has_many :reviews, | |
inverse_of: :ruby_gem, | |
dependent: :nullify | |
validates_presence_of :name | |
validates_uniqueness_of :name, message: 'already exists' | |
def self.author?(ruby_gem, user) | |
!ruby_gem.reviews.where(user_id: user).empty? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
feature "user adds review", %Q{ | |
As a registered and logged-in user | |
I want to add a review for a gem | |
So that I can tell people what I think of that gem | |
} do | |
# Acceptance Criteria | |
# | |
# * I have to be signed in to write a review | |
# * I can write a review for a specific gem | |
# * I can only review a gem once | |
# * I can edit my review | |
# * I can delete my review | |
scenario "user successfully adds review" do | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
prev_count = newgem.review.count | |
user_signs_in(user) | |
visit ruby_gems_path | |
click_link newgem | |
click_link "Add New Review" | |
fill_in "Rating", with: 7 | |
fill_in "Title", with: "Awesome Gem" | |
fill_in "Content", with: "It's Awesome" | |
click_button "Add Review" | |
expect(page).to have_content("Thanks for your Review") | |
expect(newgem.review.count).to eql(prev_count + 1) | |
expect(page).to have_content("Add New Gem") | |
end | |
scenario "user fails to add review" do | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
prev_count = newgem.review.count | |
user_signs_in(user) | |
visit ruby_gems_path | |
click_link newgem | |
click_link "Add New Review" | |
click_button "Add Review" | |
expect(page).to_not have_content("Thanks for your Review") | |
expect(newgem.review.count).to eql(prev_count) | |
expect(page).to have_content("can't be blank") | |
end | |
scenario "user tries to review same gem again" do | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
user_signs_in(user) | |
create_review(newgem) | |
prev_count = newgem.review.count | |
create_review(newgem) | |
expect(newgem.review.count).to eql(prev_count) | |
expect(page).to have_content("You can only review a Gem once") | |
expect(page).to_not have_content("Thanks for your Review") | |
end | |
scenario "user edits review" do | |
new_content = "It's really Awesome" | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
user_signs_in(user) | |
create_review(newgem) | |
content = newgem.content | |
visit edit_review_path | |
fill_in "Rating", with: 6 | |
fill_in "Title", with: "" | |
fill_in "Content", with: new_content | |
expect(page).to have_content("You successfully updated your review") | |
expect(content).to eql(new_content) | |
end | |
scenario "non-author fails to edit review" do | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
review = FactoryGirl.create(:review) | |
user_signs_in(user) | |
visit edit_review_path | |
expect(page).to have_content("Only the author can edit this review") | |
expect(page).to have_content("List of Reviews") | |
end | |
scenario "user deletes review" do | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
user_signs_in(user) | |
create_review(newgem) | |
prev_count = newgem.review.count | |
click_link "Destroy" | |
expect(newgem.review.count).to eql(prev_count - 1) | |
expect(page).to have_content("Your review has been deleted") | |
end | |
scenario "non-author fails to destroy or update review" do | |
user = FactoryGirl.create(:user) | |
newgem = FactoryGirl.create(:ruby_gem) | |
review = FactoryGirl.create(:review) | |
user_signs_in(user) | |
visit edit_review_path | |
expect(page).to_not have_content("Destroy") | |
expect(page).to_not have_content("Update") | |
end | |
scenario "user views a list of reviews" do | |
reviews = FactoryGirl.create_list(:review, 3) | |
visit reviews_path | |
reviews.each do |review| | |
expect(page).to have_content(review.ruby_gem.name) | |
end | |
expect(page).to have_content("List of Reviews") | |
end | |
def create_review(a_gem) | |
visit ruby_gems_path | |
click_link a_gem | |
click_link "Add New Review" | |
fill_in "Rating", with: 7 | |
fill_in "Title", with: "Awesome Gem" | |
fill_in "Content", with: "It's Awesome" | |
click_button "Add Review" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= @ruby_gem.name %> | |
<p> | |
<strong>Reviews</strong><br> | |
<% @ruby_gem.reviews.each do |review| %> | |
<%= review.title %> | |
<%= review.content %> | |
<%= review.rating %> | |
<% if RubyGem.author?(@ruby_gem, current_user) %> | |
<%= link_to "Edit", edit_ruby_gem_review_path(@ruby_gem, @review) %> | |
<% end %> | |
<%- end -%> | |
</p> | |
<% if current_user != nil %> | |
<h1>Add New Gem Review</h1> | |
<%= render 'reviews/reviews_form' %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment