Skip to content

Commit

Permalink
paperclip uploading for photos and user avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Feb 8, 2011
1 parent 513ebe3 commit 95284e7
Show file tree
Hide file tree
Showing 37 changed files with 781 additions and 552 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugin's website.


The MIT license:
Copyright (c) 2008 Curbly, LLC
Copyright (c) 2011 Curbly, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ begin
gem.add_dependency 'rack', '1.2.1'
gem.add_dependency 'configatron'
gem.add_dependency 'hpricot'
gem.add_dependency 'rmagick'
gem.add_dependency 'htmlentities'
gem.add_dependency 'haml'
gem.add_dependency 'calendar_date_select'
Expand Down
41 changes: 10 additions & 31 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'pp'
require 'pp'

class PhotosController < BaseController
include Viewable
before_filter :login_required, :only => [:new, :edit, :update, :destroy, :create, :swfupload]
before_filter :find_user, :only => [:new, :edit, :index, :show, :slideshow, :swfupload]
before_filter :require_current_user, :only => [:new, :edit, :update, :destroy, :swfupload]
before_filter :find_user, :only => [:new, :edit, :index, :show]
before_filter :require_current_user, :only => [:new, :edit, :update, :destroy]

skip_before_filter :verify_authenticity_token, :only => [:create] #because the TinyMCE image uploader can't provide the auth token

Expand All @@ -23,14 +23,13 @@ def recent
def index
@user = User.find(params[:user_id])

cond = Caboose::EZ::Condition.new
cond.user_id == @user.id
@photos = Photo.where :user_id => @user.id
if params[:tag_name]
cond.append ['tags.name = ?', params[:tag_name]]
@photos = @photos.where('tags.name = ?', params[:tag_name])
end

@photos = Photo.recent.find(:all, :conditions => cond.to_sql, :include => :tags, :page => {:current => params[:page], :size => 30})

@photos = @photos.includes(:tags).recent.paginate(:page => params[:page])
@tags = Photo.tag_counts :conditions => { :user_id => @user.id }, :limit => 20

@rss_title = "#{configatron.community_name}: #{@user.login}'s photos"
Expand All @@ -54,10 +53,9 @@ def index
def manage_photos
if logged_in?
@user = current_user
cond = Caboose::EZ::Condition.new
cond.user_id == @user.id
@photos = Photo.where :user_id => @user.id
if params[:tag_name]
cond.append ['tags.name = ?', params[:tag_name]]
@photos = @photos.where('tags.name = ?', params[:tag_name])
end

@selected = params[:photo_id]
Expand Down Expand Up @@ -117,8 +115,6 @@ def create

respond_to do |format|
if @photo.save
#start the garbage collector
GC.start
flash[:notice] = :photo_was_successfully_created.l

