Commit 01ef21ce

mo khan <mo@mokhan.ca>
2014-05-21 03:18:06
refactory many to many creation to categories to a single category per creation.
1 parent 8fe1b91
app/controllers/creations_controller.rb
@@ -21,7 +21,7 @@ class CreationsController < ApplicationController
   end
 
   def create
-    CreateCakeCommand.new(self).run(creation_params, params[:category_id], params[:creation_tags])
+    CreateCakeCommand.new(self).run(creation_params, params[:creation_tags])
   end
 
   def create_cake_succeeded(cake)
@@ -36,7 +36,7 @@ class CreationsController < ApplicationController
   end
 
   def update
-    UpdateCakeCommand.new(self).run(params[:id], params[:category_id], params[:creation_tags], creation_params)
+    UpdateCakeCommand.new(self).run(params[:id], params[:creation_tags], creation_params)
   end
 
   def update_cake_succeeded(cake)
@@ -62,6 +62,6 @@ class CreationsController < ApplicationController
   private
 
   def creation_params
-    params.require(:creation).permit(:name, :story, :is_restricted, :watermark)
+    params.require(:creation).permit(:name, :story, :is_restricted, :watermark, :category_id)
   end
 end
app/models/creation.rb
@@ -1,7 +1,7 @@
 class Creation < ActiveRecord::Base
   validates :name,  :presence => true
   belongs_to :user, :counter_cache => true
-  has_and_belongs_to_many :categories, :join_table => 'creations_categories', :autosave => true
+  belongs_to :category
   has_many :photos, -> { order :created_at }, :dependent => :destroy
   has_many :favorites, :dependent => :destroy
   has_many :comments, dependent: :destroy
app/services/application/create_cake_command.rb
@@ -5,9 +5,8 @@ class CreateCakeCommand
     @message_bus = message_bus
   end
 
-  def run(creation_attributes, category_id, tags)
+  def run(creation_attributes, tags)
     cake = @current_user.creations.create(creation_attributes)
-    cake.categories << Category.find(category_id) if category_id
     @current_user.tag(cake, with: tags, on: :tags)
 
     if cake.save
app/services/application/update_cake_command.rb
@@ -3,12 +3,10 @@ class UpdateCakeCommand
     @context = context
   end
 
-  def run(id, category_id, tags, creation_params)
+  def run(id, tags, creation_params)
     current_user = @context.current_user
 
     cake = current_user.creations.find(id)
-    cake.categories.clear
-    cake.categories << Category.find(category_id) if category_id
     current_user.tag(cake, with: tags, on: :tags)
 
     if cake.update_attributes(creation_params)
app/views/creations/_form.html.erb
@@ -32,7 +32,7 @@
     <div class="control-group">
       <label class="control-label">Category</label>
       <div class="controls">
-        <%= select_tag :category_id, options_from_collection_for_select(@categories, "id", "name") %>
+        <%= select_tag 'creation[category_id]', options_from_collection_for_select(@categories, "id", "name") %>
       </div>
     </div>
     <div class="control-group">
app/views/creations/show.html.erb
@@ -31,9 +31,7 @@
 
 <div class="row">
   <div class="span6">
-    <%= link_to creation_photo_path(@creation, @creation.primary_image) do %>
-      <img class="thumbnail" src="<%= @creation.primary_image.image.large.url %>" alt="<%= @creation.name %>" />
-    <% end %>
+    <img class="thumbnail" src="<%= @creation.primary_image.image.large.url %>" alt="<%= @creation.name %>" />
   </div>
   <div class="span6">
     <h1><%= link_to @creation.name, creation_path(@creation) %></h1>
db/migrate/20140520023608_add_category_id_to_creations.rb
@@ -0,0 +1,5 @@
+class AddCategoryIdToCreations < ActiveRecord::Migration
+  def change
+    add_column :creations, :category_id, :integer
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20140119052825) do
+ActiveRecord::Schema.define(version: 20140520023608) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -30,8 +30,8 @@ ActiveRecord::Schema.define(version: 20140119052825) do
 
   create_table "avatars", force: true do |t|
     t.integer  "user_id"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at",        null: false
