From 49ed70eb3d742e49d8b74b3f82019e21bea3886a Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Fri, 1 Apr 2022 12:33:27 +0100 Subject: [PATCH 01/11] Show all messages --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app.rb | 15 ++++++++++ lib/peep.rb | 16 +++++++++++ spec/features/view_peeps_spec.rb | 12 ++++++++ views/chitter.erb | 48 ++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 lib/peep.rb create mode 100644 spec/features/view_peeps_spec.rb create mode 100644 views/chitter.erb diff --git a/Gemfile b/Gemfile index 99d8e519..cc0e0cc6 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'webrick' + group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..4d11e03c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,7 @@ GEM unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) unicode-display_width (2.0.0) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -93,6 +94,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + webrick RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb index 2450fb92..9800f156 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,24 @@ require 'sinatra/base' +require './lib/peep' + class Chitter < Sinatra::Base get '/test' do 'Test page' end + get '/' do + 'Chitter' + end + + get '/chitter' do + @peeps = [ + "Hello world!", + "This is a peep", + "Hi Chitter! This is my first peep!" + ] + erb :'chitter' +end + run! if app_file == $0 end diff --git a/lib/peep.rb b/lib/peep.rb new file mode 100644 index 00000000..1a3ef2b9 --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,16 @@ +require 'pg' + +class Peep + 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") + result.map { | peep | + Peep.new(id: peep['peep'], message: peep['message']) + } + end +end \ No newline at end of file diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb new file mode 100644 index 00000000..6cd76d20 --- /dev/null +++ b/spec/features/view_peeps_spec.rb @@ -0,0 +1,12 @@ +require './app' + +RSpec.describe "Peeps" do + feature "view peeps" do + scenario "user can view all peeps in their browser" do + visit('/chitter') + + expect(page).to have_content("Hello world!") + expect(page).to have_content("Hi Chitter! This is my first peep!") + end + end +end \ No newline at end of file diff --git a/views/chitter.erb b/views/chitter.erb new file mode 100644 index 00000000..cf8d33d0 --- /dev/null +++ b/views/chitter.erb @@ -0,0 +1,48 @@ + + + + + + +

Chitter

+ +
+ <% @peeps.each do |peep| %> +
+

<%= peep %>

