Class: AhoyCaptain::EventQuery

Inherits:
ApplicationQuery show all
Defined in:
app/queries/ahoy_captain/event_query.rb

Instance Attribute Summary

Attributes inherited from ApplicationQuery

#params

Instance Method Summary collapse

Methods inherited from ApplicationQuery

call, inherited, #initialize, #inspect

Constructor Details

This class inherits a constructor from AhoyCaptain::ApplicationQuery

Instance Method Details

#buildObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/queries/ahoy_captain/event_query.rb', line 4

def build
  entry_pages = ransack_params_for(:event).select { |k,v| k.start_with?("entry_page") }
  exit_pages = ransack_params_for(:event).select { |k,v| k.start_with?("exit_page") }

  event = AhoyCaptain.event
  shared_context = Ransack::Context.for(event)

  search_parents = event.ransack(
    ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }, context: shared_context
  )

  visit_params = ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") || k.start_with?("events_") || k == :c  }.transform_keys { |key| "visit_#{key}" }
  search_children = AhoyCaptain.visit.ransack(
    visit_params, context: shared_context
  )
  shared_conditions = [search_parents, search_children].map { |search|
    Ransack::Visitor.new.accept(search.base)
  }

  joined = AhoyCaptain.event.joins(shared_context.join_sources)

  if entry_pages.values.any?(&:present?) || params[:controller].include?("entry_pages")
    joined = joined.with_entry_pages
  end

  if exit_pages.values.any?(&:present?) || params[:controller].include?("exit_pages")
    joined = joined.with_exit_pages
  end

  joined.where(shared_conditions.reduce(&:and))
end

#page_viewObject



36
37
38
39
40
# File 'app/queries/ahoy_captain/event_query.rb', line 36

def page_view
  @query = @query.page_view

  self
end