Skip to content

Commit

Permalink
draperify
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed Mar 30, 2012
1 parent f637e01 commit 136467b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem "tzinfo"
gem "rsa"
gem "exceptional"
gem "newrelic_rpm"
gem "draper"

# background job queue
gem "delayed_job", :git => "git://github.com/collectiveidea/delayed_job.git", :tag => "v2.1.4"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ GEM
crack (0.1.8)
daemons (1.1.4)
database_cleaner (0.6.7)
draper (0.11.1)
activesupport (>= 2.3.10)
erubis (2.7.0)
exceptional (2.0.32)
rack
Expand Down Expand Up @@ -310,6 +312,7 @@ DEPENDENCIES
database_cleaner
delayed_job!
delayed_job_mongo_mapper!
draper
exceptional
fabrication
haml-rails
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ def reset_password_with_token
private

def find_user
@user = User.find_by_case_insensitive_username(params[:id])
if @user = User.find_by_case_insensitive_username(params[:id])
@user = UserDecorator.decorate(@user)
end
end
end
2 changes: 2 additions & 0 deletions app/decorators/application_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationDecorator < Draper::Base
end
13 changes: 13 additions & 0 deletions app/decorators/author_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AuthorDecorator < ApplicationDecorator
decorates :author

def website_url
url = if model.website[0,7] == "http://" or model.website[0,8] == "https://"
model.website
else
"http://#{model.website}"
end

h.link_to(url, url, :rel => 'me', :class => 'url')
end
end
4 changes: 4 additions & 0 deletions app/decorators/user_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserDecorator < ApplicationDecorator
decorates :user
decorates_association :author
end
8 changes: 1 addition & 7 deletions app/views/users/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@
.info
- unless @author.website.blank?
.website
- web_url = ""
- if @author.website[0,7] == "http://" or @author.website[0,8] == "https://"
- web_url = @author.website
- else
- web_url = "http://#{@author.website}"
%a.url{:rel => "me", :href => web_url}
= web_url
= @author.website_url

%p.note= @author.bio

Expand Down
20 changes: 20 additions & 0 deletions test/decorators/author_decorator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative '../test_helper'

describe AuthorDecorator do
include TestHelper

describe '#website_url' do
before do
@author = AuthorDecorator.decorate(Fabricate(:author))
end

it 'returns link to authors website' do
assert_match 'http://example.com', @author.website_url
end

it 'returns link to authors website when website is without http prefix' do
@author.website = 'test.com'
assert_match 'http://test.com', @author.website_url
end
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def setup
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.start

ApplicationController.new.set_current_view_context
end

def teardown
Expand Down

0 comments on commit 136467b

Please sign in to comment.