+
+ <% end %> +
\ No newline at end of file From f0dc210502a572951d77c2aa5776f1dfc94b25bb Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Fri, 1 Apr 2022 12:45:21 +0100 Subject: [PATCH 02/11] Connect to chitter db --- app.rb | 10 +++------- lib/peep.rb | 13 +++---------- spec/peep_spec.rb | 13 +++++++++++++ 3 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 spec/peep_spec.rb diff --git a/app.rb b/app.rb index 9800f156..62f72ef9 100644 --- a/app.rb +++ b/app.rb @@ -12,13 +12,9 @@ class Chitter < Sinatra::Base end get '/chitter' do - @peeps = [ - "Hello world!", - "This is a peep", - "Hi Chitter! This is my first peep!" - ] - erb :'chitter' -end + @peeps = Peep.all + erb :'chitter' + end run! if app_file == $0 end diff --git a/lib/peep.rb b/lib/peep.rb index 1a3ef2b9..9c9250cb 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -2,15 +2,8 @@ class Peep 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") - result.map { | peep | - Peep.new(id: peep['peep'], message: peep['message']) - } + connection = PG.connect(dbname: 'chitter') + result = connection.exec("SELECT * FROM peeps;") + result.map { |peep| peep['message']} end end \ No newline at end of file diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb new file mode 100644 index 00000000..f5bee2d1 --- /dev/null +++ b/spec/peep_spec.rb @@ -0,0 +1,13 @@ +require 'peep' + +describe Peep do + describe '.all' do + it 'returns all peeps' do + peeps = Peep.all + + expect(peeps).to include("Hello world!") + expect(peeps).to include("Hi Chitter! This is my first peep!") + expect(peeps).to include("This is a peep!") + end + end +end \ No newline at end of file From 7948ca8036c7f8569e113fa8484a7ee72962acea Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Fri, 1 Apr 2022 12:58:33 +0100 Subject: [PATCH 03/11] Set up testing environment --- lib/peep.rb | 4 ++++ spec/features/view_peeps_spec.rb | 8 ++++++++ spec/peep_spec.rb | 15 +++++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/peep.rb b/lib/peep.rb index 9c9250cb..f944fe0e 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -2,7 +2,11 @@ class Peep 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;") result.map { |peep| peep['message']} end diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb index 6cd76d20..e0f00b69 100644 --- a/spec/features/view_peeps_spec.rb +++ b/spec/features/view_peeps_spec.rb @@ -1,8 +1,16 @@ require './app' +require 'pg' RSpec.describe "Peeps" do feature "view peeps" do scenario "user can view all peeps in their browser" do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message) VALUES ('Hello world!');") + connection.exec("INSERT INTO peeps (message) VALUES('Hi Chitter! This is my first peep!');") + connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + visit('/chitter') expect(page).to have_content("Hello world!") diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index f5bee2d1..76dcfea4 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -3,11 +3,18 @@ describe Peep do describe '.all' do it 'returns all peeps' do - peeps = Peep.all + connection = PG.connect(dbname: 'chitter_test') - expect(peeps).to include("Hello world!") - expect(peeps).to include("Hi Chitter! This is my first peep!") - expect(peeps).to include("This is a peep!") + # Add the test data + connection.exec("INSERT INTO peeps (message) VALUES ('Hello world!');") + connection.exec("INSERT INTO peeps (message) VALUES('Hi Chitter! This is my first peep!');") + connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + + peeps = Peep.all + + expect(peeps).to include("Hello world!") + expect(peeps).to include("Hi Chitter! This is my first peep!") + expect(peeps).to include("This is a peep!") end end end \ No newline at end of file From 586a24013c189f919e685fd1a752ae7b097c7f48 Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Sat, 2 Apr 2022 22:08:59 +0100 Subject: [PATCH 04/11] Show user own peeps and allow user to post --- app.rb | 21 ++++++- lib/database.rb | 14 +++++ lib/peep.rb | 43 +++++++++++++-- spec/features/post_peeps_spec.rb | 11 ++++ spec/features/view_peeps_spec.rb | 24 +++++++- spec/peep_spec.rb | 29 ++++++++-- views/chitter.erb | 34 +++++++++++- views/my_peeps.erb | 94 ++++++++++++++++++++++++++++++++ views/post_peep.erb | 76 ++++++++++++++++++++++++++ 9 files changed, 330 insertions(+), 16 deletions(-) create mode 100644 lib/database.rb create mode 100644 spec/features/post_peeps_spec.rb create mode 100644 views/my_peeps.erb create mode 100644 views/post_peep.erb diff --git a/app.rb b/app.rb index 62f72ef9..2cef56a2 100644 --- a/app.rb +++ b/app.rb @@ -13,7 +13,26 @@ class Chitter < Sinatra::Base get '/chitter' do @peeps = Peep.all - erb :'chitter' + erb :chitter + end + + get '/my_peeps' do + @peeps = Peep.own_peeps + erb :my_peeps + end + + get '/my_peeps/new_peep' do + erb :post_peep + end + + post '/bookmarks/new' do + @bookmarks_added = Bookmark.add(title: params[:title], url: params[:url]) + redirect '/bookmarks' + end + + post '/my_peeps/create_peep' do + @peeps_posted = Peep.post(message: params[:message], author_id: params[:author_id]) + redirect ('/my_peeps') end run! if app_file == $0 diff --git a/lib/database.rb b/lib/database.rb new file mode 100644 index 00000000..e3952ee0 --- /dev/null +++ b/lib/database.rb @@ -0,0 +1,14 @@ +class Database + + def self.setup(db_name) + @connection = PG.connect :dbname => db_name + end + + def self.current_connection + @connection + end + + def self.query(sql) + @connection.exec(sql) + end +end \ No newline at end of file diff --git a/lib/peep.rb b/lib/peep.rb index f944fe0e..24e7e239 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,13 +1,48 @@ require 'pg' +require './lib/database' class Peep + + attr_reader :id, :message, :author_id + + def initialize(id:, message:, author_id:) + @id = id + @message = message + @author_id = author_id + end + def self.all if ENV['ENVIRONMENT'] == 'test' - connection = PG.connect(dbname: 'chitter_test') + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + result = connection.exec("SELECT * FROM peeps WHERE author_id != 1;") + result.map { |peep| + Peep.new(id: peep['id'], message: peep['message'], author_id: peep['author_id']) + } + end + + def self.own_peeps + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + result = connection.exec("SELECT * FROM peeps WHERE author_id = 1;") + result.map { |peep| + Peep.new(id: peep['id'], message: peep['message'], author_id: peep['author_id']) + } + end + + def self.post(message:, author_id:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') else - connection = PG.connect(dbname: 'chitter') + connection = PG.connect(dbname: 'chitter') end - result = connection.exec("SELECT * FROM peeps;") - result.map { |peep| peep['message']} + result = connection.exec_params( + "INSERT INTO peeps (message, author_id) VALUES($1, $2) RETURNING id, message, author_id;", [message, author_id = 1]) + Peep.new(id: result[0]['id'], message: result[0]['message'], author_id: result[0][author_id]) end end \ No newline at end of file diff --git a/spec/features/post_peeps_spec.rb b/spec/features/post_peeps_spec.rb new file mode 100644 index 00000000..dda43d64 --- /dev/null +++ b/spec/features/post_peeps_spec.rb @@ -0,0 +1,11 @@ +feature 'Posting a peep' do + scenario 'A user can post a peep' do + visit('/chitter') + click_button('My Peeps') + click_button('New Peep') + fill_in('message', with: 'I am posting for the first time!') + click_button('Post') + + expect(page).to have_content 'I am posting for the first time!' + end +end \ No newline at end of file diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb index e0f00b69..4634e056 100644 --- a/spec/features/view_peeps_spec.rb +++ b/spec/features/view_peeps_spec.rb @@ -7,14 +7,32 @@ connection = PG.connect(dbname: 'chitter_test') # Add the test data - connection.exec("INSERT INTO peeps (message) VALUES ('Hello world!');") - connection.exec("INSERT INTO peeps (message) VALUES('Hi Chitter! This is my first peep!');") - connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 2);") visit('/chitter') expect(page).to have_content("Hello world!") expect(page).to have_content("Hi Chitter! This is my first peep!") + expect(page).to have_content("This is a peep!") + end + end + + feature "view my own peeps" do + scenario "user can view their own peeps in their browser" do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Cannot believe it is snowing in April', 1);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Happy Friday everyone!', 1);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Looking forward to the weekend!', 1);") + + visit('/chitter') + click_button('My Peeps') + + expect(page).to have_content("Cannot believe it is snowing in April") + expect(page).to have_content("Looking forward to the weekend!") end end end \ No newline at end of file diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index 76dcfea4..0271a83d 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -6,15 +6,32 @@ connection = PG.connect(dbname: 'chitter_test') # Add the test data - connection.exec("INSERT INTO peeps (message) VALUES ('Hello world!');") - connection.exec("INSERT INTO peeps (message) VALUES('Hi Chitter! This is my first peep!');") - connection.exec("INSERT INTO peeps (message) VALUES('This is a peep!');") + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!',3);") peeps = Peep.all - expect(peeps).to include("Hello world!") - expect(peeps).to include("Hi Chitter! This is my first peep!") - expect(peeps).to include("This is a peep!") + expect(peeps[0].message).to include("Hello world!") + expect(peeps[1].message).to include("Hi Chitter! This is my first peep!") + expect(peeps[2].message).to include("This is a peep!") + end + end + + describe '.own_peeps' do + it 'returns all my peeps' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Can''t believe it''s snowing in April', 1);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Happy Friday everyone!', 1);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Looking forward to the weekend!', 1);") + + peeps = Peep.own_peeps + + expect(peeps[0].message).to include("Can't believe it's snowing in April") + expect(peeps[1].message).to include("Happy Friday everyone!") + expect(peeps[2].message).to include("Looking forward to the weekend!") end end end \ No newline at end of file diff --git a/views/chitter.erb b/views/chitter.erb index cf8d33d0..1633b106 100644 --- a/views/chitter.erb +++ b/views/chitter.erb @@ -12,14 +12,20 @@ font-size: 100px; color: #1A132F; text-align: center; + margin: 5; + padding: 5; } - .container { + .navbar, .container { display: flex; justify-content: center; flex-direction: column; } + .navbar { + margin: 30; + } + .peep { background: rgb(255, 255, 255, 0.7); margin: 20 auto; @@ -35,14 +41,38 @@ text-align: center; } + .my-peeps{ + color: white; + background-color: #1A132F; + border: none; + padding: 20; + font-family: Ubuntu; + font-size: 20px; + border-radius: 10px; + margin: auto; + width: 10%; + display: block; + } + + .my-peeps:hover { + color: white; + background-color: rgb(26, 19, 47, 0.8); + cursor: pointer; + }

