diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 66545a6..7198718 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -1,3 +1,5 @@ +require 'uri' + class ProposalsController < ApplicationController before_filter :authenticate_user!, :except => [:index, :show] @@ -16,6 +18,8 @@ def show def new @proposal = Proposal.new + title = params[:title] + @proposal.title = URI.unescape title if title end def create @@ -36,7 +40,7 @@ def update if @proposal.update_attributes(params[:proposal]) redirect_to proposal_path(@proposal) else - render :edit + render :edit end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fe2f888..01d2d48 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,5 @@ require 'redcarpet' +require 'uri' module ApplicationHelper def render_page_title @@ -32,8 +33,17 @@ def avatar_url(user, bigger=false) end end + def wikiize(text) + names = {} + User.all.each {|user| names[user.name] = link_to(user.name, user) } + Proposal.all.each {|proposal| names[proposal.title] = link_to(proposal.title, proposal) } + text.gsub!(/\[\[(.+?)\]\]/) {|s| names[$1] || link_to($1, {:controller => "proposals", :action => "new", :title => URI.escape($1)}, :class => "New") } + text.gsub(/(^|\W)(@(.+?))(\W|$)/) {|s| user = User.find_by_twitter_nickname $3; $1 + (user ? link_to($2, user) : $2) + $4 } + end + def markdown(text) if text + text = wikiize(text) markdown_parser.render(text).html_safe else nil diff --git a/db/seeds.rb b/db/seeds.rb index 76909c2..1da96e0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,8 +6,8 @@ # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) # Mayor.create(:name => 'Daley', :city => cities.first) -def user(name) - User.create!(:name => name, :twitter_nickname => name) +def user(name, nickname=nil) + User.create!(:name => name, :twitter_nickname => nickname || name) end def suggestions_for(proposal, suggestions) @@ -19,10 +19,10 @@ def suggestions_for(proposal, suggestions) end end -alice = user("alice") +alice = user("Alice", "alice") bob = user("bob") -charlie = user("charlie") -daniel = user("daniel") +charlie = user("Charlie Parker", "charlie") +daniel = user("Daniel Boone", "daniel") a = alice.proposals.create :title => "Fake it till you make it", :description => <<-EOS I've written a few things recently which work against 3rd party HTTP API's recently. diff --git a/public/stylesheets/vestibule.css b/public/stylesheets/vestibule.css index 338ffe9..cc69e08 100644 --- a/public/stylesheets/vestibule.css +++ b/public/stylesheets/vestibule.css @@ -123,4 +123,6 @@ header .background li { #selections form, #available_selections form { float: left; -} \ No newline at end of file +} + +.New {color:black;} \ No newline at end of file diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 595fd91..72b0bfc 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -22,4 +22,43 @@ class ApplicationHelperTest < ActionView::TestCase end end end + + context "wiki" do + context "for a single user" do + setup do + @user = Factory(:user, :twitter_nickname => "rdrake98", :name => "Richard Drake") + @long = link_to("Richard Drake", "/users/rdrake98") + @short = link_to("@rdrake98", "/users/rdrake98") + end + + should "not turn into link" do + assert_equal "[[]]", wikiize("[[]]") + # what about multiple lines inside [[...]]? + assert_equal "@other", wikiize("@other") + end + + should "turn into link" do + assert_equal @long, wikiize("[[Richard Drake]]") + assert_equal @short, wikiize("@rdrake98") + assert_equal "#{@long}same as #{@short}; #{@short} is but not @other.", wikiize("[[Richard Drake]]same as @rdrake98; @rdrake98 is but not @other.") + assert_equal "#{@long}not same as@rdrake98; #{@short} is.", wikiize("[[Richard Drake]]not same as@rdrake98; @rdrake98 is.") + # the following ain't perfect but we can probably live with it + assert_equal "#{@long}same as #{@short};@rdrake98 isn't.", wikiize("[[Richard Drake]]same as @rdrake98;@rdrake98 isn't.") + end + end + + context "for a single proposal" do + setup do + @user = Factory(:proposal, :title => "My One and Only") + @my_only = link_to("My One and Only", "/proposals/1") + @her_latest = link_to("Her Latest Idea", "/proposals/new?title=Her%2520Latest%2520Idea", :class => "New") + end + + should "turn into link" do + assert_equal @my_only, wikiize("[[My One and Only]]") + assert_equal @her_latest, wikiize("[[Her Latest Idea]]") + assert_equal "I prefer #{@her_latest} to #{@my_only}.", wikiize("I prefer [[Her Latest Idea]] to [[My One and Only]].") + end + end + end end