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



46
47
48
49
50
51
52
# File 'app/controllers/effective/events_controller.rb', line 46

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



54
55
56
57
# File 'app/controllers/effective/events_controller.rb', line 54

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
# File 'app/controllers/effective/events_controller.rb', line 10

def index
  @page_title ||= 'Events'
  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



59
60
61
62
63
64
65
66
67
# File 'app/controllers/effective/events_controller.rb', line 59

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/effective/events_controller.rb', line 24

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?),
      'Click here to',
      ("<a href='#{effective_events.edit_admin_event_path(@event)}' class='alert-link'>edit event settings</a>.")
    ].compact.join(' ')
  end

  @page_title ||= @event.to_s
end