Chitter

+ +
<% @peeps.each do |peep| %>
-

<%= peep %>

+

<%= peep.message %>

<% end %>
\ No newline at end of file diff --git a/views/my_peeps.erb b/views/my_peeps.erb new file mode 100644 index 00000000..6adaaaac --- /dev/null +++ b/views/my_peeps.erb @@ -0,0 +1,94 @@ + + + + + + +

Chitter

+ + + +
+

My Peeps

+ +
+ <% @peeps.each do |peep| %> +
+

<%= peep.message %>

+
+ <% end %> +
+
+ diff --git a/views/post_peep.erb b/views/post_peep.erb new file mode 100644 index 00000000..0dba766e --- /dev/null +++ b/views/post_peep.erb @@ -0,0 +1,76 @@ + + + + + + +

Chitter

+ +
+
+ + +
+
+ From d079e50cce3e1f0b0b5036cad386b86e8d5b5a91 Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Sun, 3 Apr 2022 19:28:00 +0100 Subject: [PATCH 05/11] Add timestamp to messages --- app.rb | 5 --- db/migrations/01_create_chitter_table.sql | 6 ++- db/migrations/02_create_author_table.sql | 1 + db/migrations/03_add_timestamp_to_peeps.sql | 2 + lib/peep.rb | 26 ++++++++----- spec/database_helpers.rb | 7 ++++ spec/features/view_peeps_spec.rb | 14 +++---- spec/peep_spec.rb | 42 +++++++++++++-------- views/chitter.erb | 22 ++++++++++- views/my_peeps.erb | 22 ++++++++++- 10 files changed, 108 insertions(+), 39 deletions(-) create mode 100644 db/migrations/02_create_author_table.sql create mode 100644 db/migrations/03_add_timestamp_to_peeps.sql create mode 100644 spec/database_helpers.rb diff --git a/app.rb b/app.rb index 2cef56a2..7132ff0e 100644 --- a/app.rb +++ b/app.rb @@ -25,11 +25,6 @@ class Chitter < Sinatra::Base erb :post_peep end - post '/bookmarks/new' do - @bookmarks_added = Bookmark.add(title: params[:title], url: params[:url]) - redirect '/bookmarks' - end - post '/my_peeps/create_peep' do @peeps_posted = Peep.post(message: params[:message], author_id: params[:author_id]) redirect ('/my_peeps') diff --git a/db/migrations/01_create_chitter_table.sql b/db/migrations/01_create_chitter_table.sql index 6e077248..6a66f512 100644 --- a/db/migrations/01_create_chitter_table.sql +++ b/db/migrations/01_create_chitter_table.sql @@ -1 +1,5 @@ -CREATE TABLE peeps(id SERIAL PRIMARY KEY, message VARCHAR(60)); +CREATE TABLE peeps( + id SERIAL PRIMARY KEY, + message VARCHAR(60), + author_id +); \ No newline at end of file diff --git a/db/migrations/02_create_author_table.sql b/db/migrations/02_create_author_table.sql new file mode 100644 index 00000000..2b35b903 --- /dev/null +++ b/db/migrations/02_create_author_table.sql @@ -0,0 +1 @@ +CREATE TABLE author(id SERIAL PRIMARY KEY, name VARCHAR(60)); \ No newline at end of file diff --git a/db/migrations/03_add_timestamp_to_peeps.sql b/db/migrations/03_add_timestamp_to_peeps.sql new file mode 100644 index 00000000..788070c3 --- /dev/null +++ b/db/migrations/03_add_timestamp_to_peeps.sql @@ -0,0 +1,2 @@ +ALTER TABLE peeps +ADD created_at VARCHAR(60) DEFAULT to_char(NOW(), 'On dd-MM-yyyy at HH24:MI'); \ No newline at end of file diff --git a/lib/peep.rb b/lib/peep.rb index 24e7e239..e88d32db 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -3,12 +3,13 @@ class Peep - attr_reader :id, :message, :author_id + attr_reader :id, :message, :name, :date_time, :created_at - def initialize(id:, message:, author_id:) + def initialize(id:, message:, name:, created_at:) @id = id @message = message - @author_id = author_id + @name = name + @created_at = created_at end def self.all @@ -17,9 +18,12 @@ def self.all else connection = PG.connect(dbname: 'chitter') end - result = connection.exec("SELECT * FROM peeps WHERE author_id != 1;") + result = connection.exec( + "SELECT peeps.id, peeps.message, author.name, peeps.created_at + FROM peeps JOIN author ON peeps.author_id = author.id + WHERE author.id != 1;") result.map { |peep| - Peep.new(id: peep['id'], message: peep['message'], author_id: peep['author_id']) + Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) } end @@ -29,9 +33,12 @@ def self.own_peeps else connection = PG.connect(dbname: 'chitter') end - result = connection.exec("SELECT * FROM peeps WHERE author_id = 1;") + result = connection.exec(" + SELECT peeps.id, peeps.message, author.name, peeps.created_at + FROM peeps JOIN author ON peeps.author_id = author.id + WHERE author_id = 1;") result.map { |peep| - Peep.new(id: peep['id'], message: peep['message'], author_id: peep['author_id']) + Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) } end @@ -42,7 +49,8 @@ def self.post(message:, author_id:) connection = PG.connect(dbname: 'chitter') end result = connection.exec_params( - "INSERT INTO peeps (message, author_id) VALUES($1, $2) RETURNING id, message, author_id;", [message, author_id = 1]) - Peep.new(id: result[0]['id'], message: result[0]['message'], author_id: result[0][author_id]) + "INSERT INTO peeps (message, author_id) + VALUES($1, $2) RETURNING id, message, author_id;", [message, 1]) + Peep.new(id: result[0]['id'], message: result[0]['message'], name: result[0]['name'], created_at: result[0]['created_at']) end end \ No newline at end of file diff --git a/spec/database_helpers.rb b/spec/database_helpers.rb new file mode 100644 index 00000000..2bb1ec76 --- /dev/null +++ b/spec/database_helpers.rb @@ -0,0 +1,7 @@ +require 'pg' + +def persisted_data(id:) + connection = PG.connect(dbname: 'chitter_test') + result = connection.query("SELECT * FROM peeps WHERE id = #{id};") + result.first +end \ No newline at end of file diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb index 4634e056..11fe05fa 100644 --- a/spec/features/view_peeps_spec.rb +++ b/spec/features/view_peeps_spec.rb @@ -4,6 +4,7 @@ RSpec.describe "Peeps" do feature "view peeps" do scenario "user can view all peeps in their browser" do + connection = PG.connect(dbname: 'chitter_test') # Add the test data @@ -16,23 +17,22 @@ expect(page).to have_content("Hello world!") expect(page).to have_content("Hi Chitter! This is my first peep!") expect(page).to have_content("This is a peep!") + expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") end end feature "view my own peeps" do scenario "user can view their own peeps in their browser" do - connection = PG.connect(dbname: 'chitter_test') - - # Add the test data - connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Cannot believe it is snowing in April', 1);") - connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Happy Friday everyone!', 1);") - connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Looking forward to the weekend!', 1);") - + Peep.post(message: "Cannot believe it is snowing in April", author_id: 1) + Peep.post(message: "Happy Friday everyone!", author_id: 1) + Peep.post(message: "Looking forward to the weekend!", author_id: 1) + visit('/chitter') click_button('My Peeps') expect(page).to have_content("Cannot believe it is snowing in April") expect(page).to have_content("Looking forward to the weekend!") + expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") end end end \ No newline at end of file diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index 0271a83d..e4c85ae2 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -1,4 +1,5 @@ require 'peep' +require 'database_helpers' describe Peep do describe '.all' do @@ -8,30 +9,41 @@ # Add the test data connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") - connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!',3);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 3);") peeps = Peep.all - expect(peeps[0].message).to include("Hello world!") - expect(peeps[1].message).to include("Hi Chitter! This is my first peep!") - expect(peeps[2].message).to include("This is a peep!") + expect(peeps.length).to eq 3 + expect(peeps.first).to be_a Peep + expect(peeps.first.message).to eq 'Hello world!' + expect(peeps.first.name).to eq 'Joe Blogg' + expect(peeps.first.created_at).to eq Time.now.strftime("On %d-%m-%Y at %H:%M") end end describe '.own_peeps' do - it 'returns all my peeps' do - connection = PG.connect(dbname: 'chitter_test') + it 'posts a peep and returns all my peeps' do + connection = PG.connect(dbname: 'chitter_test') + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Can''t believe it''s snowing in April!', 1);") - # Add the test data - connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Can''t believe it''s snowing in April', 1);") - connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Happy Friday everyone!', 1);") - connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Looking forward to the weekend!', 1);") + peeps = Peep.own_peeps - peeps = Peep.own_peeps + expect(peeps.first).to be_a Peep + expect(peeps.first.message).to eq 'Can\'t believe it\'s snowing in April!' + expect(peeps.first.name).to eq 'Laura Cab' + expect(peeps.first.created_at).to eq Time.now.strftime("On %d-%m-%Y at %H:%M") + end + end - expect(peeps[0].message).to include("Can't believe it's snowing in April") - expect(peeps[1].message).to include("Happy Friday everyone!") - expect(peeps[2].message).to include("Looking forward to the weekend!") + describe '.post' do + it 'creates a new peep' do + peep = Peep.post(message: 'Oh! It\'s raining today! Where is my umbrella?', author_id: 1) + persisted_data = persisted_data(id: peep.id) + + expect(peep).to be_a Peep + expect(peep.message).to eq 'Oh! It\'s raining today! Where is my umbrella?' end end -end \ No newline at end of file +end + + \ No newline at end of file diff --git a/views/chitter.erb b/views/chitter.erb index 1633b106..db0a5eda 100644 --- a/views/chitter.erb +++ b/views/chitter.erb @@ -35,10 +35,26 @@ display: block; } - h3 { + h3, h4 { font-family: Ubuntu; + } + + h3 { font-size: 20px; text-align: center; + margin: 5 5 20 5; + } + + h4 { + font-size: 14px; + text-align: left; + margin: 5 5 0 5; + display: inline; + } + + .header { + display: flex; + justify-content: space-between; } .my-peeps{ @@ -72,6 +88,10 @@
<% @peeps.each do |peep| %>
+
+

