From 1f649fb520b757c86fbaa04bb454eb9fabfcaea8 Mon Sep 17 00:00:00 2001 From: Ivan Kocienski Date: Wed, 18 Sep 2024 09:49:25 +0100 Subject: [PATCH] Code tidy-up - removed event scopes that are on event instance - removed dead code - removed some other bits --- app/controllers/events_controller.rb | 2 +- app/models/event.rb | 17 ------ app/views/events/index.html.haml | 2 - lib/tasks/db_import.rake | 30 ----------- spec/integration/event_integration_spec.rb | 53 ------------------- spec/models/event_instance_spec.rb | 57 +++++++++++++++++++- spec/models/event_spec.rb | 60 ---------------------- 7 files changed, 57 insertions(+), 164 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 1fcb037..61a95a4 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -25,8 +25,8 @@ def index def show @event_instance = EventInstance .where( id: params[:id] ) - #.joins(:event) .first + if @event_instance.blank? @message = "Could not find event with that ID" render 'home/not_found', status: :not_found diff --git a/app/models/event.rb b/app/models/event.rb index 96c6956..a2491f9 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -25,21 +25,4 @@ def to_s def address @address ||= EventAddress.new(self) end - - # needed ?? - - scope :from_day_onward, lambda { |epoch| - joins(:event_instances) - .where("date(event_instances.starts_at) >= date(?)", epoch) - } - - scope :on_day, lambda { |day| - joins(:event_instances) - .where("date(event_instances.starts_at) = date(?)", day) - } - - scope :before_date, lambda { |day| - joins(:event_instances) - .where("date(event_instances.starts_at) < date(?)", day) - } end diff --git a/app/views/events/index.html.haml b/app/views/events/index.html.haml index aa31dc4..840e5ff 100644 --- a/app/views/events/index.html.haml +++ b/app/views/events/index.html.haml @@ -2,6 +2,4 @@ %h1 Events --# %p #{@events.count} events from #{@epoch.strftime('%A %e, %Y')} - .events-listing= render_event_by_day_list(@event_instances) diff --git a/lib/tasks/db_import.rake b/lib/tasks/db_import.rake index 86c5beb..4c012a2 100644 --- a/lib/tasks/db_import.rake +++ b/lib/tasks/db_import.rake @@ -179,33 +179,3 @@ namespace :db do end end end - -__END__ - - def import_events - path = Rails.root.join('db/fixtures/events.json') - puts "Importing events from #{path}" - - data = JSON.parse(File.open(path).read) - data['data']['eventsByFilter'].each do |event_data| - organizer_id = event_data['organizer']['id'].to_i - - partner = @partners_by_placecal_id[organizer_id] - raise "import_events: partner not found for organizer #{organizer_id}" if partner.blank? - - event_data['address'] ||= {} - - partner.events.create!( - placecal_id: event_data['id'], - name: event_data['name'], - summary: event_data['summary'], - description: event_data['description'], - starts_at: Time.zone.parse(event_data['startDate']), - ends_at: Time.zone.parse(event_data['endDate']), - organizer_placecal_id: organizer_id, - address_street: event_data['address']['streetAddress'], - address_postcode: event_data['address']['postalCode'] - ) - - end - end diff --git a/spec/integration/event_integration_spec.rb b/spec/integration/event_integration_spec.rb index 48f258f..23ef2eb 100644 --- a/spec/integration/event_integration_spec.rb +++ b/spec/integration/event_integration_spec.rb @@ -50,56 +50,3 @@ def create_events_for_partner(count, id_offset, time_offset) end end end - - -__END__ - - 4.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY, - placecal_id: 200 + n - ) - end - 2.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY + 1.day, - placecal_id: 300 + n - ) - end - 3.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY + 2.days, - placecal_id: 400 + n - ) - end - 1.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY + 3.days, - placecal_id: 500 + n - ) - end - 4.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY + 4.days, - placecal_id: 600 + n - ) - end - 5.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY + 5.days, - placecal_id: 700 + n - ) - end - 1.times do |n| - partner.events.create!( - name: 'Alpha', - starts_at: FakeTime::TODAY + 6.days, - placecal_id: 800 + n - ) - end diff --git a/spec/models/event_instance_spec.rb b/spec/models/event_instance_spec.rb index 8505417..905b1a7 100644 --- a/spec/models/event_instance_spec.rb +++ b/spec/models/event_instance_spec.rb @@ -1,5 +1,60 @@ require 'rails_helper' RSpec.describe EventInstance, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + context '(date scope)' do + def create_event(start, placecal_id, partner) + Event.transaction do + + event = Event.create! partner: partner + event.event_instances.create!( + starts_at: start, + placecal_id: placecal_id + ) + end + end + + before :each do + partner = Partner.create! name: 'Alpha', placecal_id: 200 + + # 1st of june + create_event Time.new(2000, 6, 1, 10, 30), 100, partner + create_event Time.new(2000, 6, 1, 12, 30), 100, partner + + # 4th of june + create_event Time.new(2000, 6, 4, 9, 0), 100, partner + create_event Time.new(2000, 6, 4, 12, 0), 100, partner + create_event Time.new(2000, 6, 4, 17, 30), 100, partner + + # 10th of june + create_event Time.new(2000, 6, 10, 8, 0), 100, partner + create_event Time.new(2000, 6, 10, 10, 0), 100, partner + + # 1st of july + create_event Time.new(2000, 7, 1, 10, 30), 100, partner + end + + # june 4th at 4.25pm + let(:epoch) { Time.new(2000, 6, 4, 16, 25) } + + context '::from_day_onward' do + it 'returns events in future of epoch (including all events on same day)' do + found = EventInstance.from_day_onward(epoch) + expect(found.count).to eq 6 + end + end + + context '::on_day' do + it 'returns events only on that day' do + found = EventInstance.on_day(epoch) + expect(found.count).to eq 3 + end + end + + context '::before_date' do + it 'returns all events *before* date' do + found = EventInstance.before_date(epoch) + expect(found.count).to eq 2 + end + end + end end diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 4af6dc4..5692e24 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -1,64 +1,4 @@ require 'rails_helper' RSpec.describe Event, type: :model do - - context '(date scope)' do - def create_event(start, placecal_id, partner) - Event.transaction do - - event = Event.create! partner: partner - event.event_instances.create!( - starts_at: start, - placecal_id: placecal_id - ) - end - end - - before :each do - partner = Partner.create! name: 'Alpha', placecal_id: 200 - - # 1st of june - create_event Time.new(2000, 6, 1, 10, 30), 100, partner - create_event Time.new(2000, 6, 1, 12, 30), 100, partner - - # 4th of june - create_event Time.new(2000, 6, 4, 9, 0), 100, partner - create_event Time.new(2000, 6, 4, 12, 0), 100, partner - create_event Time.new(2000, 6, 4, 17, 30), 100, partner - - # 10th of june - create_event Time.new(2000, 6, 10, 8, 0), 100, partner - create_event Time.new(2000, 6, 10, 10, 0), 100, partner - - # 1st of july - create_event Time.new(2000, 7, 1, 10, 30), 100, partner - end - - # june 4th at 4.25pm - let(:epoch) { Time.new(2000, 6, 4, 16, 25) } - - context '::from_day_onward' do - it 'returns events in future of epoch (including all events on same day)' do - found = Event.from_day_onward(epoch) - expect(found.count).to eq 6 - end - end - - context '::on_day' do - it 'returns events only on that day' do - found = Event.on_day(epoch) - expect(found.count).to eq 3 - end - end - - context '::before_date' do - it 'returns all events *before* date' do - found = Event.before_date(epoch) - expect(found.count).to eq 2 - end - end - - - - end end