Skip to content

Commit

Permalink
Use respond_with on EventsController#index. [#64]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Dec 29, 2011
1 parent 952e9d4 commit 7ca52c4
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions app/controllers/events_controller.rb
Expand Up @@ -6,34 +6,31 @@ class EventsController < ApplicationController
before_filter :require_user, :except => :feed
before_filter :login_from_key, :only => :feed
after_filter :ical_header, :only => :export # assign the correct MIME type so that it gets recognized as an iCal event

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

make_resourceful do
actions :index, :create, :new, :edit, :update, :show

before :index do
params[:order] ||= 'date' # isn't it enough to define this in routes.rb?
params[:direction] ||= 'asc' # and this?
@page_title = _("Upcoming events")
@order = params[:order]
@direction = params[:direction]
@search = params[:search].extend(Search) if params[:search]
end

response_for :index do |format|
format.html

respond_to :html
respond_to :pdf, only: :index

def index
set_table_headers
@events = current_objects
respond_with @events do |format|
format.pdf do
@users = current_objects.blank? ? [] : current_objects[0].calendar.permissions.find_all_by_show_in_report(true, :include => :user).collect{|x| x.user}.sort # TODO: fix for multiple calendars
prawnto :prawn => {:page_layout => :landscape}
render :layout => false
end
end

end

make_resourceful do
actions :create, :new, :edit, :update, :show

before :new do
@page_title = _("Add event")
end

response_for :edit do
if !current_object.allow?(:edit)
flash[:error] = _("You are not authorized to edit that event.")
Expand All @@ -43,17 +40,17 @@ class EventsController < ApplicationController
render :action => 'new'
end
end

response_for :update, :create do
flash[:notice] = _("Your event has been saved.")
redirect_to :action => :index
end

response_for :update_fails, :create_fails do
flash[:error] = _("We couldn't process that request. Please try again.")
render :new
end

response_for :show do
if !current_object.allow?(:show)
flash[:error] = _("You are not authorized to view that event.")
Expand All @@ -63,7 +60,7 @@ class EventsController < ApplicationController
end
end
end

# Generate an RSS feed of events.
def feed
respond_to do |format|
Expand Down Expand Up @@ -115,7 +112,7 @@ def change_status
redirect_to :action => :index
end
end

# Display a map page for the current #Event.
def map
begin
Expand All @@ -127,7 +124,7 @@ def map
end
@page_title = _("Map for %{event}") % {:event => @event.name}
end

# Return non-deleted events between params[:from_date] and params[:to_date], optionally ordered as specified by params[:order] and [:direction]. Provided for use with make_resourceful[http://mr.hamptoncatlin.com].
def current_objects
user = params[:feed_user] || User.current_user
Expand All @@ -143,36 +140,45 @@ def current_objects

calendars = search[:calendar_id].blank? ? nil : search[:calendar_id]
end

order = params[:order] || 'date'
from_date = params[:from_date] || Time.zone.today
to_date = params[:to_date]
direction = params[:direction] || 'asc'
calendars ||= user.calendars.collect{|c| c.id}

if to_date == nil
date_query = 'date >= :from_date'
else
date_query = 'date BETWEEN :from_date AND :to_date'
end

@current_objects || current_model.find(:all, :conditions => ['calendar_id IN (:calendars) AND ' + date_query, {:calendars => calendars, :from_date => from_date, :to_date => to_date}], :order => "#{order} #{direction}")
end
protected

private
# Return an HTTP header with proper MIME type for iCal.
def ical_header
headers['Content-Type'] = 'text/calendar'
end

# Log user in based on single_access_token.
def login_from_key
params[:feed_user] = User.find_by_single_access_token(params[:key])
end

# Handler for #RecordNotFound.
def record_not_found
flash[:error] = _("Couldn't find any event to edit!")
redirect_to(:action => :index) and return
end

def set_table_headers
params[:order] ||= 'date' # isn't it enough to define this in routes.rb?
params[:direction] ||= 'asc' # and this?
@page_title = _("Upcoming events")
@order = params[:order]
@direction = params[:direction]
@search = params[:search].extend(Search) if params[:search]
end
end

0 comments on commit 7ca52c4

Please sign in to comment.