+    t.datetime "updated_at",        null: false
     t.string   "avatar"
     t.boolean  "avatar_processing"
     t.string   "avatar_tmp"
@@ -69,6 +69,7 @@ ActiveRecord::Schema.define(version: 20140119052825) do
     t.string   "watermark"
     t.integer  "photos_count",    default: 0
     t.integer  "favorites_count", default: 0
+    t.integer  "category_id"
   end
 
   add_index "creations", ["created_at"], name: "index_creations_on_created_at", using: :btree
@@ -92,8 +93,8 @@ ActiveRecord::Schema.define(version: 20140119052825) do
     t.datetime "failed_at"
     t.string   "locked_by"
     t.string   "queue"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at",             null: false
+    t.datetime "updated_at",             null: false
   end
 
   add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
@@ -110,8 +111,8 @@ ActiveRecord::Schema.define(version: 20140119052825) do
 
   create_table "interests", force: true do |t|
     t.string   "name"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
   end
 
   create_table "photos", force: true do |t|
@@ -150,8 +151,8 @@ ActiveRecord::Schema.define(version: 20140119052825) do
     t.text     "description"
     t.string   "url"
     t.integer  "user_id"
-    t.datetime "created_at"
-    t.datetime "updated_at"
+    t.datetime "created_at",  null: false
+    t.datetime "updated_at",  null: false
     t.string   "image_url"
     t.string   "author"
     t.string   "author_url"
@@ -160,12 +161,12 @@ ActiveRecord::Schema.define(version: 20140119052825) do
   add_index "tutorials", ["user_id"], name: "index_tutorials_on_user_id", using: :btree
 
   create_table "users", force: true do |t|
-    t.string   "email",                  default: "", null: false
-    t.string   "encrypted_password",     default: "", null: false
+    t.string   "email",                              default: "", null: false
+    t.string   "encrypted_password",     limit: 128, default: "", null: false
     t.string   "reset_password_token"
     t.datetime "reset_password_sent_at"
     t.datetime "remember_created_at"
-    t.integer  "sign_in_count",          default: 0
+    t.integer  "sign_in_count",                      default: 0
     t.datetime "current_sign_in_at"
     t.datetime "last_sign_in_at"
     t.string   "current_sign_in_ip"
@@ -184,13 +185,13 @@ ActiveRecord::Schema.define(version: 20140119052825) do
     t.datetime "confirmed_at"
     t.datetime "confirmation_sent_at"
     t.string   "unconfirmed_email"
-    t.integer  "failed_attempts",        default: 0
+    t.integer  "failed_attempts",                    default: 0
     t.string   "unlock_token"
     t.datetime "locked_at"
     t.string   "authentication_token"
     t.string   "invitation_token"
     t.string   "full_address"
-    t.integer  "creations_count",        default: 0
+    t.integer  "creations_count",                    default: 0
     t.boolean  "is_admin"
   end
 
spec/controllers/creations_controller_spec.rb
@@ -55,7 +55,13 @@ describe CreationsController do
         let(:category) { create(:category) }
 
         before :each do
-          post :create, :creation => { :name => 'stone', :story => 'morning glory', :is_restricted => true, :watermark => "watery"}, :creation_tags => 'cake', :category_id => category.id
+          post :create, creation: {
+            name: 'stone',
+            story: 'morning glory',
+            is_restricted: true,
+            watermark: "watery",
+            category_id: category.id
+          }, creation_tags: 'cake'
         end
 
         it "assigns a newly created creation" do
spec/features/upload_creation_spec.rb
@@ -13,13 +13,12 @@ describe "uploading a new creation", :js => true do
     end
     click_button("Sign In")
     click_link("Add Creation")
-    #visit new_creation_path
 
     within(".form-horizontal") do
       fill_in("creation_name", :with => "yummy cake")
       fill_in("creation_watermark", :with => "yummy")
       fill_in("creation_story", :with => "this was just so damn yummy so i ate it alone.")
-      select(category_2.name, :from => 'category_id')
+      select(category_2.name, :from => 'creation_category')
       #fill_in("creation_tags", :with => "cake, yummy")
     end
     click_button("NEXT STEP")