Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 867886dbff
Fetching contributors…

Cannot retrieve contributors at this time

file 204 lines (161 sloc) 4.897 kb
1 2 3 4 5 6 7 8 9 10 11 12 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
# encoding: utf-8
require "sinatra/base"
# require 'sinatra/cache'
require 'date'
require 'haml'
require 'sass'
require 'ostruct'
require 'haml-coderay'


ENV["RACK_ENV"] = "development" unless ENV["RACK_ENV"]
RACK_ENV = ENV["RACK_ENV"]

%w(speaker talk).each do |model|
  require File.expand_path(File.join(File.dirname(__FILE__), "lib/#{model}.rb"))
end

module RubyConf
  class Website < Sinatra::Application
    
    CONFIG = OpenStruct.new(YAML.load(ERB.new(File.read("#{Dir.pwd}/config/settings.yml")).result)[RACK_ENV])
    LANGUAGES = CONFIG.languages
    
    def self.check_language!
      condition { LANGUAGES.keys.include?(params[:lang]) }
    end

    def self.page(path, &block)
      path = "/" + path.gsub(/^\//, '')

      get path do
        redirect "#{language}#{path}"
      end

      get "/:lang#{path}", &block
    end

    set :root, Dir.pwd
    set :public_folder, File.expand_path("../public", __FILE__)
    set :haml, :format => :html5
    set :logging, :true
    enable :sessions
    # register(Sinatra::Cache)
    # set :cache_enabled, true
    
    # configure do
    # DataMapper::Logger.new($stdout, :debug) if RACK_ENV == "development"
    # DataMapper.setup(:default, CONFIG.database)
    # DataMapper.auto_upgrade!
    # end

    helpers do
      
      def full_price?
        DateTime.now >= DateTime.parse("2011/10/1 0:0:0")
      end
      
      def partial(page, options={})
        haml page, options.merge!(:layout => false)
      end

      def flashes
        [:notice, :warning, :error].each do |key|
          haml_tag(:div, session.delete(key), :class => "flash #{key}") if session.has_key?(key)
        end
      end
      def simple_format(text)
        return "" if text.nil?
        "<p>#{text.gsub("\n", "</p><p>")}</p>"
      end
      alias flash session
    end

    get "/" do
      redirect language
    end

    get '/website.css' do
      sass :website
    end
    
    get "/drinkups" do
      haml :drinkups, :skip_translation => true
    end

    check_language!
    get "/:lang" do |lang|
      haml :home
    end

    post "/register" do
      @registration = Registration.new params[:registration]

      if @registration.save
        flash[:notice] = "You have successfully pre-registered!"
        redirect "/"
      else
        haml :home
      end
    end

    page "proposals" do
      @proposal = Proposal.new
      haml :proposals
    end

    post '/proposals' do
      @proposal = Proposal.new params[:proposal]

      if @proposal.save
        flash[:notice] = "You have successfully sent your proposal!"
        redirect "/"
      else
        haml :proposals
      end
    end

    page "/rubyfunday" do
      haml :rubyfunday
    end

    page "/agenda" do
      haml :agenda
    end

    page "registration" do
      flash[:notice] = "Please, fill the 'Keep me posted' form. "
      redirect "/"
    end
    
    page "speakers" do
      haml :speakers
    end

    page "sponsoring" do
      haml :sponsoring
    end

    page "event" do
      haml :event
    end

    page "event/now" do
      @now, @next = Talk.find_current_and_upcoming
      haml :now
    end

    def language
      @lang ||= params[:lang] || language_from_http || "en"
    end

    def current_path(options = {})
      request.url.gsub(/\/(#{language})/, "/#{options.fetch(:lang, language)}")
    end

    def language_from_http
      env["HTTP_ACCEPT_LANGUAGE"].to_s.split(",").each do |lang|
        %w(en es).each {|code| return code if lang =~ /^#{code}/ }
      end
      nil
    end

    def speaker_twitter(user)
      "Twitter: " + link_to("@#{user}", "http://twitter.com/#{user}")
    end

    def twitter(text="Seguinos en twitter", user="RubyConfAr")
      link_to text, "http://twitter.com/#{user}", :class => "twitter", :target => 'blank'
    end

    def email(text="envíennos un email", address="info@rubyconfuruguay.org")
      link_to text, "mailto:#{address}", :class => "email"
    end

    def active_link?(route)
      request.path == route
    end

    def link_to(text, url=nil, options={}, &block)
      url, text = text, capture_haml(&block) if url.nil?
      options.merge!(:class => 'active') if active_link?(url)
      capture_haml do
        haml_tag :a, text, options.merge(:href => url)
      end
    end

    def pluralize(count, singular, plural = nil)
      "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
    end

    def haml(template_or_code, options={}, &block)
      layout = options.has_key?(:layout) ? options.delete(:layout) : :layout
      options[:layout] = :"#{layout}_#{language}" if layout

      skip_translation = options.delete(:skip_translation)

      if Symbol === template_or_code && !skip_translation
        super(:"#{template_or_code}_#{language}", options, &block)
      else
        super
      end
    end
  end
end
Something went wrong with that request. Please try again.