-
Notifications
You must be signed in to change notification settings - Fork 235
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
Miranda Frudd - chitter challenge (unfinished) #235
base: main
Are you sure you want to change the base?
Changes from 12 commits
2c98eba
6329b1b
96908d2
51209b4
0463a2f
293d014
cb4774f
e1e59a2
e6fe070
fb5ef0a
0c3031e
5254d32
0d6b20f
4e08431
14561ea
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,33 @@ | ||
require 'pg' | ||
|
||
class Message | ||
# def initialize | ||
# @messages = [] | ||
# end | ||
|
||
# def add(message) | ||
# @messages << message | ||
# end | ||
|
||
def self.all | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
result = connection.exec('SELECT * FROM peeps;') | ||
#p result | ||
result.map { |peep| peep['message'] } | ||
# return @messages | ||
end | ||
|
||
def self.create(message:) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
Comment on lines
+15
to
+19
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. You're using this same piece of code in more than one place in this file. How might you get rid of this duplication? |
||
|
||
connection.exec("INSERT INTO peeps (message) VALUES '#{message}' RETURNING id, message;") | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
feature '/' do | ||
scenario 'to have a form' do | ||
visit '/' | ||
fill_in 'Message', with: 'First Message' | ||
expect(page).to have_button('Submit') | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
feature '/' do | ||
scenario 'to submit a form' do | ||
visit '/' | ||
fill_in 'Message', with: 'First Message' | ||
click_on 'Submit' | ||
visit '/messages' | ||
expect(page).to have_content "First Message" | ||
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. This is a good example of a feature test, well done! |
||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
require 'message' | ||
|
||
RSpec.describe '.all' do | ||
context "so that I can see what people are doing" do | ||
it "#all displays all messages" do | ||
connection = PG.connect(dbname: 'chitter_test') | ||
|
||
connection.exec("INSERT INTO peeps (message) VALUES ('First message')") | ||
connection.exec("INSERT INTO peeps (message) VALUES ('Second message')") | ||
|
||
messages = Message.all | ||
|
||
expect(messages).to include "First message" | ||
expect(messages).to include "Second message" | ||
|
||
end | ||
end | ||
end | ||
|
||
# RSpec.describe '.create' do | ||
# context "so that I can create a new message" do | ||
# it "#create adds a new message" do | ||
# message = Message.create(message: 'Another message') | ||
# expect(message.message).to eq "Another message" | ||
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. You were really close! One way to check that 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. Thank you! I think I understand how the class variables can be used to query the database now. I think I was trying to add class objects to the database but that's not right - I needed to gather params from a form and add them to a row of the database table using an SQL query within my class 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. Correct! |
||
# end | ||
# end | ||
# end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<form action="/messages" method="post" > | ||
<label for="message">Message</label> | ||
<input id="message" name="message" type="text" /> | ||
<input name="submit" type="submit" value="Submit" data-disable-with="Search" /> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= @message %> |
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.
It looks like you were thinking of creating the message in your POST route above (the commented out bit), which is good. Where would the right place be to retrieve it the message again so that you can display it to the user? You already have all the methods you need to achieve that in your
Message
class!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.
I should have used Message.all to display the messages from the db table in the get route
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.
Exactly! Nice one.