From 85cf2fb4b8c23d6c0642bd0f217b80b94d6568c7 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Tue, 14 Dec 2010 12:51:34 -0500 Subject: [PATCH] Updated photos model to use named scopes. Cleaned up some code. Added meta_where as a gem, although I'm not using it yet. --- Gemfile | 1 + Gemfile.lock | 5 +++++ app/controllers/categories_controller.rb | 4 +--- app/controllers/pages_controller.rb | 2 +- app/controllers/photos_controller.rb | 6 ++---- app/models/photo.rb | 7 +++++++ app/views/categories/index.html.erb | 2 +- app/views/photos/index.html.erb | 2 +- 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index c516d89..4c614d2 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem 'sqlite3-ruby', :require => 'sqlite3' gem "exception_notification", :git => "git://github.com/rails/exception_notification", :require => 'exception_notifier' gem 'pg' +gem 'meta_where' gem 'typus', :git => 'git://github.com/fesplugas/typus.git' gem 'mini_exiftool' gem 'will_paginate', :git => 'http://github.com/mislav/will_paginate.git', :branch => 'rails3' diff --git a/Gemfile.lock b/Gemfile.lock index 18aba73..550f115 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,6 +57,10 @@ GEM activesupport (>= 2.3.6) mime-types treetop (>= 1.4.5) + meta_where (0.9.6) + activerecord (~> 3.0.0) + activesupport (~> 3.0.0) + arel (~> 1.0.1) mime-types (1.16) mini_exiftool (1.0.1) pg (0.9.0) @@ -92,6 +96,7 @@ PLATFORMS DEPENDENCIES exception_notification! + meta_where mini_exiftool pg rails (= 3.0.0) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index def87a0..d847a2f 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -5,9 +5,7 @@ class CategoriesController < ApplicationController def index @categories = Category.order('sort ASC').paginate :all, :page => params[:page], :per_page => 4 - @num_categories = @categories.count - - @photos = Photo.where(:enabled => true).where(:featured => true).limit(2).order('RANDOM()') + @photos = Photo.featured.limit(2).order('RANDOM()') respond_to do |format| format.html # index.html.erb diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index a70964f..cc45f9e 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -2,7 +2,7 @@ class PagesController < ApplicationController layout "photos" def index - @photo = Photo.where(:featured => true).where(:enabled => true).order('RANDOM()').first + @photo = Photo.featured.order('RANDOM()').first end def about diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 7ae5be4..ffd58d1 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -7,11 +7,9 @@ class PhotosController < ApplicationController def index if params[:category_id] @category = Category.find_by_id(params[:category_id]) - @photos = @category.photos.where(:enabled => true).paginate(:page => params[:page], :per_page => 11) - @num_photos = @photos.count + @photos = @category.photos.enabled.paginate(:page => params[:page], :per_page => 11) else - @photos = Photo.paginate :all, :page => params[:page], :per_page => 11 - @num_photos = @photos.count + @photos = Photo.enabled.paginate :all, :page => params[:page], :per_page => 11 end respond_to do |format| format.html diff --git a/app/models/photo.rb b/app/models/photo.rb index 47951c8..69c1b52 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -13,6 +13,13 @@ class Photo < ActiveRecord::Base after_create :get_exif @@per_page = 11 + + scope :enabled, lambda { + where(:enabled => true) + } + scope :featured, lambda { + enabled.where(:featured => true) + } def to_s self.title diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb index ea8d3c9..157e71f 100644 --- a/app/views/categories/index.html.erb +++ b/app/views/categories/index.html.erb @@ -10,7 +10,7 @@ -<% @num_blank = 4 - @num_categories -%> +<% @num_blank = 4 - @categories.count -%>
diff --git a/app/views/photos/index.html.erb b/app/views/photos/index.html.erb index 09d49b9..add6701 100644 --- a/app/views/photos/index.html.erb +++ b/app/views/photos/index.html.erb @@ -22,7 +22,7 @@
<% end %> -<% @num_blank = 11 - @num_photos -%> +<% @num_blank = 11 - @photos.count -%> <% @photos.each do |photo| %>