-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: development
Are you sure you want to change the base?
Changes from all commits
22f9e3b
25be6a9
1f9a218
a408eeb
237efde
d7eb10c
2f57db6
33456e4
6bc6f76
99b9f4a
00218f3
2c5b6b3
bd91206
98f66ce
03cab9a
0fc9693
052d9fc
793351c
1458b1c
24bec19
92fb521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]) | ||
@offer = @listing.offers.create(offer_params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need to use an instance variable here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didnt work without There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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 |
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" |
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 |
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
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 |
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didnt work without
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What did you try?