Skip to content

Commit

Permalink
Add feature to view peep dates
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallwj committed Apr 1, 2022
1 parent 29ca323 commit ea56f75
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions db/migrations/02_add_date_to_peeps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE peeps ADD date DATE;
15 changes: 8 additions & 7 deletions lib/peep.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
require "pg"

class Peep
attr_reader :id, :message
attr_reader :id, :message, :date

def initialize(id:, message:)
def initialize(id:, message:, date:)
@message = message
@id = id
@date = date
end

def self.create(message:)
def self.create(message:, date: Date.today.to_s)
if ENV["RACK_ENV"] == "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 id, message",
[message]
"INSERT into peeps (message, date) VALUES ($1, $2) RETURNING id, message, date",
[message, date]
)
Peep.new(id: result[0]["id"], message: result[0]["message"])
Peep.new(id: result[0]["id"], message: result[0]["message"], date: result[0]["date"])
end

def self.all
Expand All @@ -29,7 +30,7 @@ def self.all
end
result = connection.exec("SELECT * FROM peeps")
result.map do |peep|
Peep.new(id: peep["id"], message: peep["message"])
Peep.new(id: peep["id"], message: peep["message"], date: peep["date"])
end
end
end
11 changes: 11 additions & 0 deletions spec/features/viewing_peep_dates_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
feature "Viewing peep dates" do
scenario "User can see the date a peep was posted" do
first_peep = Peep.create(message: "Test peep 1")
second_peep = Peep.create(message: "Test peep 2", date: "2022-01-01")
visit("/peeps")
expect(page).to have_content("Test peep 1")
expect(page).to have_content(Date.today.to_s)
expect(page).to have_content("Test peep 2")
expect(page).to have_content("2022-01-01")
end
end
4 changes: 2 additions & 2 deletions spec/features/viewing_peeps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
first_peep = Peep.create(message: "Test peep 1")
second_peep = Peep.create(message: "Test peep 2")
visit("/peeps")
expect(page).to have_content(first_peep.message)
expect(page).to have_content(second_peep.message)
expect(page).to have_content("Test peep 1")
expect(page).to have_content("Test peep 2")
end

end
2 changes: 2 additions & 0 deletions spec/peep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
expect(peep).to be_a(Peep)
expect(peep.id).to eq(persisted_data.first["id"])
expect(peep.message).to eq("This is a peep")
expect(peep.date).to eq(Date.today.to_s)
end
end

Expand All @@ -21,6 +22,7 @@
expect(peeps.first).to be_a(Peep)
expect(peeps.first.id).to eq(first_peep.id)
expect(peeps.first.message).to eq("Test peep 1")
expect(peeps.first.date).to eq(Date.today.to_s)
end
end
end
3 changes: 3 additions & 0 deletions views/peeps/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<% @peeps.each do |peep| %>
<li class="peep" id="peep-<%= peep.id %>">
<%= peep.message %>
<br>
[<%= peep.date %>]
<br><br>
</li>
<% end %>
</ul>

0 comments on commit ea56f75

Please sign in to comment.