Commit cff3acf1

mo khan <mo@mokhan.ca>
2014-06-29 03:38:11
extract method to create cake and ensure the category exists first.
1 parent 6446929
Changed files (3)
app/models/creation.rb
@@ -1,6 +1,6 @@
 class Creation < ActiveRecord::Base
-  validates :name,  :presence => true
-  validates :category_id, :presence => true
+  validates :name, presence: true
+  validates :category_id, presence: true
   belongs_to :user, :counter_cache => true
   belongs_to :category
   has_many :photos, -> { order :created_at }, :dependent => :destroy
@@ -34,4 +34,8 @@ class Creation < ActiveRecord::Base
   def liked_by(user)
     favorites.find_or_create_by(user: user)
   end
+
+  def hide_from_children!
+    update_attribute(:is_restricted, true)
+  end
 end
app/models/user.rb
@@ -62,6 +62,10 @@ class User < ActiveRecord::Base
     favorites.includes(:creation).map {|f| f.creation }
   end
 
+  def create_cake(name:, description: nil, category:, watermark: nil)
+    creations.create(name: name, story: description, category_id: category.id, watermark: watermark)
+  end
+
   class << self
     def ordered
       User.order(:creations_count => :desc)
app/services/application/create_cake_command.rb
@@ -5,8 +5,9 @@ class CreateCakeCommand
     @message_bus = message_bus
   end
 
-  def run(creation_attributes, tags)
-    cake = @current_user.creations.create(creation_attributes)
+  def run(attributes, tags)
+    cake = @current_user.create_cake(name: attributes[:name], description: attributes[:story], category: Category.find(attributes[:category_id]), watermark: attributes[:watermark])
+    cake.hide_from_children! if attributes[:is_restricted].present? && attributes[:is_restricted] == true
     @current_user.tag(cake, with: tags, on: :tags)
 
     if cake.save