Commit df0de9b0

mo k <mo@mokhan.ca>
2012-10-13 04:20:00
add is_published to the creation model and only show fully published creations in the index page.
1 parent aa94f69
app/controllers/creations_controller.rb
@@ -2,7 +2,8 @@ class CreationsController < ApplicationController
   before_filter :authenticate_user!, :except => [:show, :index]
   # GET /creations
   def index
-    @creations = Creation.where(:is_restricted => false).page(params[:page]).per(16)
+    @creations = Creation.where(:is_restricted => false, :is_published => true).page(params[:page]).per(16)
+    #@creations = Creation.where(:is_restricted => false, :is_published => true).page(params[:page]).per(16)
   end
 
   # GET /creations/1
@@ -27,7 +28,7 @@ class CreationsController < ApplicationController
     @creation.category_ids = params[:creation][:category_ids] ||= []
     if @creation.save
       @creation.delay.migrate_primary_image
-      redirect_to(creations_url, :notice => 'Thank you for sharing your creation.') 
+      redirect_to(creations_url, :notice => 'Thank you for sharing your creation. It will appear in the main timeline shortly.') 
     else
       flash[:error] = @creation.errors.full_messages
       render :action => "new" 
app/models/creation.rb
@@ -22,10 +22,6 @@ class Creation < ActiveRecord::Base
     photos.where(:is_primary => true).first
   end
 
-  def main_image_thumb_url
-    primary_image.image.thumb.url
-  end
-
   def migrate_primary_image
       photo = photos.build({:is_primary => true})
       photo.created_at = created_at
@@ -34,7 +30,9 @@ class Creation < ActiveRecord::Base
       photo.save!
   end
 
-  def is_published?
-    photos.where(:is_primary => true).any?
+  def publish
+    if (photos.where(:is_primary => true).any?)
+      self.update_attribute(:is_published, true)
+    end
   end
 end
app/models/photo.rb
@@ -9,6 +9,10 @@ class Photo < ActiveRecord::Base
     creation.watermark
   end
 
+  def processing_complete
+    creation.publish if is_primary
+  end
+
   def to_s
     "#{id} #{image}"
   end
app/uploaders/photo_uploader.rb
@@ -4,6 +4,7 @@ class PhotoUploader < CarrierWave::Uploader::Base
   include CarrierWave::RMagick
   include CarrierWave::MimeTypes
   include ::CarrierWave::Backgrounder::Delay
+  after :store, :publish_photo
 
   if Rails.env.production?
     storage :fog
@@ -33,7 +34,6 @@ class PhotoUploader < CarrierWave::Uploader::Base
 
   def watermark
     return if model.watermark.blank?
-    puts "processing watermark #{model.watermark}"
     manipulate! do |image|
       gc = Magick::Draw.new
       gc.gravity = Magick::SouthEastGravity
@@ -61,4 +61,8 @@ class PhotoUploader < CarrierWave::Uploader::Base
       super.chomp(File.extname(super)) + '.png'
     end
   end
+
+  def publish_photo(file)
+    model.processing_complete
+  end
 end
app/views/layouts/_messages.html.erb
@@ -16,7 +16,7 @@
         <%= yield(:page_header) -%>
         &nbsp;
       </div>
-      <div class="span3" style="text-align: right; padding-bottom: 8px">
+      <div class="span3 hidden-phone" style="text-align: right; padding-bottom: 8px">
         <a href="https://www.facebook.com/pages/CakeSide/214607468615074" target="_blank" align="left"><img src="/assets/facebook.png" border="0"></a>
         <a href="http://twitter.com/cakeside" target="_blank" align="left"><img src="/assets/twitter.png" border="0"></a>
         <a href="http://pinterest.com/cakeside/" target="_blank" align="left" ><img src="/assets/pinterest.png" border="0"></a>
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.main_image_thumb_url %>" alt="<%= creation.name %>" style="width:260px;height:180px;" /></a>
+      <a href="<%= url_for creation %>"><img src="<%= creation.primary_image.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>
db/migrate/20121013031725_add_is_published_to_creation.rb
@@ -0,0 +1,9 @@
+class AddIsPublishedToCreation < ActiveRecord::Migration
+  def change
+    add_column :creations, :is_published, :boolean, :default => false
+    Creation.reset_column_information
+    Creation.all.each do |p|
+      p.update_column :is_published, true
+    end
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20121010190225) do
+ActiveRecord::Schema.define(:version => 20121013031725) do
 
   create_table "active_admin_comments", :force => true do |t|
     t.integer  "resource_id",   :null => false
@@ -86,6 +86,7 @@ ActiveRecord::Schema.define(:version => 20121010190225) do
     t.string   "image"
     t.boolean  "is_restricted", :default => false, :null => false
     t.string   "watermark"
+    t.boolean  "is_published",  :default => false
   end
 
   create_table "creations_categories", :id => false, :force => true do |t|
spec/controllers/creations_controller_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
 
 describe CreationsController do
   let(:user){ FactoryGirl.create(:user) }
-  let(:creation){ FactoryGirl.create(:creation, :user => user) }
+  let(:creation){ FactoryGirl.create(:creation, :user => user, :is_published => true) }
 
   before (:each) do
     http_login(user)
spec/models/creation_spec.rb
@@ -5,16 +5,23 @@ describe Creation do
 
   context "when not published" do
     it "should return false" do
-      sut.is_published?.should == false
+      sut.is_published.should == false
     end
   end
   context "when published" do
     it "should return true" do
-      sut.is_published?.should == true
+      Creation.find(sut.id).is_published.should == true
     end
     before(:each) do
       sut.migrate_primary_image
     end
   end
 
+  describe "should be able to set attributes" do
+    it "should save is_published" do
+      sut.is_published=true
+      sut.save
+      Creation.find(sut.id).is_published.should == true
+    end
+  end
 end
spec/requests/creations_spec.rb
@@ -2,6 +2,9 @@ require 'spec_helper'
 
 describe "Creations" do
   describe "GET /creations" do
+    before(:each) do
+      FactoryGirl.create(:creation, :is_published => true)
+    end
     it "works! (now write some real specs)" do
       get creations_path
       response.status.should be(200)