-
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
first version #216
base: main
Are you sure you want to change the base?
first version #216
Changes from all commits
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ ruby '3.0.2' | |
|
||
gem 'pg' | ||
gem 'sinatra' | ||
gem 'sinatra-contrib' | ||
gem 'launchy' | ||
|
||
group :test do | ||
gem 'capybara' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<ul> | ||
<% @peeps.each do |peep| %> | ||
<li> | ||
<%= peep.message %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
Please enter a new peep below: | ||
<form action= "/chitter/peeps" method = "post"> | ||
<input type="text" name="message" placeholder="Peep" /> | ||
<input type="submit" value="Submit" /> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
require 'sinatra/base' | ||
require './lib/peeps' | ||
|
||
class Chitter < Sinatra::Base | ||
get '/test' do | ||
'Test page' | ||
|
||
# get '/' do | ||
# redirect '/chitter/peeps' | ||
# end | ||
|
||
get '/chitter/peeps' do | ||
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. It looks like you tried to organise your routes, however the best way to do this is with a system we call REST. For example with your routes as they are now you would presumably have /chitter/ in front of everything making it unhelpful bloat, and peeps is a bit generic and doesn't give any indication of what that route is for. Perhaps something like get '/peeps' for the landing page and then post '/peeps/new' might be a better solution for your other route. This way we know that these routes are for peeps and that one of them is to create a new one. |
||
@peeps = Peeps.all | ||
erb :'chitter/peeps' | ||
end | ||
|
||
post '/chitter/peeps' do | ||
Peeps.create(message: params[:message]) | ||
redirect :'chitter/peeps' | ||
end | ||
|
||
run! if app_file == $0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Peeps | ||
attr_reader :id, :message | ||
|
||
def initialize(id:, message:) | ||
@id = id | ||
@message = message | ||
end | ||
|
||
def self.all | ||
if ENV['ENVIRONMENT'] == 'test' | ||
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 if else is going to be needed in any function you create and it already adds quite a lot of bloat with just two at the moment, could you extract that into a separate function? |
||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
result = connection.exec('SELECT * FROM peeps;') | ||
result.map do |peep| | ||
Peeps.new(id: peep['id'], message: peep['message']) | ||
end | ||
end | ||
|
||
def self.create(message:) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
result = connection.exec_params("INSERT INTO peeps (message) VALUES($1) RETURNING message;", | ||
[message]) | ||
Peeps.new(id: result[0]['id'], message: result[0]['message']) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
feature 'Add a peep' do | ||
scenario 'A user can add a peep' do | ||
visit('/chitter/peeps') | ||
fill_in('message', :with => 'Leigh') | ||
click_button('Submit') | ||
expect(page).to have_content('Leigh') | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
feature 'Viewing peeps' do | ||
scenario 'A user can see peeps' do | ||
Peeps.create(message: "hello") | ||
Peeps.create(message: "it's") | ||
Peeps.create(message: "me") | ||
visit('/chitter/peeps') | ||
expect(page).to have_content("hello") | ||
expect(page).to have_content("it's") | ||
expect(page).to have_content("me") | ||
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.
I would probably try to avoid leaving commented out code in your code base, either you need that redirect or you don't