From 16f989504843dd792b742a599e0cc38c4bd131dd Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 18 Dec 2010 08:55:17 -0500 Subject: [PATCH] Fixed problem where views could be null. Moved view increment to model. --- app/controllers/photos_controller.rb | 3 +-- app/models/photo.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index c9a0d57..e700f6e 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -19,8 +19,7 @@ class PhotosController < ApplicationController def show # Log the view @photo = Photo.find(params[:id]) - @photo.views += 1 - @photo.save + @photo.log_view # Get the image and send it to the browser data = File.open(@photo.photo.path, 'rb').read send_data(data , :filename => 'photo', :type => 'image/jpg', :disposition => 'inline') diff --git a/app/models/photo.rb b/app/models/photo.rb index e0131e2..0844b25 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -29,6 +29,15 @@ class Photo < ActiveRecord::Base self.title end + def log_view + if self.views.nil? + self.views = 1 + else + self.views += 1 + end + self.save + end + private def get_exif exif = MiniExiftool.new photo.queued_for_write[:original].path