Module: MongoidOccurrenceViews::Event::HasViewsOnOccurrences::ClassMethods

Defined in:
lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb

Instance Method Summary collapse

Instance Method Details

#event_class_namesObject



69
70
71
72
73
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 69

def event_class_names
  ObjectSpace.each_object(Class).select do |cls|
    cls.included_modules.include?(MongoidOccurrenceViews::Event)
  end.map(&:to_s)
end

#event_relationsObject



55
56
57
58
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 55

def event_relations
  return unless self.relations.present?
  self.relations.values.select { |rel| is_event_relation?(rel) }
end

#expanded_occurrences_view_nameObject



26
27
28
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 26

def expanded_occurrences_view_name
  [collection.name, EXPANDED_VIEW_NAME_SUFFIX].join('__').freeze
end

#is_event_relation?(rel) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 60

def is_event_relation?(rel)
  relation_class_name = rel.options[:class_name]
  return false unless relation_class_name.present?
  return true if event_class_names.include?(relation_class_name)
  return unless relation_class = relation_class_name.safe_constantize
  event_superclasses = event_class_names.map { |cn| cn.safe_constantize.ancestors }.flatten
  event_superclasses.include? relation_class
end

#occurrence_relation_chainObject



50
51
52
53
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 50

def occurrence_relation_chain
  return occurrence_relation_names unless event_relations.present?
  [event_relations.first.store_as, occurrence_relation_names].flatten
end

#occurrence_relation_namesObject



75
76
77
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 75

def occurrence_relation_names
  %w[occurrences daily_occurrences]
end

#occurrence_relations_chainedObject



44
45
46
47
48
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 44

def occurrence_relations_chained
  occurrence_relation_chain.each_with_index.map do |_, i|
    occurrence_relation_chain[0..i].join('.')
  end
end

#occurrences_ordering_view_nameObject



22
23
24
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 22

def occurrences_ordering_view_name
  [collection.name, ORDERING_VIEW_NAME_SUFFIX].join('__').freeze
end

#with_expanded_occurrences_view(&block) ⇒ Object



30
31
32
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 30

def with_expanded_occurrences_view(&block)
  with_occurrences_view(expanded: true, &block)
end

#with_occurrences_ordering_view(&block) ⇒ Object



34
35
36
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 34

def with_occurrences_ordering_view(&block)
  with_occurrences_view(expanded: false, &block)
end

#with_occurrences_view(options = {}, &block) ⇒ Object



38
39
40
41
42
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 38

def with_occurrences_view(options = {}, &block)
  expanded = options.delete(:expanded) { |_| true }
  options[:collection] ||= expanded ? expanded_occurrences_view_name : occurrences_ordering_view_name
  criteria.with(options, &block)
end