diff --git a/app/controllers/listings_controller.rb b/app/controllers/listings_controller.rb index b0d2c6b..a7f7ec6 100644 --- a/app/controllers/listings_controller.rb +++ b/app/controllers/listings_controller.rb @@ -3,6 +3,10 @@ def index @listings = Listing.all end + def show + @listing = Listing.find(params[:id]) + end + def new @listing = Listing.new end diff --git a/app/models/listing.rb b/app/models/listing.rb index ce2f2ed..ef65d40 100644 --- a/app/models/listing.rb +++ b/app/models/listing.rb @@ -1,3 +1,4 @@ class Listing < ApplicationRecord + has_many :offers validates_presence_of :pet_name, :pet_location, :pet_description, :start_date, :end_date, :pet_picture end diff --git a/app/models/offer.rb b/app/models/offer.rb new file mode 100644 index 0000000..38035cc --- /dev/null +++ b/app/models/offer.rb @@ -0,0 +1,4 @@ +class Offer < ApplicationRecord + belongs_to :listing + validates_presence_of :name, :email, :location, :price +end diff --git a/app/views/listings/index.html.haml b/app/views/listings/index.html.haml index 4bf7960..2c52c68 100644 --- a/app/views/listings/index.html.haml +++ b/app/views/listings/index.html.haml @@ -7,3 +7,4 @@ %p= listing.start_date %p= listing.end_date %p= listing.pet_picture + %p= link_to "Show listing", listing_path(listing) diff --git a/app/views/listings/show.html.haml b/app/views/listings/show.html.haml new file mode 100644 index 0000000..33f634c --- /dev/null +++ b/app/views/listings/show.html.haml @@ -0,0 +1,15 @@ +%h1= @listing.pet_name +%p=@listing.pet_picture +%p Dates: #{@listing.start_date} to #{@listing.end_date} + +%h2 Offers +- if @listing.offers.any? + - @listing.offers.each do |offer| + .offer{id: dom_id(offer)} + %p= offer.name + %p= offer.email + %p= offer.location + %p #{offer.price} kr + %p= link_to "Accept offer", "#" +- else + .message There are no offers on this listing \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1fd0c3f..bab5ac8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do root controller: :listings, action: :index - resources :listings, only: [:new, :create] + + resources :listings, only: [:new, :show, :create] do + resources :offers + end + end diff --git a/db/migrate/20190509114546_create_offers.rb b/db/migrate/20190509114546_create_offers.rb new file mode 100644 index 0000000..fa06fc4 --- /dev/null +++ b/db/migrate/20190509114546_create_offers.rb @@ -0,0 +1,12 @@ +class CreateOffers < ActiveRecord::Migration[5.2] + def change + create_table :offers do |t| + t.string :name + t.string :email + t.string :location + t.integer :price + + t.timestamps + end + end +end diff --git a/db/migrate/20190509121618_add_listing_to_offers.rb b/db/migrate/20190509121618_add_listing_to_offers.rb new file mode 100644 index 0000000..fef522d --- /dev/null +++ b/db/migrate/20190509121618_add_listing_to_offers.rb @@ -0,0 +1,5 @@ +class AddListingToOffers < ActiveRecord::Migration[5.2] + def change + add_reference :offers, :listing, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 5688d75..bea9d07 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_05_08_150729) do +ActiveRecord::Schema.define(version: 2019_05_09_121618) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,4 +26,16 @@ t.string "pet_picture" end + create_table "offers", force: :cascade do |t| + t.string "name" + t.string "email" + t.string "location" + t.integer "price" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "listing_id" + t.index ["listing_id"], name: "index_offers_on_listing_id" + end + + add_foreign_key "offers", "listings" end diff --git a/features/owner_can_view_offer.feature b/features/owner_can_view_offer.feature new file mode 100644 index 0000000..1ce4158 --- /dev/null +++ b/features/owner_can_view_offer.feature @@ -0,0 +1,33 @@ +Feature: Owner can view offers on Listing page + As a cat owner, + In order to choose host for my cat + I need to be able to view the offers for my listing + + Background: Offers exists on a listing + Given the following listings exist + | pet_name | pet_location | pet_description | start_date | end_date | pet_picture | + | Leif | Gothenburg | I'm nice | 2019-06-28 | 2019-06-29 | picture1 | + | Ace | Stockholm | I'm nice | 2019-06-28 | 2019-06-29 | picture2 | + And the following offers exists on a listing + | name | email | location | price | listing | + | Felix | felix@craft.se | Gothenburg | 100 | Leif | + And I visit the landing page + + Scenario: Owner can click on listing and see correct offers in that listing + When I click "Show listing" within "Leif" section + Then I should be on the "Leif" listing page + And I should see "Leif" + And I should see "picture1" + And I should see "Dates: 2019-06-28 to 2019-06-29" + And I should see "Offers" + And I should see "Felix" + And I should see "felix@craft.se" + And I should see "Gothenburg" + And I should see "100 kr" + And I should see "Accept offer" + And I should not see "There are no offers on this listing" + + Scenario: Owner can click on listing and see "There are no offers for this listing" if that is the case + When I click "Show listing" within "Ace" section + Then I should be on the "Ace" listing page + And I should see "There are no offers on this listing" \ No newline at end of file diff --git a/features/step_definitions/assertion_steps.rb b/features/step_definitions/assertion_steps.rb index 128a3b0..24e02a5 100644 --- a/features/step_definitions/assertion_steps.rb +++ b/features/step_definitions/assertion_steps.rb @@ -2,6 +2,11 @@ expect(page).to have_content content end +Then("I should not see {string}") do |content| + expect(page).not_to have_content content +end + + Then("I should see {string} link") do |link| expect(page).to have_link(link) end @@ -21,3 +26,11 @@ Then("I should be on landing page") do expect(current_path).to eq root_path end + +Then("I should be on the {string} listing page") do |listing_name| + name = Listing.find_by(pet_name: listing_name) + visit listing_path(name) +end + + + diff --git a/features/step_definitions/basic_steps.rb b/features/step_definitions/basic_steps.rb index fe545fd..4ac0939 100644 --- a/features/step_definitions/basic_steps.rb +++ b/features/step_definitions/basic_steps.rb @@ -20,4 +20,12 @@ Then "stop" do binding.pry +end + +When("I click {string} within {string} section") do |link, section| + name = Listing.find_by(pet_name: section) + dom_section = "#listing_#{name.id}" + within(dom_section) do + click_on link + end end \ No newline at end of file diff --git a/features/step_definitions/object_creation_steps.rb b/features/step_definitions/object_creation_steps.rb index 91a52d3..c3218e8 100644 --- a/features/step_definitions/object_creation_steps.rb +++ b/features/step_definitions/object_creation_steps.rb @@ -2,4 +2,13 @@ table.hashes.each do |listing| FactoryBot.create(:listing, listing) end +end + +Given("the following offers exists on a listing") do |table| + table.hashes.each do |offer| + listing_offer = Listing.find_or_create_by(pet_name: offer[:listing]) + FactoryBot.create(:offer, offer + .except('listing') + .merge(listing: listing_offer)) + end end \ No newline at end of file diff --git a/spec/factories/offers.rb b/spec/factories/offers.rb new file mode 100644 index 0000000..74eee26 --- /dev/null +++ b/spec/factories/offers.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :offer do + name { "MyString" } + email { "MyString" } + location { "MyString" } + price { 1 } + listing + end +end diff --git a/spec/models/listing_spec.rb b/spec/models/listing_spec.rb index 795f43e..a9dd9c9 100644 --- a/spec/models/listing_spec.rb +++ b/spec/models/listing_spec.rb @@ -19,6 +19,10 @@ it { is_expected.to validate_presence_of :end_date } it { is_expected.to validate_presence_of :pet_picture } end + + describe 'Associations' do + it { is_expected.to have_many(:offers)} + end describe 'Factory' do it 'should have a valid Factory' do diff --git a/spec/models/offer_spec.rb b/spec/models/offer_spec.rb new file mode 100644 index 0000000..58d0c6d --- /dev/null +++ b/spec/models/offer_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +RSpec.describe Offer, type: :model do + describe 'DB table' do + it { is_expected.to have_db_column :id } + it { is_expected.to have_db_column :name } + it { is_expected.to have_db_column :email } + it { is_expected.to have_db_column :location } + it { is_expected.to have_db_column :price } + end + + describe 'Validations' do + it { is_expected.to validate_presence_of :name } + it { is_expected.to validate_presence_of :email } + it { is_expected.to validate_presence_of :location } + it { is_expected.to validate_presence_of :price } + end + + describe 'Associations' do + it { is_expected.to belong_to(:listing)} + end + + describe 'Factory' do + it 'should have a valid Factory' do + expect(FactoryBot.create(:offer)).to be_valid + end + end +end \ No newline at end of file