Skip to content

Commit

Permalink
Write a decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-huang committed Mar 11, 2014
1 parent 3171edd commit 332f22a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem 'mini_magick'
gem 'fog'
gem 'stripe'
gem 'figaro'
gem 'draper'

group :development do
gem 'sqlite3'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ GEM
database_cleaner (1.2.0)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
draper (1.3.0)
actionpack (>= 3.0)
activemodel (>= 3.0)
activesupport (>= 3.0)
request_store (~> 1.0.3)
erubis (2.7.0)
eventmachine (1.0.0)
excon (0.31.0)
Expand Down Expand Up @@ -159,6 +164,7 @@ GEM
redis (3.0.7)
redis-namespace (1.4.1)
redis (~> 3.0.4)
request_store (1.0.5)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec-core (2.14.7)
Expand Down Expand Up @@ -251,6 +257,7 @@ DEPENDENCIES
carrierwave
coffee-rails
database_cleaner
draper
fabrication
faker
figaro
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
end

def show
@video = Video.find(params[:id])
@video = VideoDecorator.decorate(Video.find(params[:id]))
@reviews = @video.reviews
end

Expand Down
8 changes: 8 additions & 0 deletions app/decorators/video_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class VideoDecorator < Draper::Decorator

delegate_all

def rating
object.rating.present? ? "#{object.rating}/5.0" : "N/A"
end
end
4 changes: 4 additions & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ def self.search_by_title(search_term)
return [] if search_term.blank?
where("title LIKE ?", "%#{search_term}%").order("created_at DESC")
end

def rating
reviews.average(:rating).round(1) if reviews.average(:rating)
end
end
3 changes: 2 additions & 1 deletion app/views/videos/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
.video_info.col-sm-3
%header
%h3 #{@video.title}
%span Rating: 4.5/5.0
%span Rating:
= @video.rating
%p #{@video.description}
.actions
%a.btn.btn-primary(href="#{@video.video_url}") Watch Now
Expand Down

0 comments on commit 332f22a

Please sign in to comment.