<%= peep.name %> wrote:

+

<%= peep.created_at %>

+

<%= peep.message %>

<% end %> diff --git a/views/my_peeps.erb b/views/my_peeps.erb index 6adaaaac..8117b892 100644 --- a/views/my_peeps.erb +++ b/views/my_peeps.erb @@ -42,10 +42,26 @@ font-weight:800; } - h3 { + h3, h4 { font-family: Ubuntu; + } + + h3 { font-size: 20px; text-align: center; + margin: 5 5 20 5; + } + + h4 { + font-size: 14px; + text-align: left; + margin: 5 5 0 5; + display: inline; + } + + .header { + display: flex; + justify-content: space-between; } .new-peep{ @@ -86,6 +102,10 @@
<% @peeps.each do |peep| %>
+
+

<%= peep.name %> wrote:

+

<%= peep.created_at %>

+

<%= peep.message %>

<% end %> From 5e3156cbb4544d4803dc75fbfa29c418c3a1a471 Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Sun, 3 Apr 2022 19:48:39 +0100 Subject: [PATCH 06/11] Display peeps in reverse chronological order --- lib/peep.rb | 6 ++++-- spec/features/view_peeps_spec.rb | 20 ++++++++++++++++++++ views/my_peeps.erb | 20 +++++++++++++++----- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/peep.rb b/lib/peep.rb index e88d32db..854b34ef 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -21,7 +21,8 @@ def self.all result = connection.exec( "SELECT peeps.id, peeps.message, author.name, peeps.created_at FROM peeps JOIN author ON peeps.author_id = author.id - WHERE author.id != 1;") + WHERE author.id != 1 + ORDER BY peeps.created_at DESC;") result.map { |peep| Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) } @@ -36,7 +37,8 @@ def self.own_peeps result = connection.exec(" SELECT peeps.id, peeps.message, author.name, peeps.created_at FROM peeps JOIN author ON peeps.author_id = author.id - WHERE author_id = 1;") + WHERE author_id = 1 + ORDER BY peeps.created_at DESC;") result.map { |peep| Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) } diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb index 11fe05fa..196f216f 100644 --- a/spec/features/view_peeps_spec.rb +++ b/spec/features/view_peeps_spec.rb @@ -35,4 +35,24 @@ expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") end end + + feature "back to all peeps from my peeps" do + scenario "user can go back to all peeps from their peeps" do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 2);") + + visit('/chitter') + click_button('My Peeps') + click_button('All Peeps') + + expect(page).to have_content("Hello world!") + expect(page).to have_content("Hi Chitter! This is my first peep!") + expect(page).to have_content("This is a peep!") + expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") + end + end end \ No newline at end of file diff --git a/views/my_peeps.erb b/views/my_peeps.erb index 8117b892..3559785d 100644 --- a/views/my_peeps.erb +++ b/views/my_peeps.erb @@ -18,12 +18,20 @@ .navbar, .container { display: flex; - justify-content: center; + } + + .container { flex-direction: column; + justify-content: center; } .navbar { margin: 30; + justify-content: center; + } + + form { + margin: 20; } .peep { @@ -64,7 +72,7 @@ justify-content: space-between; } - .new-peep{ + .new-peep, .all-peeps{ color: white; background-color: #1A132F; border: none; @@ -73,11 +81,10 @@ font-size: 20px; border-radius: 10px; margin: auto; - width: 10%; - display: block; + width: 100%; } - .new-peep:hover { + .new-peep:hover, .all-peeps:hover { color: white; background-color: rgb(26, 19, 47, 0.8); cursor: pointer; @@ -94,6 +101,9 @@
+
+ +
From 333f4b39d3668bc20eacd8198178cfbce36765ee Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Mon, 4 Apr 2022 09:10:21 +0100 Subject: [PATCH 07/11] Attempt to filter peeps by keyword --- README.md | 50 +++++----- app.rb | 8 ++ db/migrations/04_insert_data_to_peeps.sql | 12 +++ db/migrations/05_insert_data_to_author.sql | 6 ++ lib/peep.rb | 17 ++++ spec/features/search_peeps_spec.rb | 18 ++++ views/chitter.erb | 11 +++ views/filtered_results.erb | 108 +++++++++++++++++++++ 8 files changed, 204 insertions(+), 26 deletions(-) create mode 100644 db/migrations/04_insert_data_to_peeps.sql create mode 100644 db/migrations/05_insert_data_to_author.sql create mode 100644 spec/features/search_peeps_spec.rb create mode 100644 views/filtered_results.erb diff --git a/README.md b/README.md index f9638b66..0a93a7f1 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,34 @@ -## Chitter Challenge - -* Challenge time: until the end of the day -* Feel free to use google, your notes, books etc but please work on your own -* Please raise a pull request when you start this challenge, and keep pushing updates as and when you make commits throughout the day -* There is _no expectation_ to finish all or any of the user stories, please use this time to reflect on where you feel you are with the skill and what may support your learning. -* If you get blocked, please reflect on what blocked you and any strategies you adopted that helped you make progress. +## CHITTER CHALLENGE We are going to write a small Twitter clone that will allow the users to post messages to a public stream. -## Set up +## SETTING UP THE DATABASE -To setup the database: +1. Connect to `psql` (you may need to install postgresql first by running `brew install postgres`) +2. Create the database using the psql command `CREATE DATABASE chitter` +3. Connect to the database using the pqsl command `\c chitter` +4. Run the query we have saved in the file **01_create_chitter_table.sql** +5. Run the query we have saved in the file **02_create_author_table.sql** +6. Run the query we have saved in the file **03_add_timestamp_to_peeps.sql** +7. Populate your table by running the queries saved in the file **04_insert_data_to_peeps.sql** and **05_insert_data_to_author.sql** -* Connect to psql -* Create the database using the psql command `CREATE DATABASE chitter;` -* Connect to the database using the psql command `\c chitter`; -* Run the query we have saved in the file 01_create_chitter_table.sql -* Populate your table with a row by running `INSERT INTO peeps (message) values ('This is a peep!');` +To check you have everything set up ok, please take a look at the peeps table and the author inside the chitter database. -To check you have everything set up ok, please take a look at the peeps table inside the chitter database. You should see one row in there. +## SETTING UP THE TEST DATABASE -To setup the test database: -* Connect to psql -* Create the database using the psql -command `CREATE DATABASE chitter_test;`; -* Connect to the database using the psql command `\c chitter_test` -* Run the query we have saved in the file 01_create_chitter_table.sql +1. Connect to `psql` +2. Create the database using the psql command `CREATE DATABASE chitter_test` +3. Connect to the database using the pqsl command `\c chitter_test` +4. Run the query we have saved in the file **01_create_chitter_table.sql** +5. Run the query we have saved in the file **02_create_author_table.sql** +6. Run the query we have saved in the file **03_add_timestamp_to_peeps.sql** -* `bundle install` -* `rspec` +## START THE APP -You should see 1 passing test. +- Run `bundle install` to add predefined package dependencies +- Run `rackup` and run the app on localhost 9292 -## User stories +## USER STORIES ``` As a Maker @@ -43,7 +39,7 @@ in a browser ``` As a Maker -So that I can let people know what I am doing +So that I can let people know what I am doing I want to post a message (peep) to chitter ``` @@ -52,6 +48,7 @@ As a Maker So that I can see when people are doing things I want to see the date the message was posted ``` + (Hint the database table will need to change to store the date too) ``` @@ -59,6 +56,7 @@ As a Maker So that I can easily see the latest peeps I want to see a list of peeps in reverse chronological order ``` + ``` As a Maker So that I can find relevant peeps diff --git a/app.rb b/app.rb index 7132ff0e..236c76c3 100644 --- a/app.rb +++ b/app.rb @@ -3,6 +3,7 @@ class Chitter < Sinatra::Base + get '/test' do 'Test page' end @@ -30,5 +31,12 @@ class Chitter < Sinatra::Base redirect ('/my_peeps') end + post '/chitter/search' do + @keyword = params[:keyword] + p @keyword #Just to check if keyword comes through + @peeps_filtered = Peep.search(keyword: params[:keyword]) + erb :filtered_results + end + run! if app_file == $0 end diff --git a/db/migrations/04_insert_data_to_peeps.sql b/db/migrations/04_insert_data_to_peeps.sql new file mode 100644 index 00000000..898836f4 --- /dev/null +++ b/db/migrations/04_insert_data_to_peeps.sql @@ -0,0 +1,12 @@ +INSERT INTO peeps (message, author_id) +VALUES ('This is a peep', 2); +INSERT INTO peeps (message, author_id) +VALUES ('Hello world!', 1); +INSERT INTO peeps (message, author_id) +VALUES ('What a beautiful day today!', 1); +INSERT INTO peeps (message, author_id) +VALUES ('This Chitter thing is great you know!', 2); +INSERT INTO peeps (message, author_id) +VALUES ('This is a peep', 3); +INSERT INTO peeps (message, author_id) +VALUES ('This is another peep', 2); \ No newline at end of file diff --git a/db/migrations/05_insert_data_to_author.sql b/db/migrations/05_insert_data_to_author.sql new file mode 100644 index 00000000..366ef4c2 --- /dev/null +++ b/db/migrations/05_insert_data_to_author.sql @@ -0,0 +1,6 @@ +INSERT INTO author (id, name) +VALUES (1, 'Me'); +INSERT INTO author (id, name) +VALUES (2, 'The Real Donald Trump'); +INSERT INTO author (id, name) +VALUES (3, 'Joe Blogg'); \ No newline at end of file diff --git a/lib/peep.rb b/lib/peep.rb index 854b34ef..15059b69 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -55,4 +55,21 @@ def self.post(message:, author_id:) VALUES($1, $2) RETURNING id, message, author_id;", [message, 1]) Peep.new(id: result[0]['id'], message: result[0]['message'], name: result[0]['name'], created_at: result[0]['created_at']) end + + + def self.search(keyword:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + result = connection.exec( + "SELECT peeps.id, peeps.message, author.name, peeps.created_at + FROM peeps JOIN author ON peeps.author_id = author.id + WHERE author.id != 1 AND peeps.message LIKE '%#{keyword}%' + ORDER BY peeps.created_at DESC;") + result.map { |peep| + Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) + } + end end \ No newline at end of file diff --git a/spec/features/search_peeps_spec.rb b/spec/features/search_peeps_spec.rb new file mode 100644 index 00000000..eb1f0389 --- /dev/null +++ b/spec/features/search_peeps_spec.rb @@ -0,0 +1,18 @@ +feature "filter peeps" do + scenario "A user can search peeps by keywords" do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") + connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 2);") + + visit('/chitter') + fill_in('filter', with: 'peep') + click_button('Filter') + + expect(page).not_to have_content("Hello world!") + expect(page).to have_content("Hi Chitter! This is my first peep!") + expect(page).to have_content("This is a peep!") + end +end \ No newline at end of file diff --git a/views/chitter.erb b/views/chitter.erb index db0a5eda..9ce1bb56 100644 --- a/views/chitter.erb +++ b/views/chitter.erb @@ -26,6 +26,11 @@ margin: 30; } + .search { + display: flex; + justify-content: center; + } + .peep { background: rgb(255, 255, 255, 0.7); margin: 20 auto; @@ -86,6 +91,12 @@
+ <% @peeps.each do |peep| %>
diff --git a/views/filtered_results.erb b/views/filtered_results.erb new file mode 100644 index 00000000..b368c92d --- /dev/null +++ b/views/filtered_results.erb @@ -0,0 +1,108 @@ + + + + + + +

