Skip to content
This repository was archived by the owner on Mar 13, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/controllers/proposals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'uri'

class ProposalsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]

Expand All @@ -16,6 +18,8 @@ def show

def new
@proposal = Proposal.new
title = params[:title]
@proposal.title = URI.unescape title if title
end

def create
Expand All @@ -36,7 +40,7 @@ def update
if @proposal.update_attributes(params[:proposal])
redirect_to proposal_path(@proposal)
else
render :edit
render :edit
end
end

Expand Down
10 changes: 10 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'redcarpet'
require 'uri'

module ApplicationHelper
def render_page_title
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion public/stylesheets/vestibule.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,6 @@ header .background li {
#selections form,
#available_selections form {
float: left;
}
}

.New {color:black;}
39 changes: 39 additions & 0 deletions test/unit/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @nikname markup is more problematic than [[this kind]] I think, as I've explored it, because it's much more likely that someone might choose text for a link with @... inside. A clash with an existing Vestibule user nickname would be bad luck, admittedly, but the preprocessing (before any Markdown) is very simplistic at present.

[[Ruby variables are @hlame imho]] is a bit contrived but would create an anchor link within an anchor link, as will similar text for an attempted Markdown link.

I'd want to write the tests for this and plug the hole to a reasonable degree - or back out of the @hlame idiom I think. But the bigger picture of Ruby Manor logistics is now clearer to me in any case. Will come back to this in some form.

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