Commit 6a3a803e

mo k <mo@mokhan.ca>
2012-10-07 22:38:56
migrate creation to load primary image from photos collection instead of image attribute.
1 parent 46d2693
app/controllers/creations_controller.rb
@@ -25,9 +25,11 @@ class CreationsController < ApplicationController
   def create
     @creation = current_user.creations.create(params[:creation])
     @creation.category_ids = params[:creation][:category_ids] ||= []
+    @creation.photos.build({:image => params[:photo]})
 
     if @creation.save
-      redirect_to(@creation, :notice => 'Creation was successfully cropped.') 
+      #redirect_to(@creation, :notice => 'Creation was successfully cropped.') 
+      redirect_to(creations_url) 
     else
       flash[:error] = @creation.errors.full_messages
       render :action => "new" 
app/models/creation.rb
@@ -1,16 +1,17 @@
 class Creation < ActiveRecord::Base
   acts_as_commentable
   validates :name,  :presence => true
-  validates :image, :presence => true
-  attr_accessible :user_id, :story, :name, :image, :remote_image_url, :category_ids, :is_restricted, :watermark
+  #validates :image, :presence => true
+  #attr_accessible :user_id, :story, :name, :image, :remote_image_url, :category_ids, :is_restricted, :watermark
+  attr_accessible :user_id, :story, :name, :category_ids, :is_restricted, :watermark
   belongs_to :user
   has_and_belongs_to_many :categories, :join_table => 'creations_categories', :uniq => true, :autosave => true
   has_many :photos, :dependent => :destroy
   has_many :favorites, :dependent => :destroy
-  mount_uploader :image, ImageUploader
+  #mount_uploader :image, ImageUploader
 
   attr_accessor :crop_x, :crop_y, :crop_h, :crop_w
-  after_update :reprocess_image, :if => :cropping?
+  #after_update :reprocess_image, :if => :cropping?
 
   default_scope order("created_at DESC")
 
@@ -22,14 +23,14 @@ class Creation < ActiveRecord::Base
     !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
   end
 
-  def reprocess_image
-    begin
-      self.image.recreate_versions!
-    rescue Exception => e
-      logger.error e.message
-      logger.error e.backtrace.inspect
-    end
-  end
+  #def reprocess_image
+    #begin
+      #self.image.recreate_versions!
+    #rescue Exception => e
+      #logger.error e.message
+      #logger.error e.backtrace.inspect
+    #end
+  #end
 
   def is_owned_by(user)
     @user == user
@@ -42,4 +43,12 @@ class Creation < ActiveRecord::Base
   def has_comments
     self.comment_threads.length > 0
   end
+
+  def primary_image
+    photos.first
+  end
+
+  def main_image_thumb_url
+    primary_image.image.thumb.url
+  end
 end
app/uploaders/photo_uploader.rb
@@ -21,14 +21,40 @@ class PhotoUploader < CarrierWave::Uploader::Base
   end
 
   process :set_content_type
-  process :resize_to_fit => [910, 630]
   process :convert => 'png'
 
   # Create different versions of your uploaded files:
-  version :thumb do
+  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:
   def extension_white_list
@@ -38,6 +64,8 @@ class PhotoUploader < CarrierWave::Uploader::Base
   # Override the filename of the uploaded files:
   # Avoid using model.id or version_name here, see uploader/store.rb for details.
   def filename
-    super.chomp(File.extname(super)) + '.png'
+    if original_filename 
+      super.chomp(File.extname(super)) + '.png'
+    end
   end
 end
app/views/creations/_form.html.erb
@@ -21,9 +21,11 @@
       </div>
     </div>
     <div class="control-group">
-      <%= f.label :image, "Upload Main Image", :class => "control-label" %>
+      <%# f.label :image, "Upload Main Image", :class => "control-label" %>
+      <label for="photo" class="control-label">Upload Main Image</label>
       <div class="controls">
-        <%= f.file_field :image, :class => "input-file" %>
+        <%# f.file_field :image, :class => "input-file" %>
+        <input type="file" name="photo" id="photo" value="" accept="image/*" />
       </div>
     </div>
     <div class="control-group">
app/views/creations/show.html.erb
@@ -31,8 +31,8 @@
 <div class="row">
   <div class="span12">
     <ul class="thumbnails">
-      <li class="span6"><a class="thumbnail gallery" href="<%= @creation.image_url(:large).to_s %>"><img src="<%= @creation.image_url(:large).to_s %>" alt="<%= @creation.name %>" /></a></li>
-      <% @creation.photos.each do |photo| %>
+      <li class="span6"><a class="thumbnail gallery" href="<%= @creation.primary_image.image_url(:large).to_s %>"><img src="<%= @creation.primary_image.image_url(:large).to_s %>" alt="<%= @creation.name %>" /></a></li>
+      <% @creation.photos.where(:is_primary => false).each do |photo| %>
       <li class="span3"><a class="thumbnail gallery" href="<%= photo.image.url.to_s %>"><img src="<%= photo.image.thumb.url.to_s %>" alt="<%= @creation.name %>" /></a></li>
       <% end %>
       <% if @creation.user == current_user %>
app/views/shared/_creation_image_gallery.html.erb
@@ -2,7 +2,7 @@
   <% @creations.each do |creation| %>
     <li class="span3">
     <div class="thumbnail">
-      <a href="<%= url_for creation %>"><img src="<%= creation.image.thumb.url %>" alt="<%= creation.name %>" style="width:260px;height:180px;" /></a>
+      <a href="<%= url_for creation %>"><img src="<%= creation.main_image_thumb_url %>" alt="<%= creation.name %>" style="width:260px;height:180px;" /></a>
       <h3><a href="<%= url_for creation %>"><%= short_name(creation, 15) %></a></h3>
       <h4><small><a href="<%= url_for profile_path(creation.user) %>"><%= shrink(creation.user.name, 20) %></a></small></h4>
       <p>