Chitter

+ +
+

Results including "<%= @keyword %>"

+ <% @peeps_filtered.each do |peep| %> +
+
+

<%= peep.name %> wrote:

+

<%= peep.created_at %>

+
+

<%= peep.message %>

+
+ <% end %> +
\ No newline at end of file From 416f8b42a5a767093e4ff63b7932c7662f3d1c84 Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Mon, 4 Apr 2022 09:18:48 +0100 Subject: [PATCH 08/11] Style the keyword search box and button --- views/chitter.erb | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/views/chitter.erb b/views/chitter.erb index 9ce1bb56..f2676f78 100644 --- a/views/chitter.erb +++ b/views/chitter.erb @@ -62,20 +62,41 @@ justify-content: space-between; } - .my-peeps{ + .my-peeps, .Filter{ color: white; background-color: #1A132F; border: none; - padding: 20; font-family: Ubuntu; - font-size: 20px; - border-radius: 10px; margin: auto; - width: 10%; + } + + .my-peeps { + padding: 20; display: block; + font-size: 20px; + width: 10%; + border-radius: 10px; + } + + .filter { + padding: 10; + display: inline; + font-size: 14px; + width: 25%; + border-radius: 4px; + } + + .filter-box { + width: 70%; + padding: 12px 20px; + margin: 8px 0; + display: inline; + border: 1px solid #ccc; + border-radius: 4px; + box-sizing: border-box; } - .my-peeps:hover { + .my-peeps:hover, .filter:hover { color: white; background-color: rgb(26, 19, 47, 0.8); cursor: pointer; @@ -93,8 +114,8 @@
<% @peeps.each do |peep| %> From 780a3c5c7882ab4ba785f76563e47621febb0038 Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Mon, 4 Apr 2022 09:28:05 +0100 Subject: [PATCH 09/11] Update Readme.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0a93a7f1..91fac105 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ To check you have everything set up ok, please take a look at the peeps table an - Run `bundle install` to add predefined package dependencies - Run `rackup` and run the app on localhost 9292 +- Run `rspec` to test code ## USER STORIES From 4b81357a88bd0794aac19adc10a409ccc7ad4c6d Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Mon, 4 Apr 2022 11:03:25 +0100 Subject: [PATCH 10/11] Correct filter feature --- app.rb | 6 +++--- lib/peep.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app.rb b/app.rb index 236c76c3..1aa03b00 100644 --- a/app.rb +++ b/app.rb @@ -32,9 +32,9 @@ class Chitter < Sinatra::Base end post '/chitter/search' do - @keyword = params[:keyword] - p @keyword #Just to check if keyword comes through - @peeps_filtered = Peep.search(keyword: params[:keyword]) + @keyword = params[:filter] + p params #Just to check if keyword comes through + @peeps_filtered = Peep.search(keyword: params[:filter]) erb :filtered_results end diff --git a/lib/peep.rb b/lib/peep.rb index 15059b69..b99308e7 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -3,7 +3,7 @@ class Peep - attr_reader :id, :message, :name, :date_time, :created_at + attr_reader :id, :message, :name, :created_at def initialize(id:, message:, name:, created_at:) @id = id From c5b84d3a4ad4997bcc744c4fc02aac9af0188be0 Mon Sep 17 00:00:00 2001 From: Laura Cabantous Date: Wed, 20 Apr 2022 21:11:48 +0100 Subject: [PATCH 11/11] Tidy up code as per review --- app.rb | 5 ----- lib/database.rb | 4 ++-- lib/peep.rb | 40 ++++++++++++++++------------------------ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/app.rb b/app.rb index 1aa03b00..0c5022f4 100644 --- a/app.rb +++ b/app.rb @@ -4,10 +4,6 @@ class Chitter < Sinatra::Base - get '/test' do - 'Test page' - end - get '/' do 'Chitter' end @@ -33,7 +29,6 @@ class Chitter < Sinatra::Base post '/chitter/search' do @keyword = params[:filter] - p params #Just to check if keyword comes through @peeps_filtered = Peep.search(keyword: params[:filter]) erb :filtered_results end diff --git a/lib/database.rb b/lib/database.rb index e3952ee0..4ac8d010 100644 --- a/lib/database.rb +++ b/lib/database.rb @@ -1,10 +1,10 @@ class Database - def self.setup(db_name) + def self.setup(db_name) @connection = PG.connect :dbname => db_name end - def self.current_connection + def self.current_connection @connection end diff --git a/lib/peep.rb b/lib/peep.rb index b99308e7..8c53e62f 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -13,11 +13,7 @@ def initialize(id:, message:, name:, created_at:) end def self.all - if ENV['ENVIRONMENT'] == 'test' - connection = PG.connect(dbname: 'chitter_test') - else - connection = PG.connect(dbname: 'chitter') - end + connection = select_db result = connection.exec( "SELECT peeps.id, peeps.message, author.name, peeps.created_at FROM peeps JOIN author ON peeps.author_id = author.id @@ -29,11 +25,7 @@ def self.all end def self.own_peeps - if ENV['ENVIRONMENT'] == 'test' - connection = PG.connect(dbname: 'chitter_test') - else - connection = PG.connect(dbname: 'chitter') - end + connection = select_db result = connection.exec(" SELECT peeps.id, peeps.message, author.name, peeps.created_at FROM peeps JOIN author ON peeps.author_id = author.id @@ -45,24 +37,16 @@ def self.own_peeps end def self.post(message:, author_id:) - 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, author_id) - VALUES($1, $2) RETURNING id, message, author_id;", [message, 1]) - Peep.new(id: result[0]['id'], message: result[0]['message'], name: result[0]['name'], created_at: result[0]['created_at']) + connection = select_db + result = connection.exec_params( + "INSERT INTO peeps (message, author_id) + VALUES($1, $2) RETURNING id, message, author_id;", [message, 1]) + Peep.new(id: result[0]['id'], message: result[0]['message'], name: result[0]['name'], created_at: result[0]['created_at']) end def self.search(keyword:) - if ENV['ENVIRONMENT'] == 'test' - connection = PG.connect(dbname: 'chitter_test') - else - connection = PG.connect(dbname: 'chitter') - end + connection = select_db result = connection.exec( "SELECT peeps.id, peeps.message, author.name, peeps.created_at FROM peeps JOIN author ON peeps.author_id = author.id @@ -72,4 +56,12 @@ def self.search(keyword:) Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) } end + + def self.select_db + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + end end \ No newline at end of file