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

Miranda Frudd - chitter challenge (unfinished) #235

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ GEM
mini_mime (1.1.1)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3-arm64-darwin)
racc (~> 1.4)
nokogiri (1.12.3-x86_64-darwin)
racc (~> 1.4)
parallel (1.20.1)
Expand Down Expand Up @@ -83,6 +85,7 @@ GEM
nokogiri (~> 1.8)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
Expand Down
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ command `CREATE DATABASE chitter_test;`;
* `bundle install`
* `rspec`

You should see 1 passing test.
You should see 3 passing tests.

## Run

run use ruby app.rb

rackup is not working - currently unsure why

## User stories

Expand Down Expand Up @@ -64,3 +70,51 @@ As a Maker
So that I can find relevant peeps
I want to filter on a specific keyword
```


# FIRST USER STORY

```
As a Maker
So that I can see what people are doing
I want to see all the messages (peeps)
in a browser
```

class Message
def initialize # name is a string
@messages = []
end

def add(message)
# adds the message to @messages
end

def self.all
# Returns @messages
end
end

# TESTS

# 1
RSpec.describe Message do
context "so that I can see what people are doing" do
it "#all displays all messages do
message1 = Message.new
message2 = Message.new
message1.add("First message")
message2.add("Second message")
expect(Message.all).to eq ["First message", "Second message"]
end
end
end

# 2 -> feature test

feature 'messages' do
scenario 'to have a messages on it' do
visit '/messages'
expect(page).to have_content "First message", "Second message"
end
end
18 changes: 16 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
require 'sinatra/base'

class Chitter < Sinatra::Base
get '/test' do
'Test page'

enable :sessions

get '/' do
erb :index
end

post '/messages' do
# Message.create(message: params[:message])
Copy link
Contributor

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!

Copy link
Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly! Nice one.

session[:message] = params[:message]
redirect '/messages'
end

get '/messages' do
@message = session[:message]
erb :messages
end

run! if app_file == $0
Expand Down
33 changes: 33 additions & 0 deletions lib/message.rb
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
7 changes: 7 additions & 0 deletions spec/features/homepage_spec.rb
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
9 changes: 9 additions & 0 deletions spec/features/messages_page_spec.rb
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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

28 changes: 28 additions & 0 deletions spec/message_spec.rb
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"
Copy link
Contributor

Choose a reason for hiding this comment

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

You were really close! One way to check that create has really added the message to the database would be to call Message.all after calling create and check that your message is now part of the result set.

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct!

# end
# end
# end
5 changes: 5 additions & 0 deletions views/index.erb
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>
1 change: 1 addition & 0 deletions views/messages.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @message %>