Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

add draper gem #14

Merged
merged 1 commit into from Apr 16, 2012
Jump to file or symbol
Failed to load files and symbols.
+67 −37
Split
View
@@ -7,6 +7,8 @@ gem 'haml-rails'
gem 'rdiscount'
gem 'kaminari'
+gem 'draper'
+
gem 'omniauth'
gem 'omniauth-github'
gem 'omniauth-twitter'
View
@@ -85,6 +85,8 @@ GEM
compass
database_cleaner (0.7.1)
diff-lcs (1.1.3)
+ draper (0.11.1)
+ activesupport (>= 2.3.10)
ejs (1.0.0)
erubis (2.7.0)
escape (0.0.4)
@@ -317,6 +319,7 @@ DEPENDENCIES
compass-rails (>= 1.0.0.rc.1)
compass_twitter_bootstrap (>= 2.0.1)
database_cleaner
+ draper
factory_girl_rails
fivemat
foreman
@@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base
private
def current_user
- @current_user ||= User.find(session[:user_id]) if session[:user_id]
+ @current_user ||= UserDecorator.find(session[:user_id]) if session[:user_id]
end
def logged_in?
@@ -21,7 +21,7 @@ def create
def commentable
params.each do |name, value|
if name =~ /(.+)_id$/ and COMMENTABLES.include?($1)
- return $1.classify.constantize.find(value)
+ return "#{$1.classify}Decorator".constantize.find(value)
end
end
nil
@@ -5,7 +5,7 @@ def landing
if logged_in? && current_user.comments_for_records.any?
redirect_to dashboard_path
else
- @records = Record.visible_by(current_user).desc(:created_at).page(1)
+ @records = RecordDecorator.decorate(Record.visible_by(current_user).desc(:created_at).page(1))
end
end
@@ -16,7 +16,7 @@ def opensearch
def dashboard
if logged_in?
- @comments = current_user.comments_for_records(params[:page])
+ @comments = CommentDecorator.decorate(current_user.comments_for_records(params[:page]))
else
redirect_to records_path
end
@@ -9,9 +9,9 @@ class RecordsController < ApplicationController
def index
@query = Record.desc(:created_at).without(:typescript, :timing).visible_by(current_user)
if params[:tags]
- @records = @query.where(:tags.in => params[:tags]).page(params[:page])
+ @records = RecordDecorator.decorate(@query.where(:tags.in => params[:tags]).page(params[:page]))
else
- @records = @query.page(params[:page])
+ @records = RecordDecorator.decorate(@query.page(params[:page]))
end
respond_with @records
end
@@ -80,10 +80,11 @@ def search
def find_record
if params[:access_key].present?
- @record = Record.priv.where(access_key: params[:access_key]).find(params[:id])
+ @record = RecordDecorator.decorate(Record.priv.where(access_key: params[:access_key]).find(params[:id]))
else
- @record = Record.visible_by(current_user).find(params[:id])
+ @record = RecordDecorator.decorate(Record.visible_by(current_user).find(params[:id]))
end
+
rescue Mongoid::Errors::DocumentNotFound
render :no_such_record
end
@@ -6,7 +6,7 @@ class UsersController < ApplicationController
before_filter :find_user, only: [:show, :edit, :update]
def index
- @users = User.desc(:created_at).page(params[:page]).per(10)
+ @users = UserDecorator.decorate(User.desc(:created_at).page(params[:page]).per(10))
end
def show
@@ -68,6 +68,6 @@ def authenticate
private
def find_user
- @user = User.find(params[:id])
+ @user = UserDecorator.find(params[:id])
end
end
@@ -0,0 +1,2 @@
+class ApplicationDecorator < Draper::Base
+end
@@ -0,0 +1,4 @@
+class CommentDecorator < ApplicationDecorator
+ decorates :comment
+ decorates_association :user
+end
@@ -0,0 +1,5 @@
+class RecordDecorator < ApplicationDecorator
+ decorates :record
+ decorates_association :user
+ decorates_association :comments
+end
@@ -0,0 +1,14 @@
+require 'digest/md5'
+
+class UserDecorator < ApplicationDecorator
+ decorates :user
+
+ def avatar_url(size)
+ return "/assets/avatars/anonymous-#{size}.png" if model.nickname == 'Anonymous'
+ if model.email.blank?
+ return "/assets/avatars/default-#{size}.png"
+ else
+ return "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}"
+ end
+ end
+end
View
@@ -32,15 +32,6 @@ class User
before_create :maybe_assign_nickname_placeholder
after_create :generate_api_key!
- def avatar_url(size)
- return "/assets/avatars/anonymous-#{size}.png" if nickname == 'Anonymous'
- if email.blank?
- return "/assets/avatars/default-#{size}.png"
- else
- return "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}"
- end
- end
-
def owner
self
end
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe UserDecorator do
+ before { ApplicationController.new.set_current_view_context }
+
+ context '#avatar_url' do
+ let(:user) { mock_model(User, :nickname => 'nash') }
+
+ it "has anonymous user" do
+ user.stub(:nickname) { 'Anonymous' }
+ described_class.decorate(user).avatar_url(10).should == '/assets/avatars/anonymous-10.png'
+ end
+
+ it "has blank email" do
+ user.stub(:email) { '' }
+ described_class.decorate(user).avatar_url(10).should == '/assets/avatars/default-10.png'
+ end
+
+ it "has some email" do
+ user.stub(:email) { 'zomg@example.com' }
+ digest = Digest::MD5.hexdigest(user.email)
+
+ described_class.decorate(user).avatar_url(10).should == "http://www.gravatar.com/avatar/#{digest}?s=10"
+ end
+ end
+end
View
@@ -52,24 +52,6 @@
end
end
- describe "#avatar_url(size)" do
- context "when user is Anonymous" do
- it "should return anonymous.png" do
- subject.nickname = 'Anonymous'
- subject.avatar_url("100x100")
- .should == '/assets/avatars/anonymous-100x100.png'
- end
- end
-
- context "when email is blank" do
- it "should return default avatar" do
- subject.email = ""
- subject.avatar_url("100x100")
- .should == "/assets/avatars/default-100x100.png"
- end
- end
- end
-
describe "#comments_for_records" do
it "should return comments for record" do
commentable = create(:record, user: subject)