Jon Clarke Chitter pull request#208
Conversation
| @@ -0,0 +1,15 @@ | |||
| require 'pg' | |||
|
|
|||
| class DatabaseConnection | |||
There was a problem hiding this comment.
Great idea to split the database connection into a separate class - I hadn't thought to do this
|
|
||
| class << self | ||
| attr_reader :connection | ||
| end |
There was a problem hiding this comment.
How does this class << self function? I haven't seen an attr_reader put inside a method like that before
There was a problem hiding this comment.
This i a bit of metaprogramming.
A mix of: https://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
And the fact that all that
attr_reader :connection really does is:
def connection
@connection
endSo putting it all together, all these lines do is say:
Any time the connection class is used, it has a method called connection that will return the @connection variable value.
There was a problem hiding this comment.
Another way to write this could have been:
def self.connection
@connection
endThere was a problem hiding this comment.
This allows users of the class to use DatabaseConnection.connection
| .chitter-button:active { | ||
| box-shadow: 0 0 0 0 darkred; | ||
| transform: scale(96%); | ||
| } |
There was a problem hiding this comment.
I'm impressed you put together a CSS style sheet! I'd love to see your webpage when we are on zoom. I wanted to give this a go but didn't end up having the time. It's really useful to see how a .css is laid out for my own understanding.
|
|
||
| <ul> | ||
| <% @peeps.reverse.each do |peep| %> | ||
| <li><%= peep.peep %> Posted on <%= peep.date %>.</li> |
There was a problem hiding this comment.
How does this peep.peep work? What is the .peep method?
There was a problem hiding this comment.
@estoltzy this is the message of the peep.
@JonClarke84 - in terms of naming, the word "message" or "content" might be more meaningful.
alicelieutier
left a comment
There was a problem hiding this comment.
Great project overall, I like that you added styling.
I've added some comments, let me know if you have any questions.
| get '/test' do | ||
| 'Test page' | ||
| end |
| get '/' do | ||
| @peeps = Peeps.all | ||
| erb :index | ||
| end | ||
|
|
||
| post '/add' do | ||
| Peeps.create(peep: params[:peep]) | ||
| redirect '/' | ||
| end |
There was a problem hiding this comment.
If you wanted to make this more "standard", you could follow the restful routes naming conventions here: https://medium.com/@shubhangirajagrawal/the-7-restful-routes-a8e84201f206
| @@ -0,0 +1,15 @@ | |||
| require 'pg' | |||
|
|
|||
| class DatabaseConnection | |||
|
|
||
| class << self | ||
| attr_reader :connection | ||
| end |
There was a problem hiding this comment.
This i a bit of metaprogramming.
A mix of: https://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
And the fact that all that
attr_reader :connection really does is:
def connection
@connection
endSo putting it all together, all these lines do is say:
Any time the connection class is used, it has a method called connection that will return the @connection variable value.
|
|
||
| class << self | ||
| attr_reader :connection | ||
| end |
There was a problem hiding this comment.
Another way to write this could have been:
def self.connection
@connection
end|
|
||
| def self.create(peep:) | ||
| result = DatabaseConnection.query( | ||
| "INSERT INTO peeps (message) VALUES ('#{peep}') RETURNING id, message, date;" |
There was a problem hiding this comment.
Check this step walkthrough from the challenge to protect this agains SQL injections - https://github.com/makersacademy/course/blob/main/apprenticeships_bookmark_manager/walkthroughs/12.md
| end | ||
|
|
||
| def self.all | ||
| result = DatabaseConnection.query("SELECT * FROM peeps") |
| add_row_to_test_database | ||
| visit('/') | ||
| expect(page).to have_content "This is a peep! Posted on 2022-04-01." | ||
| end |
There was a problem hiding this comment.
You can add a test for reverse chronological order. Check how you can use capybara matchers to match a regular expression.
| @@ -0,0 +1,43 @@ | |||
| require_relative 'database_connection' | |||
|
|
|||
| class Peeps | |||
There was a problem hiding this comment.
By convention, class names should be singular, table names plural.
|
|
||
| <ul> | ||
| <% @peeps.reverse.each do |peep| %> | ||
| <li><%= peep.peep %> Posted on <%= peep.date %>.</li> |
There was a problem hiding this comment.
@estoltzy this is the message of the peep.
@JonClarke84 - in terms of naming, the word "message" or "content" might be more meaningful.
Your name
Jon Clarke
User stories
Please list which user stories you've implemented (delete the ones that don't apply).
README checklist
Does your README contains instructions for
Here is a pill that can help you write a great README!