Skip to content

Commit

Permalink
get the student UI to work
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Jul 6, 2010
1 parent 4a54e93 commit 9bbc618
Show file tree
Hide file tree
Showing 45 changed files with 1,373 additions and 52 deletions.
28 changes: 28 additions & 0 deletions app/controllers/contacts_controller.rb
@@ -0,0 +1,28 @@
class ContactsController < PublicController

public
def new
@contact = Contact.new
@contact.listing = Listing.first!(:id => params[:listing_id], :board => @board)
render :template => "public/contact"
end

def create
@contact = Contact.new(params[:contact])
@contact.listing = Listing.first!(:id => params[:listing_id], :board => @board)
if @contact.valid?
# first create the message
email = Mailer.create_contact(@contact)

# sent the message
Mailer.deliver_contact(@contact)

# finally display the message in the page
# after clearing all the visual noise
@message = email.encoded.gsub(/To: .*\n/, '').gsub(/Date: .*\n/, '').gsub(/From: .*\n/, '').gsub(/Subject: .*\n/, '').gsub(/Mime-Version: .*\n/, '').gsub(/Content-Type: .*\n/, '').sub(/\n/, '')
render :template => "public/sent"
else
render :template => "public/contact"
end
end
end
7 changes: 7 additions & 0 deletions app/controllers/public_boards_controller.rb
@@ -0,0 +1,7 @@
class PublicBoardsController < PublicController

# GET /:lang/:venue/:boards
def show
render :template => "public/board"
end
end
75 changes: 75 additions & 0 deletions app/controllers/public_controller.rb
@@ -0,0 +1,75 @@
class PublicController < ActionController::Base

layout "public"

before_filter :public_filter

skip_before_filter :authenticate, :guard, :check_session_expiry

def session_timeout
raise "ADsasd"
end
# p before_filters

def public_filter
@lang, @venue, @board = Venue.retrieve(params[:lang] || language_from_browser, params[:venue], params[:board] || params[:id] || params[:board_id])
if(@venue)
p (params[:board_id].nil? && params[:id].nil?)
p (params[:board] != @board.name && params[:id] != @board.id.to_s && params[:board_id] != @board.id.to_s)
if(params[:lang].nil? || params[:venue].nil? || (params[:id].nil? && params[:board_id].nil?) || params[:lang] != @lang.code || (params[:board] != @board.name && params[:id] != @board.id.to_s && params[:board_id] != @board.id.to_s))
puts "-" * 80
p @lang
p @venue
p @board
redirect_to url_for(:controller => :public_boards,
:action => :show,
:venue => @venue.name,
:id => @board.id,
:lang => @lang.code)
false
elsif(@board.nil? || @lang.nil?)
render_not_found
else
# TODO session tracking for venues with password
@path_prefix = url_for(:controller => :public_boards,
:action => :show,
:venue => @venue.name,
:board => @board.name,
:lang => @lang.code)
true
end
else
# check for php pages
php = File.join(RAILS_ROOT, 'public', params[:venue], "index.php")
if (File.exists?(php))
redirect_to "/#{params[:venue]}/index.php"
false
else
render_not_found
end
end
end

def language_from_browser
#get prefered browser language
#TODO needs find the language from the browser which fits into
#the venue's languages
accept = request.env["HTTP_ACCEPT_LANGUAGE"]
if accept
lang = accept.split(",")[0]
lang = lang[0..1]
end

# if no language could be found, fall back to default
unless lang
lang = 'en' #TODO @venue.default_language
end
lang
end

def render_not_found
render :template => "public/novenue", :layout => "error", :status => 404
false
end

end
104 changes: 104 additions & 0 deletions app/controllers/public_listings_controller.rb
@@ -0,0 +1,104 @@
class PublicListingsController < PublicController

def index
raise Exception.new("forced error to test error page")
end

# GET /listings/new/offer
def offer
@listing = Listing.new
@listing.driver = true
@listing.board = @board

render :template => "public/new"
end

# GET /listings/new/wanted
def wanted
@listing = Listing.new
@listing.driver = false
@listing.board = @board

render :template => "public/new"
end

# GET /listings/1/edit
def edit
@listing = Listing.first!(:id => params[:id], :board => @board)

render :template => "public/removal"
end

# POST /listings/1/reminder
def reminder
@listing = Listing.first!(:id => params[:id], :board => @board)

# @listing.board.venue has no lang_code state set !!!
url = @venue.iframe_url
if url.blank?
if @venue.password
# TODO maybe it is just the other url as well ?!
url = url_for(:controller => :listings,
:action => :edit)
else
url = url_for(:id => @listing.id,
:venue => @venue.name,
:board_id => @board.id,
:lang => @lang.code,
:controller => :public_listings,
:action => :edit)
end
end
logger.debug "listing url: " + url if logger && logger.debug?
Mailer.deliver_remind(@listing, url)
render :template => "public/reminder_sent"
end

# POST /listings
# POST /listings.xml
def create
date = DateTime.civil(params[:listing].delete("ridedate(1i)").to_i, params[:listing].delete("ridedate(2i)").to_i, params[:listing].delete("ridedate(3i)").to_i)
@listing = Listing.new(params[:listing])
@listing.ridedate = date
@listing.board = @board
@listing.reset_password

