diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 241e7f0093f..e08f195a38d 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -12,20 +12,20 @@ def combine_special_regular_calendar(user) private def generate_calendar_str(user) - events_list = EventList.new - personal_events_list = events_list.fetch_events_list(user) - events_icalendar_str = IcalendarStr.new - calendar_str = events_icalendar_str.convert_events_to_ical(personal_events_list) + events = EventsFetcher.new + personal_events = events.fetch_events(user) + events_icalendar_str = EventsToIcalConverter.new + calendar_str = events_icalendar_str.convert_events_to_ical(personal_events) calendar_to_ical = calendar_str.to_ical calendar_to_ical.gsub(/END:VCALENDAR\r?\n?\z/, '') end def generate_regular_calendar_str(user) - regular_events_list = EventList.new - personal_regular_events_list = regular_events_list.fetch_regular_events_list(user) - regular_events_icalendar_str = IcalendarStr.new - regular_calendar_str = regular_events_icalendar_str.convert_regular_events_to_ical(personal_regular_events_list) + regular_events = EventsFetcher.new + personal_regular_events = regular_events.fetch_regular_events(user) + regular_events_icalendar_str = EventsToIcalConverter.new + regular_calendar_str = regular_events_icalendar_str.convert_regular_events_to_ical(personal_regular_events) regular_calendar_to_ical = regular_calendar_str.to_ical regular_calendar_to_ical.gsub(/\A(?:BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:icalendar-ruby\r\nCALSCALE:GREGORIAN\r\n)/, '') diff --git a/app/models/event_list.rb b/app/models/events_fetcher.rb similarity index 57% rename from app/models/event_list.rb rename to app/models/events_fetcher.rb index 544b36b55ee..6d34b8e7504 100644 --- a/app/models/event_list.rb +++ b/app/models/events_fetcher.rb @@ -1,34 +1,34 @@ # frozen_string_literal: true -class EventList - def fetch_events_list(user) - participated_list = user.participations.pluck(:event_id) - upcoming_event_list = Event.where('start_at > ?', Date.current).pluck(:id) +class EventsFetcher + def fetch_events(user) + participated_events = user.participations.pluck(:event_id) + upcoming_events = Event.where('start_at > ?', Date.current).pluck(:id) - joined_events = Event.where(id: participated_list & upcoming_event_list) - upcoming_events = Event.where(id: upcoming_event_list).where.not(id: participated_list) + joined_events = Event.where(id: participated_events & upcoming_events) + upcoming_events = Event.where(id: upcoming_events).where.not(id: participated_events) { joined_events:, upcoming_events: } end - def fetch_regular_events_list(user) - participated_list = user.regular_event_participations.pluck(:regular_event_id) - regular_events_list = [] - RegularEvent.where(id: participated_list).where(finished: false).find_each do |event| + def fetch_regular_events(user) + participated_events = user.regular_event_participations.pluck(:regular_event_id) + regular_events = [] + RegularEvent.where(id: participated_events).where(finished: false).find_each do |event| event.regular_event_repeat_rules.each do |repeat_rule| current_date = Time.zone.today - list_regular_event_for_year(event, repeat_rule, current_date, regular_events_list) + list_regular_event_for_year(event, repeat_rule, current_date, regular_events) end end - regular_events_list + regular_events end private - def list_regular_event_for_year(event, repeat_rule, current_date, regular_events_list) + def list_regular_event_for_year(event, repeat_rule, current_date, regular_events) while current_date <= Time.zone.today + 1.year if repeat_rule.frequency.zero? day_of_the_week_symbol = DateAndTime::Calculations::DAYS_INTO_WEEK.key(repeat_rule.day_of_the_week) @@ -39,7 +39,7 @@ def list_regular_event_for_year(event, repeat_rule, current_date, regular_event event_date = event.possible_next_event_date(current_date, repeat_rule) current_date = current_date.next_month.beginning_of_month end - regular_events_list << { event_id: event.id, event_date: } + regular_events << { event_id: event.id, event_date: } end end end diff --git a/app/models/icalendar_str.rb b/app/models/events_to_ical_converter.rb similarity index 85% rename from app/models/icalendar_str.rb rename to app/models/events_to_ical_converter.rb index 1c1b4fd537b..c0f0ce3871e 100644 --- a/app/models/icalendar_str.rb +++ b/app/models/events_to_ical_converter.rb @@ -1,23 +1,23 @@ # frozen_string_literal: true -class IcalendarStr - def convert_events_to_ical(events_list) +class EventsToIcalConverter + def convert_events_to_ical(events) event_cal = Icalendar::Calendar.new - events_list[:joined_events].each do |event| + events[:joined_events].each do |event| add_event_to_calendar(event_cal, event, true) end - events_list[:upcoming_events].each do |event| + events[:upcoming_events].each do |event| add_event_to_calendar(event_cal, event, false) end event_cal end - def convert_regular_events_to_ical(regular_events_list) + def convert_regular_events_to_ical(regular_events) regular_event_cal = Icalendar::Calendar.new - add_regular_event_to_calendar(regular_event_cal, regular_events_list) + add_regular_event_to_calendar(regular_event_cal, regular_events) regular_event_cal end @@ -36,8 +36,8 @@ def add_event_to_calendar(event_cal, event, joined) end end - def add_regular_event_to_calendar(regular_event_cal, regular_events_list) - regular_events_list.each do |regular_event| + def add_regular_event_to_calendar(regular_event_cal, regular_events) + regular_events.each do |regular_event| tzid = 'Asia/Tokyo' event_date = Date.parse(regular_event[:event_date].to_s) event = RegularEvent.find(regular_event[:event_id])