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



76
77
78
79
80
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 76

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

#event_relationsObject



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

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

#expanded_occurrences_view_nameObject



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

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

#is_event_relation?(rel) ⇒ Boolean

Returns:

  • (Boolean)


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

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



57
58
59
60
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 57

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



82
83
84
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 82

def occurrence_relation_names
  %w[occurrences daily_occurrences]
end

#occurrence_relations_chainedObject



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

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



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

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

#with_expanded_occurrences_view(&block) ⇒ Object



37
38
39
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 37

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

#with_occurrences_ordering_view(&block) ⇒ Object



41
42
43
# File 'lib/mongoid_occurrence_views/event/has_views_on_occurrences.rb', line 41

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

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



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

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