Skip to content

Commit

Permalink
Testimonials fully RESTful, and loads more refactored as i went.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aimee Daniells committed Apr 13, 2009
1 parent 16c73ce commit d53462a
Show file tree
Hide file tree
Showing 24 changed files with 310 additions and 226 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin_controller.rb
Expand Up @@ -12,7 +12,7 @@ def login
session[:preference] = Preference.find(:first, :conditions => ["person_id = ?", session[:person].id ])

flash[:notice] = "You are now logged in."
redirect_back_or_default :controller => 'home', :action => 'index'
redirect_back_or_default(home_path)
else
flash[:notice] = "Log in failed - please try again."
@login = params[:person_login]
Expand Down
40 changes: 37 additions & 3 deletions app/controllers/application_controller.rb
Expand Up @@ -17,9 +17,39 @@ def show_ads?
return true if !logged_in?
session[:person].ads
end

def admin?
return false if !logged_in?
session[:person].status == 'Site Creator'
end

# local: starts with a / or contains the same domain of this site.
def local?(referer)
return true if referer =~ /^\//
referer.include?(request.domain)
end

def home_path
return welcome_path if !logged_in?
case session[:person].default_view
when 'Workload' then return workload_path
when 'Hot map' then return hotmap_path
when 'Calendar' then return calendar_path
when 'Collage' then return collage_path
when 'Statistics' then return my_statistics_path
else return workload_path
end
end


protected

def admin_authorised
if !admin?
flash[:notice] = "Sorry, you don't have permission to view that page."
redirect_back
end
end

def set_charset
content_type = headers["Content-Type"] || "text/html"
Expand All @@ -32,9 +62,13 @@ def set_time_zone
Time.zone = session[:person].timezone_name if session[:person]
end

def redirect_back(redirect_opts = nil)
redirect_opts ||= {:controller => 'tasks', :action => 'workload'}
request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : redirect_to(redirect_opts)
def redirect_back
referer = request.env["HTTP_REFERER"]
if referer.blank? or !(local?(referer))
redirect_to(home_path)
return
end
redirect_to(referer)
end

def find_current_date
Expand Down
19 changes: 1 addition & 18 deletions app/controllers/completions_controller.rb
Expand Up @@ -142,24 +142,7 @@ def undo
end

# Re-direct back wherever they came from.
if params[:return]

if params[:return] == 'task'
redirect_to :controller => 'tasks', :action => 'show', :id => @task.id
elsif params[:return] == 'today'
redirect_to :controller => 'completions', :action => 'today'
elsif params[:return] == 'sevendays'
redirect_to :controller => 'completions', :action => 'sevendays'
elsif params[:return] == 'month'
redirect_to :controller => 'completions', :action => 'month'
else
# Don't really know where they want to go!
redirect_to :controller => 'tasks', :action => 'workload'
end

else
redirect_to :controller => 'tasks', :action => 'workload'
end
redirect_back

else
# Naughty, naughty!
Expand Down
19 changes: 2 additions & 17 deletions app/controllers/home_controller.rb
Expand Up @@ -8,26 +8,11 @@ def welcome
end

def index
if session[:person].nil?


if !logged_in?
session[:referrer] = params[:referrer] if params[:referrer]
session[:code] = params[:code] if params[:code]
render :action => 'welcome'
else
@loggedon = session[:person]
if @loggedon.default_view == 'Calendar'
redirect_to :controller => 'tasks', :action => 'calendar'
elsif @loggedon.default_view == 'Statistics'
redirect_to :controller => 'tasks', :action => 'statistics'
elsif @loggedon.default_view == 'Hot map'
redirect_to :controller => 'tasks', :action => 'matrix'
elsif @loggedon.default_view == 'Collage'
redirect_to :controller => 'tasks', :action => 'collage'
else
redirect_to :controller => 'tasks', :action => 'workload'
end
end
redirect_to(home_path)
end

def search
Expand Down
83 changes: 34 additions & 49 deletions app/controllers/testimonials_controller.rb
@@ -1,75 +1,60 @@
class TestimonialsController < ApplicationController

before_filter :login_required, :only => [:edit, :update]
before_filter :admin_authorised, :only => [:edit, :update]

# GET /testimonials
def index
redirect_to :action => 'list'
@testimonials = Testimonial.approved
end

def list
@testimonials = Testimonial.find(:all, :conditions => "approved = 1", :order => "id desc")

unless session[:person].nil?
if session[:person].status == "Site Creator"
@editaccess = true
else
@editaccess = false
end
end

end


# GET /testimonials/new
def new
@testimonial = Testimonial.new

unless session[:person].nil?
if logged_in?
@testimonial.name = session[:person].name
@testimonial.login_id = session[:person].login
end
end

# POST /testimonials
def create
@testimonial = Testimonial.new(params[:testimonial])
if validate_recap(params, @testimonial.errors) && @testimonial.save
flash[:notice] = 'Thank you. Your testimonial will appear here once approved.'

@email = Email.new
@email.subject = "New MyChores testimonial from " + @testimonial.name + " (" + @testimonial.login_id + ")"
@email.message = @testimonial.message