format.html {
Expand Down Expand Up @@ -152,19 +148,6 @@ def create
end
end

def swfupload
# swfupload action set in routes.rb
@photo = Photo.new :uploaded_data => params[:Filedata]
@photo.user = current_user
@photo.album_id = params[:album_id] if params[:album_id]
@photo.album_id = params[:album_selected] unless params[:album_selected].blank?
@photo.save!

# This returns the thumbnail url for handlers.js to use to display the thumbnail
render :text => @photo.photo.url(:thumb)
rescue
render :text => "Error: #{$!}", :status => 500
end

# PUT /photos/1
# PUT /photos/1.xml
Expand Down Expand Up @@ -200,10 +183,6 @@ def destroy
end
end

def slideshow
@xml_file = user_photos_url( {:user_id => @user, :format => :xml}.merge(:tag_name => params[:tag_name]) )
render :action => 'slideshow'
end

protected

Expand Down
9 changes: 5 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class UsersController < BaseController
include Viewable
cache_sweeper :taggable_sweeper, :only => [:activate, :update, :destroy]
Expand Down Expand Up @@ -114,9 +113,9 @@ def create
def edit
@metro_areas, @states = setup_locations_for(@user)

@skills = Skill.find(:all)
@offering = Offering.new
@avatar = Photo.new
@skills = Skill.find(:all)
@offering = Offering.new
@avatar = (@user.avatar || @user.build_avatar)
end

def update
Expand All @@ -132,6 +131,8 @@ def update

@user.tag_list = params[:tag_list] || ''

params[:user][:avatar_attributes].merge!(:user_id => @user.id) if params[:user] && params[:user][:avatar_attributes]

if @user.update_attributes(params[:user])
@user.track_activity(:updated_profile)

Expand Down
2 changes: 1 addition & 1 deletion app/models/clipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def owner
end

def image_uri(size = '')
image && image.clipping_image_file.url(size) || image_url
image && image.asset.url(size) || image_url
end

def title_for_rss
Expand Down
11 changes: 4 additions & 7 deletions app/models/clipping_image.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
class ClippingImage < Asset
include UrlUpload
has_attached_file :clipping_image_file, default_s3_options.merge(
:storage => :s3,
:styles => { :original => '465>', :thumb => "100x100#", :medium_square => "200x200#", :medium => "200>" },
:path => "/:attachment/:id/:basename:maybe_style.:extension")
validates_attachment_presence :clipping_image_file
validates_attachment_content_type :clipping_image_file, :content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png']
validates_attachment_size :clipping_image_file, :less_than => 3.megabytes
has_attached_file :asset, configatron.clipping.paperclip_options
validates_attachment_presence :asset
validates_attachment_content_type :photo, :content_type => configatron.clipping.validation_options.content_type
validates_attachment_size :photo, :less_than => configatron.clipping.validation_options.max_size.to_i.megabytes
end
6 changes: 3 additions & 3 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Photo < ActiveRecord::Base
acts_as_commentable
belongs_to :album

has_attached_file :photo, configatron.photo.paperclip_options
has_attached_file :photo, configatron.photo.paperclip_options.to_hash
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => configatron.photo.validation_options.content_type
validates_attachment_size :photo, :less_than => configatron.photo.validation_options.max_size.to_i.megabytes
Expand All @@ -17,15 +17,15 @@ class Photo < ActiveRecord::Base
validates_presence_of :user

belongs_to :user
has_one :user_as_avatar, :class_name => "User", :foreign_key => "avatar_id"
has_one :user_as_avatar, :class_name => "User", :foreign_key => "avatar_id", :inverse_of => :avatar

#Named scopes
scope :recent, :order => "photos.created_at DESC"
scope :new_this_week, :order => "photos.created_at DESC", :conditions => ["photos.created_at > ?", 7.days.ago.to_s(:db)]
scope :tagged_with, lambda {|tag_name|
{:conditions => ["tags.name = ?", tag_name], :include => :tags}
}
attr_accessible :name, :description, :photo, :crop_x, :crop_y, :crop_w, :crop_h
attr_accessible :name, :description, :photo, :crop_x, :crop_y, :crop_w, :crop_h, :user_id

def display_name
(self.name && self.name.length>0) ? self.name : "#{:created_at.l.downcase}: #{I18n.l(self.created_at, :format => :published_date)}"
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class User < ActiveRecord::Base
has_many :monitorships, :dependent => :destroy
has_many :monitored_topics, :through => :monitorships, :conditions => ['monitorships.active = ?', true], :order => 'topics.replied_at desc', :source => :topic

belongs_to :avatar, :class_name => "Photo", :foreign_key => "avatar_id"
belongs_to :avatar, :class_name => "Photo", :foreign_key => "avatar_id", :inverse_of => :user_as_avatar
belongs_to :metro_area
belongs_to :state
belongs_to :country
Expand Down
43 changes: 17 additions & 26 deletions app/views/photos/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
.yui-b
= render :partial => 'posts/author_profile', :locals => {:user => @user}

-box :id => 'tag_list', :class => 'yui-u first' do
%h3=:tags.l
%ul.tags
- @tags.each do |tag|
%li
- if (tag.name.eql?(params[:tag_name]) )
= link_to tag.name, user_photos_path(@user), :class => "selected"
- else
= link_to tag.name, user_photos_path(:user_id => @user, :tag_name => tag.name)
- @tags.each do |tag|
%li
- if (tag.name.eql?(params[:tag_name]) )
= link_to tag.name, user_photos_path(@user), :class => "selected"
- else
= link_to tag.name, user_photos_path(:user_id => @user, :tag_name => tag.name)


#yui-main
.yui-b
.yui-gd
=box :id => 'tag_list', :class => 'yui-u first' do
%h3=:tags.l
%ul.tags
- @tags.each do |tag|
%li
- if (tag.name.eql?(params[:tag_name]) )
= link_to tag.name, user_photos_path(@user), :class => "selected"
- else
= link_to tag.name, user_photos_path(:user_id => @user, :tag_name => tag.name)

=box :id => "photos", :class => "yui-u" do
%h3= link_to :users_photos.l(:user=>@user.login), user_photos_path(@user)
= link_to( :new_photo.l, new_user_photo_path(@user), {:class => 'right'}) if @is_current_user

.clear
- @photos.each do |photo|
= link_to image_tag( photo.photo.url(:thumb), :class => "polaroid"), user_photo_path(@user, photo)

.pagination=paginating_links @photos if @photos.total_pages > 1

-box :id => "photos" do
%h3= link_to :users_photos.l(:user=>@user.login), user_photos_path(@user)
= link_to( :new_photo.l, new_user_photo_path(@user), {:class => 'right'}) if @is_current_user

.clear
- @photos.each do |photo|
= link_to tag('img', :src => photo.photo.url(:thumb), :class => "polaroid"), user_photo_path(@user, photo)

.pagination=paginating_links @photos if @photos.total_pages > 1


Loading

0 comments on commit 95284e7

Please sign in to comment.