Skip to content

Commit

Permalink
Merge pull request #1053 from alphagov/add-extra-info-to-csv-download
Browse files Browse the repository at this point in the history
Add extra fields to Queries CSV download
  • Loading branch information
leenagupte authored Nov 16, 2023
2 parents 9cd1ebc + a76a2da commit 7b3f5f3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
8 changes: 8 additions & 0 deletions app/models/bet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ def is_query?
def query_object
query
end

def last_edit_date
updated_at || created_at
end

def author
user.try(:name) || " "
end
end
8 changes: 6 additions & 2 deletions app/models/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ def query_object

def self.to_csv(*_args)
CSV.generate do |csv|
csv << ["query", "match_type", "link", "best/worst", "comment"]
csv << ["query", "match_type", "link", "best/worst", "comment", "status", "position", "last edit date", "author"]

all.includes(:bets).find_each do |query|
query.bets.each do |bet|
csv << [query.query,
query.match_type,
bet.link,
bet.is_best ? "best" : "worst",
bet.comment.to_s]
bet.comment.to_s,
bet.valid_until,
bet.position,
bet.last_edit_date.strftime("%d %b %Y"),
bet.author]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/queries/_bet_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<% if type == :best %>
<%= t.cell bet.position %>
<% end %>
<%= t.cell (bet.updated_at || bet.created_at).strftime("%d %b %Y") %>
<%= t.cell bet.try(:user).try(:name) || " " %>
<%= t.cell bet.last_edit_date.strftime("%d %b %Y") %>
<%= t.cell bet.author %>
<%= t.cell bet.valid_until %>
<% if bet.active? %>
<%= t.cell button_to 'Deactivate', deactivate_bet_path(bet), class: "govuk-button govuk-button--secondary" %>
Expand Down
4 changes: 2 additions & 2 deletions features/support/best_bets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def check_absence_of_best_bet_on_query_page(query, link)
def check_for_queries_in_csv_format(queries)
headers, *rows = *CSV.parse(page.body)

expect(headers).to eq(["query", "match_type", "link", "best/worst", "comment"])
expect(headers).to eq(["query", "match_type", "link", "best/worst", "comment", "status", "position", "last edit date", "author"])

queries.each do |query|
query.bets.each do |bet|
expect(rows).to include([query.query, query.match_type, bet.link, "best", ""])
expect(rows).to include([query.query, query.match_type, bet.link, "best", "", "Permanent"])
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/models/best_bet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@
it { is_expected.to be_valid }
end
end

describe "#valid_until" do
it "valid_until is permanent if permanent is set to true" do
best_bet = Bet.new(@best_bet_attributes)

expect(best_bet.valid_until).to eq("Permanent")
end

it "returns an expiry date if expiration date is in the future" do
expiration_date = 1.day.from_now
bet_date_attrs = { permanent: false, expiration_date: }
best_bet = Bet.new(@best_bet_attributes.merge(bet_date_attrs))

expect(best_bet.valid_until).to eq("Expires #{expiration_date.strftime('%d %b %Y')}")
end

it "returns a status of Expired if expiration date is in the past" do
bet_date_attrs = { permanent: false }
best_bet = Bet.new(@best_bet_attributes.merge(bet_date_attrs))

expect(best_bet.valid_until).to eq("Expired")
end
end
end

context "when given an internal link" do
Expand Down Expand Up @@ -140,6 +163,16 @@
end
end

describe "#author" do
it "should return the name of user" do
user_name = "Ahsoka Tano"
user = create(:user, name: user_name)
best_bet = Bet.new(@best_bet_attributes.merge(user_id: user.id))

expect(best_bet.author).to eq(user_name)
end
end

describe "bet_date validations" do
it "Temporary bets must have an expiration date" do
bet_date_attrs = { permanent: false, expiration_date: "" }
Expand Down

0 comments on commit 7b3f5f3

Please sign in to comment.