Commit 8d5ba18d

mo k <mo@mokhan.ca>
2012-11-08 04:34:00
split DTO, mapper and query into separate files.
1 parent 6713f24
app/models/creation.rb
@@ -12,6 +12,7 @@ class Creation < ActiveRecord::Base
 
   default_scope order("created_at DESC")
 
+  # to be removed and moved to the DisplayCreationDTO
   def to_param
     "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
   end
app/services/creation_to_display_creation_dto_mapper.rb
@@ -0,0 +1,14 @@
+class CreationToDisplayCreationDTOMapper
+  def map_from(creation)
+    dto = DisplayCreationDTO.new
+    dto.id = creation.id
+    dto.name = creation.name
+    dto.story = creation.story
+    dto.primary_image = creation.primary_image
+    dto.thumb_url = creation.primary_image.image.thumb.url
+    dto.user = creation.user
+    dto.favorites = creation.favorites
+    dto.comment_threads = creation.comment_threads
+    dto
+  end
+end
app/services/display_creation_dto.rb
@@ -0,0 +1,9 @@
+class DisplayCreationDTO
+  attr_accessor :id, :name, :story, :primary_image, :thumb_url, :user, :favorites, :comment_threads
+  def self.model_name
+    ActiveModel::Name.new(self, nil, 'Creation')
+  end
+  def to_param
+    "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
+  end
+end
app/services/find_all_creations_query.rb
@@ -1,5 +1,5 @@
 class FindAllCreationsQuery
-  def initialize(user, mapper = CreationToDisplayCreationDTO.new)
+  def initialize(user, mapper = CreationToDisplayCreationDTOMapper.new)
     @user = user
     @mapper = mapper
   end
@@ -18,26 +18,3 @@ class FindAllCreationsQuery
     Kaminari.paginate_array(items).page(page).per(16)
   end
 end
-class CreationToDisplayCreationDTO
-  def map_from(creation)
-    dto = DisplayCreationDTO.new
-    dto.id = creation.id
-    dto.name = creation.name
-    dto.story = creation.story
-    dto.primary_image = creation.primary_image
-    dto.thumb_url = creation.primary_image.image.thumb.url
-    dto.user = creation.user
-    dto.favorites = creation.favorites
-    dto.comment_threads = creation.comment_threads
-    dto
-  end
-end
-class DisplayCreationDTO
-  attr_accessor :id, :name, :story, :primary_image, :thumb_url, :user, :favorites, :comment_threads
-  def self.model_name
-    ActiveModel::Name.new(self, nil, 'Creation')
-  end
-  def to_param
-    "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
-  end
-end
spec/controllers/creations_controller_spec.rb
@@ -13,7 +13,11 @@ describe CreationsController do
   describe "GET index" do
     it "assigns all creations as @creations" do
       get :index
-      assigns(:creations).should eq([creation])
+      creation = assigns(:creations).first
+      creation.id.should == creation.id
+      creation.name.should == creation.name
+      creation.story.should == creation.story
+      creation.user.should == creation.user
     end
   end