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



50
51
52
53
54
55
56
# File 'app/controllers/effective/events_controller.rb', line 50

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



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

def event_category
  return nil unless params[:category].present?
  EffectiveEvents.categories.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
# File 'app/controllers/effective/events_controller.rb', line 10

def index
  if event_category.present?
    @page_title = event_category
    @event_category = event_category
  else
    @page_title ||= view_context.events_name_label
  end
  EffectiveResources.authorize!(self, :index, Effective::Event)

  # 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



63
64
65
66
67
68
69
70
71
# File 'app/controllers/effective/events_controller.rb', line 63

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/effective/events_controller.rb', line 29

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