From 136467b9450544ad37e994b49c4422b968419fbf Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Fri, 30 Mar 2012 20:21:40 +0300 Subject: [PATCH] draperify --- Gemfile | 1 + Gemfile.lock | 3 +++ app/controllers/users_controller.rb | 4 +++- app/decorators/application_decorator.rb | 2 ++ app/decorators/author_decorator.rb | 13 +++++++++++++ app/decorators/user_decorator.rb | 4 ++++ app/views/users/show.haml | 8 +------- test/decorators/author_decorator_test.rb | 20 ++++++++++++++++++++ test/test_helper.rb | 2 ++ 9 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 app/decorators/application_decorator.rb create mode 100644 app/decorators/author_decorator.rb create mode 100644 app/decorators/user_decorator.rb create mode 100644 test/decorators/author_decorator_test.rb diff --git a/Gemfile b/Gemfile index 03a335a8..f5568277 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index 11ed9b84..01e24211 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -310,6 +312,7 @@ DEPENDENCIES database_cleaner delayed_job! delayed_job_mongo_mapper! + draper exceptional fabrication haml-rails diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7bf2fae9..63c24575 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/decorators/application_decorator.rb b/app/decorators/application_decorator.rb new file mode 100644 index 00000000..a3a457f2 --- /dev/null +++ b/app/decorators/application_decorator.rb @@ -0,0 +1,2 @@ +class ApplicationDecorator < Draper::Base +end diff --git a/app/decorators/author_decorator.rb b/app/decorators/author_decorator.rb new file mode 100644 index 00000000..229b9373 --- /dev/null +++ b/app/decorators/author_decorator.rb @@ -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 diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb new file mode 100644 index 00000000..eaf86355 --- /dev/null +++ b/app/decorators/user_decorator.rb @@ -0,0 +1,4 @@ +class UserDecorator < ApplicationDecorator + decorates :user + decorates_association :author +end diff --git a/app/views/users/show.haml b/app/views/users/show.haml index 341d5a2e..1d776a74 100644 --- a/app/views/users/show.haml +++ b/app/views/users/show.haml @@ -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 diff --git a/test/decorators/author_decorator_test.rb b/test/decorators/author_decorator_test.rb new file mode 100644 index 00000000..b3d7e133 --- /dev/null +++ b/test/decorators/author_decorator_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 22cb7d8c..5521a7a3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -36,6 +36,8 @@ def setup DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) DatabaseCleaner.start + + ApplicationController.new.set_current_view_context end def teardown