if !Listing.first(:board_id => @listing.board.id,
:name => @listing.name,
:location => @listing.location,
:email => @listing.email,
:ridedate => @listing.ridedate,
:driver => @listing.driver).nil?
render :template => "public/exists"
elsif @listing.save
url = @venue.iframe_url
if url.blank?
url = url_for(:venue => @venue.name,
:id => @board.id,
:lang => @lang.code,
:controller => :public_boards,
:action => :show)
end

Mailer.deliver_confirm(@listing, url, @lang.code)

render :template => "public/created"
else
render :template => "public/new"
end
end

# DELETE /listings/1
# DELETE /listings/1.xml
def destroy
@listing = Listing.first(:id => params[:id], :board => @board)

if @listing && params[:password] == @listing.password
@listing.destroy
render :template => "public/confirmed"
else
render :template => "public/not_authorized"
end
end

end
11 changes: 11 additions & 0 deletions app/helpers/contacts_helper.rb
@@ -0,0 +1,11 @@
module ContactsHelper

def display(tag, field)
if @contact.errors.invalid? field
"<div class='fieldWithErrors'>#{tag}</div>"
else
tag
end
end

end
24 changes: 24 additions & 0 deletions app/models/board.rb
Expand Up @@ -10,6 +10,11 @@ class Board

modified_by Ixtlan::Models::USER

belongs_to :venue

has n, :board_configs
has n, :listings

require 'dm-serializer'
alias :to_x :to_xml_document
def to_xml_document(opts = {}, doc = nil)
Expand All @@ -19,4 +24,23 @@ def to_xml_document(opts = {}, doc = nil)
to_x(opts, doc)
end

def locale= (locale, create = false)
@lang = BoardConfig.first(:board => self, :locale => locacle)

# create a new one if needed and wanted
@lang = BoardConfig.create(:locale => locale, :board => self) if @lang.nil? && create
end

#create delegate methods
%w(directions_url map_url).each do |name|
class_eval <<-CODE, __FILE__, __LINE__
def #{name}
@lang.#{name} if @lang
end
def #{name}?
!(@lang && @lang.#{name}).blank?
end
CODE
end
end
12 changes: 8 additions & 4 deletions app/models/board_config.rb
Expand Up @@ -2,10 +2,14 @@ class BoardConfig
include DataMapper::Resource

property :id, Serial
property :board_id, Integer, :required => true
property :locale_id, Integer, :required => true
property :map_url, String, :required => true, :format => /^[^<'&">]*$/, :length => 255
property :directions_url, String, :required => true, :format => /^[^<'&">]*$/, :length => 255
property :map_url, String, :required => false, :format => /#{VenueConfig::URL_PATTERN}/, :length => 255, :message => VenueConfig::FORMAT_MESSAGE
property :directions_url, String, :required => false, :format => /#{VenueConfig::URL_PATTERN}/, :length => 255, :message => VenueConfig::FORMAT_MESSAGE

#property :board_id, Integer, :required => true
#property :locale_id, Integer, :required => true
belongs_to :locale
belongs_to :board

timestamps :at

modified_by Ixtlan::Models::USER
Expand Down
61 changes: 61 additions & 0 deletions app/models/contact.rb
@@ -0,0 +1,61 @@
class Contact
attr_accessor :name, :email, :phone

def model
self.class
end

def key
name
end

def initialize(contact = nil)
if contact
self.name = contact[:name]
self.email = contact[:email]
self.phone = contact[:phone]
@my_listing_id = contact[:listing_id]
@my_listing = contact[:listing]
end
@my_errors = { }
end

def valid?
validate
@my_errors.size == 0
end

def errors
def @my_errors.invalid?(attr)
self[attr.to_sym] || false
end
def @my_errors.count
self.size
end
def @my_errors.full_messages
self.values
end
@my_errors
end

def validate
@my_errors[:listing] = "listing cannot be blank" if (@my_listing == nil && @my_listing_id == nil)
@my_errors[:name] = "Please fill in your name" if self.name == nil
@my_errors[:name] = "Please fill in a valid name" unless (self.name =~ /^\w+$/)
@my_errors[:phone] = "Please fill in a valid phone number" if (self.phone && !self.phone =~ /^[-0-9() +]+$/)
@my_errors[:email] = "Please fill in a valid email address" unless self.email =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/
end

def listing_id
listing.id
end

def listing=(listing)
@my_listing = listing
end

def listing
@my_listing = Listing.find(@my_listing_id) if @my_listing == nil and @my_listing_id != nil
@my_listing
end
end
17 changes: 16 additions & 1 deletion app/models/listing.rb
Expand Up @@ -10,7 +10,9 @@ class Listing
property :password, String, :required => true, :format => /^[^<'&">]*$/, :length => 255
timestamps :at

modified_by Ixtlan::Models::USER
# modified_by Ixtlan::Models::USER

belongs_to :board

require 'dm-serializer'
alias :to_x :to_xml_document
Expand All @@ -21,4 +23,17 @@ def to_xml_document(opts = {}, doc = nil)
to_x(opts, doc)
end

def reset_password
if(email == "test@rides.server.dhamma.org")
self.password = "31415"
else
self.password = Ixtlan::Passwords.generate(5)
end
end

def self.first!(*args)
result = self.first(*args)
raise DataMapper::ObjectNotFoundError.new("Listing" + args.inspect) if result.nil?
result
end
end

0 comments on commit 9bbc618

Please sign in to comment.