Class: Effective::EventsController

Inherits:
ApplicationController
  • Object
show all
Includes:
CrudController
Defined in:
app/controllers/effective/events_controller.rb

Instance Method Summary collapse

Instance Method Details

#build_event_searchObject



56
57
58
59
60
61
62
# File 'app/controllers/effective/events_controller.rb', line 56

def build_event_search
  search = EventSearch.new(search_params)
  search.current_user = current_user
  search.unpublished = EffectiveResources.authorized?(self, :admin, :effective_events)
  search.category ||= event_category
  search
end

#event_categoryObject



64
65
66
67
# File 'app/controllers/effective/events_controller.rb', line 64

def event_category
  return nil unless params[:category].present?
  (Array(EffectiveEvents.categories) + ['past']).find { |category| category.parameterize == params[:category] }
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/effective/events_controller.rb', line 10

def index
  EffectiveResources.authorize!(self, :index, Effective::Event)

  # Page Title
  if event_category.blank?
    @page_title = "Upcoming #{EffectiveResources.ets(Effective::Event)}"
  elsif event_category == 'past'
    @page_title = "Past #{EffectiveResources.ets(Effective::Event)}"
  elsif event_category.present?
    @page_title = event_category
    @event_category = event_category
  else
    @page_title = view_context.events_name_label
  end

  # Sometimes we just display a Datatable for the events
  @datatable = EffectiveResources.best('EffectiveEventsDatatable').new

  # But more often we do a full paginated index with search screen
  @event_search = build_event_search
  @event_search.search!

  @events = @event_search.results(page: params[:page])
end

#search_paramsObject



69
70
71
72
73
74
75
76
77
# File 'app/controllers/effective/events_controller.rb', line 69

def search_params
  return {} unless params[:q].present?

  if params[:q].respond_to?(:to_h)      # From the search form
    params.require(:q).permit!
  else
    { term: params.permit(:q).fetch(:q) }   # From the url /events?q=asdf
  end
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/effective/events_controller.rb', line 35

def show
  @event = resource_scope.find(params[:id])
  @upcoming_events = resource_scope.upcoming.where.not(id: @event.id)

  if @event.respond_to?(:roles_permit?)
    raise Effective::AccessDenied.new('Access Denied', :show, @event) unless @event.roles_permit?(current_user)
  end

  EffectiveResources.authorize!(self, :show, @event)

  if EffectiveResources.authorized?(self, :admin, :effective_events)
    flash.now[:warning] = [
      'Hi Admin!',
      ('You are viewing a hidden event. ' if @event.draft?),
      ("<a href='#{effective_events.edit_admin_event_path(@event)}' class='alert-link'>Edit this event</a>.")
    ].compact.join(' ')
  end

  @page_title ||= @event.to_s
end