@email.message += "
Approve it here: "

@email.message += "http://www.mychores.co.uk/testimonials/edit/" + @testimonial.id.to_s

@email.to = "contact@mychores.co.uk"
@email.save

redirect_to :action => 'list'
else
render :action => 'new'
end
if recaptcha_valid?(params, @testimonial)
if @testimonial.save
flash[:notice] = 'Thank you. Your testimonial will appear here once approved.'
@email = Email.new
@email.to = "contact@mychores.co.uk"
@email.subject = "New MyChores testimonial from #{@testimonial.name} (#{@testimonial.login_id})"
@email.message = "#{@testimonial.message}\n\nApprove it here: #{edit_testimonial_url(@testimonial)}"
@email.save
redirect_to(testimonials_path)
else
render :action => 'new'
end
end
end

# GET /testimonials/1/edit
def edit
if session[:person].status == "Site Creator"
@testimonial = Testimonial.find(params[:id])
end
@testimonial = Testimonial.find(params[:id])
end

# PUT /testimonials/1
def update
@testimonial = Testimonial.find(params[:id])
if session[:person].status == "Site Creator"
if @testimonial.update_attributes(params[:testimonial])
flash[:notice] = 'Testimonial was successfully updated.'
redirect_to :action => 'list'
else
render :action => 'edit'
end
if @testimonial.update_attributes(params[:testimonial])
flash[:notice] = 'Testimonial was successfully updated.'
redirect_to testimonials_path
else
render :action => 'edit'
end
end


protected

def recaptcha_valid?(params, testimonial)
return true if %w(development test).include?(RAILS_ENV)
validate_recap(params, testimonial.errors)
end

end
2 changes: 2 additions & 0 deletions app/models/testimonial.rb
Expand Up @@ -2,6 +2,8 @@ class Testimonial < ActiveRecord::Base

validates_presence_of(:message)
validates_presence_of(:name)

default_scope :order => 'id DESC'

named_scope :approved, :conditions => {:approved => true}

Expand Down
18 changes: 6 additions & 12 deletions app/views/layouts/_logged_in_nav.haml
Expand Up @@ -19,18 +19,12 @@

%h3 Views
%ul
%li
%a.picturelink.workload_view{:href => '/tasks/workload'} Workload
%li
%a.picturelink.hotmap_view{:href => '/tasks/matrix'} Hot map
%li
%a.picturelink.calendar_view{:href => '/tasks/calendar'} Calendar
%li
%a.picturelink.collage_view{:href => '/tasks/collage'} Collage
%li
%a.picturelink.statistics_view{:href => '/tasks/statistics'} Statistics
%li
%a.picturelink.print_view{:href => '/tasks/print'} Print outs
%li= link_to('Workload', workload_path, :class => 'picturelink workload_view')
%li= link_to('Hot map', hotmap_path, :class => 'picturelink hotmap_view')
%li= link_to('Calendar', calendar_path, :class => 'picturelink calendar_view')
%li= link_to('Collage', collage_path, :class => 'picturelink collage_view')
%li= link_to('Statistics', my_statistics_path, :class => 'picturelink statistics_view')
%li= link_to('Print outs', print_path, :class => 'picturelink print_view')


%h3 My teams
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.rhtml
Expand Up @@ -84,7 +84,7 @@

<div id="menu">
<ul>
<li class="first"><a href="/home" accesskey="h" title="Home">Home</a></li>
<li class="first"><%= link_to('Home', controller.home_path) %></li>

<% unless @links.nil? %>
<% for link in @links %>
Expand Down
30 changes: 30 additions & 0 deletions app/views/testimonials/_form.haml
@@ -0,0 +1,30 @@
= f.error_messages

%table
%tr
%th= label(:testimonial, :message, 'Testimonial')
%td= text_area(:testimonial, :message, :rows => 12)

- if controller.admin?
%tr
%th= label(:testimonial, :short_message, 'Short version')
%td= text_area(:testimonial, :short_message, :rows => 6)

%tr
%th= label(:testimonial, :name, 'Your name')
%td= text_field(:testimonial, :name, :maxlength => 50, :class => 'styled')
%tr
%th= label(:testimonial, :login_id, 'Login ID')
%td= text_field(:testimonial, :login_id, :maxlength => 40, :class => 'styled')

- if controller.admin?
%tr
%th= label(:testimonial, :approved)
%td= check_box(:testimonial, :approved)
- else
%tr
%th reCAPTCHA
%td= get_captcha()

%br/
%br/
32 changes: 0 additions & 32 deletions app/views/testimonials/_form.rhtml

This file was deleted.

6 changes: 6 additions & 0 deletions app/views/testimonials/edit.haml
@@ -0,0 +1,6 @@
- @heading = 'Edit testimonial'

- form_for(@testimonial) do |f|
= render(:partial => 'form', :locals => {:f => f})

= f.submit('Save')
49 changes: 0 additions & 49 deletions app/views/testimonials/edit.rhtml

This file was deleted.

0 comments on commit d53462a

Please sign in to comment.