Commit f37a3036

mo k <mo@mokhan.ca>
2012-10-11 13:15:35
use :image on Creation as a temporary cache to do minimal work when uploading a new creation.
1 parent 9d80787
Changed files (4)
app/controllers/creations_controller.rb
@@ -25,8 +25,7 @@ class CreationsController < ApplicationController
   def create
     @creation = current_user.creations.create(params[:creation])
     @creation.category_ids = params[:creation][:category_ids] ||= []
-    @creation.photos.build({:creation => @creation, :image => params[:photo], :is_primary => true})
-
+    @creation.delay.migrate_primary_image
     if @creation.save
       redirect_to(creations_url, :notice => 'Thank you for sharing your creation.') 
     else
app/models/creation.rb
@@ -1,7 +1,7 @@
 class Creation < ActiveRecord::Base
   acts_as_commentable
   validates :name,  :presence => true
-  attr_accessible :user_id, :story, :name, :category_ids, :is_restricted, :watermark
+  attr_accessible :user_id, :story, :name, :category_ids, :is_restricted, :watermark, :image
   belongs_to :user
   has_and_belongs_to_many :categories, :join_table => 'creations_categories', :uniq => true, :autosave => true
   has_many :photos, :dependent => :destroy
@@ -35,12 +35,10 @@ class Creation < ActiveRecord::Base
   end
 
   def migrate_primary_image
-      puts "processing #{name}"
       photo = photos.build({:is_primary => true})
       photo.created_at = created_at
       photo.updated_at = updated_at
       photo.image = image.file
       photo.save!
-      puts "migrated #{image.url} to #{photo.attributes}"
   end
 end
app/uploaders/image_uploader.rb
@@ -3,55 +3,13 @@ class ImageUploader < CarrierWave::Uploader::Base
   include CarrierWave::RMagick
   include CarrierWave::MimeTypes
 
-  if Rails.env.production?
-    storage :fog
-  elsif Rails.env.staging?
-    storage :fog
-  elsif Rails.env.development?
-    storage :file
-  else
-    storage :file
-  end
+  storage :file
 
   def store_dir
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end
 
   process :set_content_type
-  process :convert => 'png'
-
-  # Create different versions of your uploaded files:
-  version :large do 
-    process :resize_to_fit => [910, 630]
-    #process :manualcrop
-    process :watermark
-  end
-
-  version :thumb, :from_version => :large do
-    process :resize_to_fill => [260, 180]
-  end
-
-  def watermark
-    return if model.watermark.blank?
-    manipulate! do |image|
-      gc = Magick::Draw.new
-      gc.gravity = Magick::SouthEastGravity
-      gc.pointsize = 28
-      gc.font_family = "Helvetica"
-      gc.font_weight = Magick::BoldWeight
-      gc.stroke = 'none'
-      mark = Magick::Image.new(image.columns, image.rows)
-      gc.annotate(mark, 0, 0, 25, 25, "#{model.watermark} on CakeSide.com")
-      mark = mark.shade(true, 310, 30)
-      image.composite!(mark, Magick::SouthEastGravity, Magick::HardLightCompositeOp)
-    end
-  end
-
-  def manualcrop
-    manipulate! do |img|
-      img = img.crop(model.crop_x.to_i, model.crop_y.to_i, model.crop_w.to_i, model.crop_h.to_i)
-    end
-  end
 
   # Add a white list of extensions which are allowed to be uploaded.
   # For images you might use something like this:
app/views/creations/_form.html.erb
@@ -21,11 +21,9 @@
       </div>
     </div>
     <div class="control-group">
-      <%# f.label :image, "Upload Main Image", :class => "control-label" %>
-      <label for="photo" class="control-label">Upload Main Image</label>
+      <%= f.label :image, "Upload Main Image", :class => "control-label" %>
       <div class="controls">
-        <%# f.file_field :image, :class => "input-file" %>
-        <input type="file" name="photo" id="photo" value="" accept="image/*" />
+        <%= f.file_field :image, :class => "input-file" %>
       </div>
     </div>
     <div class="control-group">