Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host can make offer on listing #15

Open
wants to merge 21 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
22f9e3b
creates feature test for host_can_make_offer_on_listing, adds button …
Carrosen May 9, 2019
25be6a9
Creates model Offer. Creates controller Offers.
stefankarlberg May 9, 2019
1f9a218
adds column to db table, Listing references to model offer
Carrosen May 9, 2019
a408eeb
Added path to new offer in listings. Trying to make tests pass. Now g…
stefankarlberg May 9, 2019
237efde
adds params to controller, tries to fix error, WIP
Carrosen May 9, 2019
d7eb10c
Created new partial - form for offer. Adds required params in Offer c…
stefankarlberg May 9, 2019
2f57db6
adds validations of db columns to offer spec, and to validate presenc…
Carrosen May 9, 2019
33456e4
Adds new view, listings/show, new path in listings/index on button. N…
stefankarlberg May 10, 2019
6bc6f76
adds form for offer to show view, changes step definitions to more ac…
Carrosen May 10, 2019
99b9f4a
Added new assertion step, 6/10 in Scenario going green.
stefankarlberg May 10, 2019
00218f3
changes test in feature to match label on offer form in show view
Carrosen May 10, 2019
2c5b6b3
Refrased feature test in host_can_make_offer_on_listing 8/10 go green
stefankarlberg May 10, 2019
bd91206
changes test I should be on landing page to correct syntax to match s…
Carrosen May 10, 2019
98f66ce
Added new Scenario in feature test for sad path, in case fields are n…
stefankarlberg May 10, 2019
03cab9a
adds if statement to create method in offers controller, adds error m…
Carrosen May 10, 2019
0fc9693
Changed syntax in create method, added listing in Factory bot
stefankarlberg May 10, 2019
052d9fc
deletes unnessecery partial form and folder for offer in view.
stefankarlberg May 10, 2019
793351c
adds associations in specs between the offers and listing, deletes ir…
Carrosen May 10, 2019
1458b1c
changes assertion step to expect page to be listing_path
Carrosen May 10, 2019
24bec19
took away instance variable in method for new, changed naming in feat…
Carrosen May 10, 2019
92fb521
solves merging conflicts, adds user to host_can_make_offer_on_listing…
Carrosen May 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/listings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def new
@listing = Listing.new
end

def show
@listing = Listing.find(params[:id])
end

def create
listing = Listing.create(listing_params)

Expand Down
26 changes: 26 additions & 0 deletions app/controllers/offers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class OffersController < ApplicationController
def index
@offers = Offer.all
end

def new
offer = Offer.new(offer_params)
end

def create
@listing = Listing.find(params[:listing_id])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to use an instance variable here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt work without

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you try?

@offer = @listing.offers.create(offer_params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to use an instance variable here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt work without

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain what is the other option you tried @Carrosen


if @offer.persisted?
redirect_to root_path
else
redirect_to listing_path(@listing), notice: "Please fill in all fields correctly"
end
end


private
def offer_params
params.require(:offer).permit(:name, :email, :location, :price)
end
end
3 changes: 2 additions & 1 deletion app/models/listing.rb
Original file line number Diff line number Diff line change
@@ -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
end
4 changes: 4 additions & 0 deletions app/models/offer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Offer < ApplicationRecord
belongs_to :listing
oliverochman marked this conversation as resolved.
Show resolved Hide resolved
validates_presence_of :name, :email, :location, :price
end
1 change: 1 addition & 0 deletions app/views/listings/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
%p= listing.start_date
%p= listing.end_date
%p= listing.pet_picture
= link_to 'Make an offer', listing_path(listing)
26 changes: 26 additions & 0 deletions app/views/listings/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%h1= @listing.pet_name
%[email protected]_location
%[email protected]_description
%p Dates: #{@listing.start_date} to #{@listing.end_date}
%[email protected]_picture

%h2 Create your offer
= form_with(model: [ @listing, @listing.offers.build ], local: true) do |form|
%p
= form.label :name
%br/
= form.text_field :name
%p
= form.label :email
%br/
= form.text_field :email
%p
= form.label :location
%br/
= form.text_area :location
%p
= form.label :price
%br/
= form.number_field :price
%p
= form.submit "Create offer"
9 changes: 4 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
devise_for :users

root controller: :listings, action: :index

resources :listings, only: [:new, :create]

resources :profiles, only: [:new, :create]

end
resources :listings, only: [:new, :show, :create] do
resources :offers
end
end
12 changes: 12 additions & 0 deletions db/migrate/20190509113524_create_offers.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20190509121804_add_listing_to_offers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddListingToOffers < ActiveRecord::Migration[5.2]
def change
add_reference :offers, :listing, foreign_key: true
end
end
12 changes: 12 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
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

create_table "profiles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand All @@ -49,5 +60,6 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "offers", "listings"
add_foreign_key "profiles", "users"
end
38 changes: 38 additions & 0 deletions features/host_can_make_offer_on_listing.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Feature: Host can make an offer on a listing
As a host,
In order to make some money,
I want to be able to make an offer on a listing

Background:
Given the following listings exist
| pet_name | pet_location | pet_description | start_date | end_date | pet_picture |
| Zane | Gothenburg | I'm nice | 2019-06-28 | 2019-06-29 | picture1 |
| Carla | Stockholm | I love cats! | 2019-05-28 | 2019-06-01 | picture2 |

Given the following user exist
| email | password |
| [email protected] | pswrd12345 |

Given I am logged in as "[email protected]"
When I visit the landing page

Scenario: Host can successfully create an offer
When I click "Make an offer" within "Zane" section
Then I should be on the "Zane" listing page
And I should see "Create your offer"
When I fill in "Name" with "Steffe"
And I fill in "Email" with "[email protected]"
And I fill in "Location" with "Gothenburg"
And I fill in "Price" with "100kr"
When I click "Create offer" button
Then I should be on landing page

Scenario: Host can not create an offer when not all the fields are filled in.
When I click "Make an offer" within "Zane" section
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Then I should be on the "Zane" listing page
And I should see "Create your offer"
And I fill in "Name" with "Steffe"
And I fill in "Location" with "Gothenburg"
And I fill in "Price" with "100kr"
When I click "Create offer" button
Then I should see "Please fill in all fields correctly"
6 changes: 6 additions & 0 deletions features/step_definitions/assertion_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@
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)
expect(current_path).to eq listing_path(name)
end

Then("I should be on Create profile page") do
expect(current_path).to eq new_profile_path
end


Then("I should be on the Log in page") do
expect(current_path).to eq new_user_session_path
end
Expand Down
15 changes: 15 additions & 0 deletions features/step_definitions/basic_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
click_button button
end

When("I click on {string} within {string} section") do |content, section|
name = Listing.find_by(pet_name: section)
dom_section = "#listing_#{name.id}"
within(dom_section) do
expect(page).to have_content content
end
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
Then "stop" do
binding.pry
end
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/offers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :offer do
name { "MyString" }
email { "MyString" }
location { "MyString" }
price { 1 }
listing
end
end
4 changes: 4 additions & 0 deletions spec/models/listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
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
expect(FactoryBot.create(:listing)).to be_valid
Expand Down
28 changes: 28 additions & 0 deletions spec/models/offer_spec.rb
Original file line number Diff line number Diff line change
@@ -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

oliverochman marked this conversation as resolved.
Show resolved Hide resolved
describe 'Factory' do
it 'should have a valid Factory' do
expect(FactoryBot.create(:offer)).to be_valid
end
end
end