diff --git a/app/views/plantings/index.ics.erb b/app/views/plantings/index.ics.erb index 6fbb44d62e..0c4ffdffcc 100644 --- a/app/views/plantings/index.ics.erb +++ b/app/views/plantings/index.ics.erb @@ -7,29 +7,29 @@ cal.description = "Plantings by #{@owner.login_name}" event = Icalendar::Event.new lines = [] - lines << "Quantity: #{planting['quantity'] ? planting['quantity'] : 'unknown' }" - lines << "Planted on: #{planting['planted_at'] ? planting['planted_at'] : 'unknown' }" - lines << "Sunniness: #{planting['sunniness'] ? planting['sunniness'] : 'unknown' }" - lines << "Planted from: #{planting['planted_from'] ? planting['planted_from'] : 'unknown' }" - lines << "First harvest from: #{planting['first_harvest_predicted_at'] ? planting['first_harvest_predicted_at'] : 'unknown' }" - lines << "Last harvest from: #{planting['last_harvest_predicted_at'] ? planting['last_harvest_predicted_at'] : 'unknown' }" - lines << "Finish predicted at: #{planting['finish_predicted_at'] ? planting['finish_predicted_at'] : 'unknown'}" - lines << "Finished at: #{planting['finished_at'] ? planting['finished_at'] : 'unknown' }" + lines << "Quantity: #{planting.quantity || 'unknown' }" + lines << "Planted on: #{planting.planted_at || 'unknown' }" + lines << "Sunniness: #{planting.sunniness || 'unknown' }" + lines << "Planted from: #{planting.planted_from || 'unknown' }" + lines << "First harvest from: #{planting.first_harvest_predicted_at || 'unknown' }" + lines << "Last harvest from: #{planting.last_harvest_predicted_at || 'unknown' }" + lines << "Finish predicted at: #{planting.finish_predicted_at || 'unknown'}" + lines << "Finished at: #{planting.finished_at || 'unknown' }" lines << planting.description - finish_date = Date.parse(planting['finished_at'] || planting['finish_predicted_at'] || planting['last_harvest_predicted_at']) rescue nil + finish_date = (planting.finished_at || planting.finish_predicted_at || planting.last_harvest_predicted_at)&.to_date - event.dtstart = Time.at(planting['created_at']) - event.dtend = finish_date || 1.day.from_now + event.dtstart = planting.created_at + event.dtend = finish_date || 1.day.from_now.to_date event.summary = planting.crop.name event.description = lines.join("\n") event.ip_class = "PUBLIC" - event.url = planting_url(slug: planting['slug']) + event.url = planting_url(slug: planting.slug) cal.add_event(event) if finish_date && finish_date > Date.today - predicted_date = Date.parse(planting['first_harvest_predicted_at']) if planting['first_harvest_predicted_at'] + predicted_date = planting.first_harvest_predicted_at&.to_date todo = Icalendar::Todo.new todo.dtstart = predicted_date || finish_date || Date.today @@ -44,7 +44,7 @@ cal.description = "Plantings by #{@owner.login_name}" event.dtend = finish_date event.summary = "Harvest #{planting.crop.name}" event.ip_class = "PUBLIC" - event.url = planting_url(slug: planting['slug']) + event.url = planting_url(slug: planting.slug) cal.add_event(event) end diff --git a/spec/requests/plantings_spec.rb b/spec/requests/plantings_spec.rb index 4516af117e..e47b7752e2 100644 --- a/spec/requests/plantings_spec.rb +++ b/spec/requests/plantings_spec.rb @@ -15,7 +15,7 @@ before do @member = create(:interesting_member) - @predictable_planting = create(:predictable_planting, owner: @member, planted_at: 1.days.ago, days_to_first_harvest: 10, + @predictable_planting = create(:predictable_planting, owner: @member, planted_at: 1.day.ago, days_to_first_harvest: 10, days_to_last_harvest: 20) @predictable_planting.crop.update(median_days_to_first_harvest: 10) @@ -27,25 +27,23 @@ end describe "GET /members/x/plantings.ics" do - it "works!", pending: "Regression from elasticsearch" do + it "works!" do get member_plantings_path(@member, format: "ics") calendar = Icalendar::Parser.new(response.body, true).parse.first expect(calendar.description[0].to_s).to eq "Plantings by #{@member.login_name}" events = calendar.events - expect(events.length).to eq 6 + expect(events.length).to eq 7 - # TODO: Better date comparison - # Predicted finish should be used - expect(events[2].summary.to_s).to include @predictable_planting.crop.name - expect(events[2].dtstart.to_datetime.to_i).to be_within(1.second).of @predictable_planting.created_at.to_i - expect(events[2].dtend.to_date).to eq @predictable_planting.finish_predicted_at + predictable_event = events.find { |e| e.dtend.to_date == @predictable_planting.finish_predicted_at } + expect(predictable_event.summary.to_s).to include @predictable_planting.crop.name + expect(predictable_event.dtstart.to_datetime.to_i).to be_within(1.second).of @predictable_planting.created_at.to_i # Actual finish should be used # expect(events[4].dtend.to_date).to be_within(1.second).of @finished_planting.finished_at # Otherwise, tomorrow should be used - expect(events[3].dtend.to_date).to eq 1.day.from_now.to_date + expect(events.map { |e| e.dtend.to_date }).to include 1.day.from_now.to_date # TBA: Perennial and annual crops predictions of 'next' harvest date don't really fit