Created
June 9, 2016 19:15
-
-
Save SuperManEver/22f60d4688f865c616a8cc3fb7e5d4a2 to your computer and use it in GitHub Desktop.
Attempt to extract class methods to separate class with delegation to this new class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event < ActiveRecord::Base | |
delegate :next_occurrence, :select_events_with_time, :find_category_next_occurences :to => :OccurrenceManager | |
end | |
class OccurrenceManager | |
def self.next_occurrence(event_type, begin_time = COLLECTION_TIME_PAST.ago) | |
events_with_times = select_events_with_time(event_type: event_type, begin_time: begin_time) | |
unless events_with_times.empty? | |
events_with_times = events_with_times.sort {|e1, e2| e1.time <=> e2.time } | |
events_with_times.first.event.next_occurrence_time_attr = events_with_times.first.time | |
events_with_times.first.event | |
end | |
end | |
def self.select_events_with_time(args) | |
event_type = args.fetch(:event_type) | |
begin_time = args.fetch(:begin_time) | |
find_category_next_occurences(event_type, begin_time) | |
end | |
def self.find_category_next_occurences(event_type, begin_time) | |
Event.where(category: event_type).map do |event| | |
event.next_event_occurrence_with_time(begin_time) | |
end.compact | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment