Permalink
Browse files

add ListEvents function to content pages

fixes #12
  • Loading branch information...
1 parent d37d7d1 commit 4526e7eec22586cda89d3d2175e7720337ca4846 @jlapier committed Oct 6, 2011
View
2 Gemfile.lock
@@ -46,7 +46,7 @@ GEM
authlogic-oid (1.0.4)
authlogic
builder (2.1.2)
- cancan (1.6.6)
+ cancan (1.6.7)
capybara (1.1.1)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
View
16 app/controllers/application_controller.rb
@@ -9,7 +9,7 @@ class ApplicationController < ActionController::Base
# these actions are accessible by users not logged in; used for EventCalendar and FileShare engines
READ_ACTIONS = %w(index show search download)
- before_filter :protect_event_calendar
+ before_filter :protect_event_calendar, :setup_events
before_filter :protect_file_share
before_filter :setup_file_share if Rails.env == 'development'
@@ -83,6 +83,20 @@ def setup_file_share
EventsController.send :helper, FileAttachmentsHelper
end
+ def setup_events
+ if in_event_calendar?
+ EventsController.send :cache_sweeper, :event_sweeper
+ end
+ end
+
+ def expire_content_page_caches
+ expire_fragment :controller => 'content_pages', :action => 'home'
+ ContentPage.find(:all).each do |content_page|
+ expire_fragment :controller => 'content_pages', :action => 'show', :id => content_page
+ expire_fragment :controller => 'content_pages', :action => 'show', :id => content_page.id.to_i
+ end
+ end
+
# checks to see if user is a member of a given access group - if not,
# redirect to account controller
# for multiple groups, if user is in any of the given groups, they have access
View
10 app/controllers/content_pages_controller.rb
@@ -1,6 +1,6 @@
class ContentPagesController < ApplicationController
before_filter :require_admin_user, :except => [:home, :index, :show, :search]
- before_filter :expire_caches, :only => [:update, :destroy, :delete_asset]
+ before_filter :expire_content_page_caches, :only => [:update, :destroy, :delete_asset]
def home
@content_page = ContentPage.get_front_page
@@ -173,12 +173,4 @@ def search
render :action => :index
end
- private
- def expire_caches
- expire_fragment :controller => 'content_pages', :action => 'home'
- ContentPage.find(:all).each do |content_page|
- expire_fragment :controller => 'content_pages', :action => 'show', :id => content_page
- expire_fragment :controller => 'content_pages', :action => 'show', :id => content_page.id.to_i
- end
- end
end
View
14 app/models/content_page.rb
@@ -60,14 +60,14 @@ def function(function_string)
:order => order_string_from_sort_in_function(sort_order),
:limit => limit, :other_params => param }
- begin
+# begin
self.send("#{function_name.downcase}_to_html", params_to_send)
- rescue NameError
- return "<em>Unknown function: #{function_name}</em>"
- rescue => e
- return "<em>Error in function: #{function_name}</em>" +
- "<span style=\"display:none;\">#{e.inspect}</span>"
- end
+# rescue NameError
+# return "<em>Unknown function: #{function_name}</em>"
+# rescue => e
+# return "<em>Error in function: #{function_name}</em>" +
+# "<!-- #{e.inspect} -->"
+# end
end
private
View
23 app/sweepers/event_sweeper.rb
@@ -0,0 +1,23 @@
+class EventSweeper < ActionController::Caching::Sweeper
+ observe Event
+
+ def after_create(event)
+ expire_content_page_caches
+ end
+
+ def after_update(event)
+ expire_content_page_caches
+ end
+
+ def after_destroy(event)
+ expire_content_page_caches
+ end
+
+ def expire_content_page_caches
+ expire_fragment :controller => 'content_pages', :action => 'home'
+ ContentPage.find(:all).each do |content_page|
+ expire_fragment :controller => 'content_pages', :action => 'show', :id => content_page
+ expire_fragment :controller => 'content_pages', :action => 'show', :id => content_page.id.to_i
+ end
+ end
+end
View
1 app/views/content_pages/_form.html.erb
@@ -15,6 +15,7 @@
<li>[[ListCategories]] &nbsp; &nbsp; <em>lists all categories</em></li>
<li>[[ListPagesInCategory Some Kind of Category]] &nbsp; &nbsp;
<em>list all pages in the category called "Some Kind of Category"</em></li>
+ <li>[[ListEvents (optional event type)]] <em>list current/upcoming events</em></li>
<li>[[LinkPage Page Name]] &nbsp; &nbsp; <em>links directly to the page specified</em></li>
<li>[[LinkCategory Some Kind of Category]] &nbsp; &nbsp; <em>links to the category index page</em></li>
<li>[[TreeCategories]] &nbsp; &nbsp; <em>create a tree-like list of all categories and their pages</em></li>
View
2 config/application.rb
@@ -56,7 +56,7 @@ class Application < Rails::Application
# config.active_record.schema_format = :sql
# Activate observers that should always be running
- config.active_record.observers = [:wiki_page_observer, :user_observer]
+ config.active_record.observers = [:wiki_page_observer, :user_observer, :event_sweeper]
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
View
17 lib/html_generator.rb
@@ -38,18 +38,23 @@ def list_categories_to_html(options = {})
alias_method :listcategories_to_html, :list_categories_to_html
def list_events_to_html(options = {})
- events = Event.all :order => options[:order], :limit => options[:limit]
- if options[:other_params]
- events.where :event_type => options[:other_params]
+ events = Event.where "start_on > ?", 1.week.ago
+ unless options[:other_params].blank?
+ events = events.where :event_type => options[:other_params]
end
+ events = events.order("start_on ASC").limit(options[:limit])
out = "<ul>"
if events.empty?
out += "<li><em>No events were found</em></li>"
else
- out += events.map { |event|
- "<li><a href=\"/events/#{event.id}\">#{event.name}</a></li>"
- }.join("")
+ events.each do |event|
+ date_string = event.start_on.strftime('%B %d')
+ if event.end_on and event.end_on.to_date != event.start_on.to_date
+ date_string += " - " + (event.start_on.month == event.end_on.month ? event.end_on.strftime('%d') : event.end_on.strftime('%B %d'))
+ end
+ out += "<li><a href=\"/events/#{event.id}\">#{date_string}: #{event.name}</a></li>"
+ end
end
out += "</ul>"
View
5 spec/models/event_observer_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe EventObserver do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 4526e7e

Please sign in to comment.