Completed
Push — master ( 8ffdab...d9364e )
by
unknown
11s
created

SessionsController.sessions_for_event()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
class SessionsController < ApplicationController
3
4
  load_resource only: [:new, :show, :edit, :create, :update]
5
  before_action :verify_session, only: [:new, :create, :update, :edit]
6
  before_action :verify_owner, only: [:update, :edit]
7
8
  respond_to :html
9
  respond_to :json, only: :index
10
11
  def show
12
    @similar_sessions = []
13
    @similar_sessions = @session.recommended_sessions
14
    respond_with(@session)
15
  end
16
17
  def new
18
    respond_with(@session)
19
  end
20
21
  def edit
22
    respond_with(@session)
23
  end
24
25
  def update
26
    @session.update(session_params)
27
    respond_with(@session)
28
  end
29
30
  def index
31
    if params[:event_id].present?
32
      @event = Event.find(params[:event_id])
33
    else
34
      @event = Event.current_event
35
    end
36
37
    respond_to do |format|
38
      format.json do
39
        render json: (
40
          cache ['sessions.json'] + event_schedule_cache_key(@event), expires_in: 10.minutes do
41
            SessionsJsonBuilder.new.to_json(
42
              Session.preload_attendance_counts(
43
                sessions_for_event(@event)))
44
          end
45
        )
46
      end
47
      format.html do
48
        @sessions = sessions_for_event(@event)
49
      end
50
    end
51
  end
52
53
  def create
54
    @session.attributes = session_params
55
    @session.participant = current_participant
56
    @session.event = Event.current_event
57
58
    if @session.save
59
      flash[:notice] = "Thanks for adding your session."
60
      redirect_to @session
61
    else
62
      render :action => 'new'
63
    end
64
  end
65
66
  STOP_WORDS = Set.new(['session', 'etc', 'just', 'presentation', 'get', 'discussion'])
67
68
  def words
69
    @sessions = Event.current_event.sessions
70
    @words = @sessions.map(&:description).
71
      map { |desc| view_context.markdown(desc) }.
72
      map { |md| view_context.strip_tags(md) }.
73
      map(&:downcase).
74
      join(" ").
75
      gsub(/[.*,-?()+!"•—%]/, '').
76
      split(/\s+/).
77
      reject { |w| STOP_WORDS.include?(w) }.
78
      join(" ")
79
  end
80
81
  def export
82
    @sessions = Event.current_event.sessions.all.order('lower(title) asc')
83
    render :layout => 'export'
84
  end
85 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
86
  def popularity
87
    @sessions = Event.current_event.sessions.with_attendence_count.all.order("COALESCE(attendence_count, 0) desc")
88
    render :layout => 'export'
89
  end
90
91
  private
92
93
  def session_params
94
    params.require(controller_name.singularize).permit(:title, :description, :level_id, :name, :email, :category_ids => [])
95
  end
96
97
  def verify_owner
98
    redirect_to @session if @session.participant != current_participant
99
  end
100
101
private
102
103
  def sessions_for_event(event)
104
    @event.sessions
105
      .includes(:presenters, :categories, :participant, :room, :timeslot, :level)
106
      .order('created_at desc')
107
      .distinct
108
  end
109
110
end
111