From 3c8ddc1923f5bae136a43f917108fbefb012f894 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 18 Dec 2010 08:31:12 -0500 Subject: [PATCH] Added view count to photos. Used acts_as_markdown for pages. --- Gemfile | 1 + Gemfile.lock | 9 +++++ app/controllers/pages_controller.rb | 3 +- app/controllers/photos_controller.rb | 40 +++++++++++-------- app/models/page.rb | 5 +-- app/views/categories/index.html.erb | 4 +- app/views/pages/about.html.erb | 4 +- app/views/pages/index.html.erb | 2 +- app/views/photos/index.html.erb | 4 +- ...20101216180143_add_view_count_to_photos.rb | 9 +++++ db/schema.rb | 3 +- 11 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 db/migrate/20101216180143_add_view_count_to_photos.rb diff --git a/Gemfile b/Gemfile index 4c614d2..e44c566 100644 --- a/Gemfile +++ b/Gemfile @@ -36,3 +36,4 @@ 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' gem 'rdiscount' +gem 'acts_as_markup' diff --git a/Gemfile.lock b/Gemfile.lock index 550f115..8ecf844 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,6 +20,7 @@ GIT GEM remote: http://rubygems.org/ specs: + RedCloth (4.2.3) abstract (1.0.0) actionmailer (3.0.0) actionpack (= 3.0.0) @@ -47,6 +48,12 @@ GEM activemodel (= 3.0.0) activesupport (= 3.0.0) activesupport (3.0.0) + acts_as_markup (1.3.4) + RedCloth (~> 4.2) + activerecord (>= 2.3.2) + activesupport (>= 2.3.2) + rdiscount (~> 1.3) + wikitext (~> 2.0) arel (1.0.1) activesupport (~> 3.0.0) builder (2.1.2) @@ -90,11 +97,13 @@ GEM treetop (1.4.8) polyglot (>= 0.3.1) tzinfo (0.3.23) + wikitext (2.1.1) PLATFORMS ruby DEPENDENCIES + acts_as_markup exception_notification! meta_where mini_exiftool diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index cc45f9e..39b8aad 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -6,6 +6,7 @@ class PagesController < ApplicationController end def about - @content = Page.find_by_name('about') + @page = Page.find_by_name('about') + @content = @page.content end end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 4e22029..c9a0d57 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -1,22 +1,28 @@ class PhotosController < ApplicationController - def new - @photo = Photo.new - @categories = Category.find(:all).map { |c| [c.name, c.id] } - end + def new + @photo = Photo.new + @categories = Category.find(:all).map { |c| [c.name, c.id] } + end - def index - if params[:category_id] - @category = Category.find_by_id(params[:category_id]) - @photos = @category.photos.enabled.order(:taken_at.desc).paginate(:page => params[:page], :per_page => 11) - else - @photos = Photo.enabled.order(:taken_at.desc).paginate :all, :page => params[:page], :per_page => 11 - end - respond_to do |format| - format.html - end + def index + if params[:category_id] + @category = Category.find_by_id(params[:category_id]) + @photos = @category.photos.enabled.order(:taken_at.desc).paginate(:page => params[:page], :per_page => 11) + else + @photos = Photo.enabled.order(:taken_at.desc).paginate :all, :page => params[:page], :per_page => 11 end + respond_to do |format| + format.html + end + end - def show - @photo = Photo.find(params[:id]) - end + def show + # Log the view + @photo = Photo.find(params[:id]) + @photo.views += 1 + @photo.save + # 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') + end end diff --git a/app/models/page.rb b/app/models/page.rb index 191f5cc..84498a7 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,8 +1,5 @@ require 'rdiscount' class Page < ActiveRecord::Base - def html - markdown = Markdown.new(self.content, :smart) - markdown.to_html - end + acts_as_markdown :content end diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb index 157e71f..0515e4f 100644 --- a/app/views/categories/index.html.erb +++ b/app/views/categories/index.html.erb @@ -16,8 +16,8 @@ <% @photos.each do |photo| %> -
- <%= link_to '', photo.photo.url, :rel => 'photo', :class => 'fancy' %> +
+ <%= link_to '', photo_url(photo, :format => :jpg), :class => 'fancy' %>
<% end %> diff --git a/app/views/pages/about.html.erb b/app/views/pages/about.html.erb index d498da8..67402ea 100644 --- a/app/views/pages/about.html.erb +++ b/app/views/pages/about.html.erb @@ -9,7 +9,7 @@
- <%= raw(@content.html) %> + <%= raw(@content.to_html) %>
@@ -26,4 +26,4 @@ <%= link_to raw('contact'), new_contact_url %>
- \ No newline at end of file + diff --git a/app/views/pages/index.html.erb b/app/views/pages/index.html.erb index e1a5a97..e8249d5 100644 --- a/app/views/pages/index.html.erb +++ b/app/views/pages/index.html.erb @@ -8,7 +8,7 @@ <% if @photo %>
- <%= link_to '', @photo.photo.url, :rel => 'photo', :class => 'fancy' %> + <%= link_to '', photo_url(@photo, :format => :jpg), :rel => 'photo', :class => 'fancy' %>
<% end %> diff --git a/app/views/photos/index.html.erb b/app/views/photos/index.html.erb index add6701..368cfc9 100644 --- a/app/views/photos/index.html.erb +++ b/app/views/photos/index.html.erb @@ -26,8 +26,8 @@ <% @photos.each do |photo| %> -
- <%= link_to '', photo.photo.url, :rel => 'photo', :class => 'fancy' %> +
+ <%= link_to '', photo_url(photo, :format => :jpg), :class => 'fancy' %>
<% end %> diff --git a/db/migrate/20101216180143_add_view_count_to_photos.rb b/db/migrate/20101216180143_add_view_count_to_photos.rb new file mode 100644 index 0000000..4ef78f2 --- /dev/null +++ b/db/migrate/20101216180143_add_view_count_to_photos.rb @@ -0,0 +1,9 @@ +class AddViewCountToPhotos < ActiveRecord::Migration + def self.up + add_column :photos, :views, :integer, :default => 0 + end + + def self.down + remove_column :photos, :views + end +end diff --git a/db/schema.rb b/db/schema.rb index b27690b..c67b931 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20101215172105) do +ActiveRecord::Schema.define(:version => 20101216180143) do create_table "admin_users", :force => true do |t| t.string "first_name", :default => "", :null => false @@ -63,6 +63,7 @@ ActiveRecord::Schema.define(:version => 20101215172105) do t.boolean "featured", :default => false t.boolean "enabled", :default => true t.datetime "taken_at" + t.integer "views", :default => 0 end end