Class: EventsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/events_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_navable, #current_navable=, #current_user, #point_navigation_to, #redirect_www_subdomain, #set_locale

Instance Method Details

#createObject

POST /events POST /events.json

ATTENTION: The create action has to handly authorization manually!



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/events_controller.rb', line 100

def create
  @group = Group.find(params[:group_id])
  authorize! :create_event, @group
  
  @event = Event.new(params[:event])
  @event.name ||= I18n.t(:enter_name_of_event_here)
  @event.start_at ||= Time.zone.now.change(hour: 20, min: 15)
  
  respond_to do |format|
    if @event.save
      
      # Attention: The save call will call some callbacks, which might cause
      # one of the following calls to run into sql deadlock issues.
      # ActiveRecord of Rails 3 does not resolve these issues.
      # Therefore, we use the transaction_retry gem, which retries the
      # call after running into locked records.
      # 
      # TODO: This needs to be carefully checked when we migrate to Rails 4,
      # since the locking behaviour might have changed. The transaction_retry
      # gem has been updated last in 2012!
      #
      @event.reload
      @event.parent_groups << @group if @group
      @event.create_attendees_group
      @event.create_contact_people_group
      @event.contact_people_group.assign_user current_user, at: 2.seconds.ago
      
      # To avoid `ActiveRecord::RecordNotFound` after the redirect, we have to
      # make sure the record can be found.
      #
      # TODO: Check if this is really necessary in Rails 4 anymore.
      #
      @event.wait_for_me_to_exist
      current_user.try(:update_last_seen_activity, I18n.t(:is_adding_an_event), @event)
      
      format.html { redirect_to event_path(@event) }
      format.json { render json: @event.attributes.merge({path: event_path(@event)}), status: :created, location: @event }
    else
      format.html { redirect_to :back }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /events/1 DELETE /events/1.json



160
161
162
163
164
165
166
167
# File 'app/controllers/events_controller.rb', line 160

def destroy
  @event.destroy

  respond_to do |format|
    format.html { redirect_to root_path }
    format.json { head :no_content }
  end
end

#indexObject

GET /events GET /events.json

ATTENTION: The index action has to handly authorization manually!



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/events_controller.rb', line 13

def index
  
  # Which events should be listed
  @group = Group.find params[:group_id] if params[:group_id]
  @user = Group.find params[:user_id] if params[:user_id]
  @user ||= current_user
  @user ||= UserAccount.find_by_auth_token(params[:token]).try(:user) if params[:token].present?
  @all = params[:all]
  @on_local_website = params[:published_on_local_website]
  @on_global_website = params[:published_on_global_website]
  @public = @on_local_website || @on_global_website
  @limit = params[:limit].to_i
  
  # Check the permissions.
  if @all and not @public
    authorize! :index_events, :all
  elsif @all and @public
    authorize! :index_public_events, :all
  elsif @group
    @public ? authorize!(:index_public_events, :all) : authorize!(:index_events, @group)
  elsif @user
    authorize! :index_events, @user
  end  
  
  # Collect the events to list.
  if @all
    @events = Event.where(true)
  elsif @group
    @events = Event.find_all_by_group(@group)
    @navable = @group
  elsif @user
    @events = Event.find_all_by_user(@user)
    @navable = @user
  end
  
  # Filter if only published events are requested.
  @events = @events.where publish_on_local_website: true if @on_local_website
  @events = @events.where publish_on_global_website: true if @on_global_website
  
  # Order events
  @events = @events.order 'events.start_at, events.created_at'
  
  # Limit the number of events.
  # If a limit exists, make sure to return upcoming events.
  @events = @events.upcoming.limit(@limit) if @limit && @limit > 0
  
  # Add the Cross-origin resource sharing header for public requests.
  response.headers['Access-Control-Allow-Origin'] = '*' if @public

  respond_to do |format|
    format.html do
      if @on_local_website or @on_global_website
        render partial: 'events/public_index', locals: {events: @events}
      else
        if @group
          current_user.try(:update_last_seen_activity, I18n.t(:is_looking_at_the_calendar_of, group_name: @group.name), @group)
        else
          current_user.try(:update_last_seen_activity, I18n.t(:is_looking_at_events))
        end
        # renders "index.html.haml"
      end
    end
    format.json { render json: @events }
    format.ics { send_data @events.to_ics, filename: "#{@group.try(:name)} #{Time.zone.now}".parameterize + ".ics" }
  end
end

#inviteObject

POST /events/:event_id/invite/:recipient params:

- recipient
- text
- event_id


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/events_controller.rb', line 221

def invite
  @event = Event.find params[:event_id]
  authorize! :update, @event
  
  @text = params[:text]
  @recipients = []
  
  if params['recipient'] == 'me'
    @recipients = [current_user]
  elsif params['recipient'].to_i > 0
    group = Group.find params['recipient'].to_i
    @recipients = group.members
  end
  
  for recipient in @recipients
    EventMailer.invitation_email(@text, [recipient], @event, current_user).deliver
  end
  
  respond_to do |format|
    format.html { redirect_to event_url(@event) }
    format.json { head :no_content }
  end
end

#joinObject

POST /events/1/join



171
172
173
# File 'app/controllers/events_controller.rb', line 171

def join
  change_attendance(true)
end

#join_via_getObject



177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/events_controller.rb', line 177

def join_via_get
  @event = Event.find params[:event_id]
  if params[:email_confirm] == 'true'  # Only allow GET from email links.
    authorize! :join, @event
    join
  else
    authorize! :read, @event
    redirect_to Event.find(params[:event_id])
  end
end

#leaveObject



174
175
176
# File 'app/controllers/events_controller.rb', line 174

def leave
  change_attendance(false)
end

#showObject

GET /events/1 GET /events/1.json



82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/events_controller.rb', line 82

def show
  @navable = @event
  
  respond_to do |format|
    format.html do
      current_user.try(:update_last_seen_activity, I18n.t(:is_looking_at_the_event, event_name: @event.name), @event)
      # show.html.erb
    end
    format.json { render json: @event }
    format.ics { render text: @event.to_ics }
  end
end

#updateObject

PUT /events/1 PUT /events/1.json



146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/events_controller.rb', line 146

def update
  respond_to do |format|
    if @event.update_attributes!(params[:event])
      format.html { redirect_to @event, notice: 'Event was successfully updated.' }
      format.json { respond_with_bip(@event) }
    else
      format.html { render action: "edit" }
      format.json { respond_with_bip(@event) }
    end
  end
end