1

我有一个审核表,其中某些模型在创建时发送以等待审核。

中度.rb

class Moderate < ActiveRecord::Base
  attr_accessible :moderatable_id, :moderatable_type, :event, :data
  belongs_to :moderatable, polymorphic: true
  belongs_to :user

  scope :need_approval, where("approved IS NULL")
  mount_uploader :moderated_image, ModerationUploader

  def self.send_to_moderation(klass, event, params, id = nil)
    Moderate.create(
      moderatable_id: id,
      moderatable_type: klass,
      event: event,
      data: params.to_s
    )
  end

  ...

end

播放器.rb

class Player < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :handle, :game_id, :sc_race, :lane, :birthday, 
                  :photo, :team_division_id, :country_id, :keyboard, :mouse, :mousepad, :headset, :short_handle, 
                  :videocard, :bio, :monitor, :processor, :facebook, :twitter, :user_id, :stream_attributes

  ...

  mount_uploader :photo, PlayerUploader

end

示例“创建”操作

def create
  @player = Player.new(params[:player])
  @player.user = current_user
  if @player.save
    Moderate.send_to_moderation("Player", "create", mod_params.to_json)
    redirect_to players_url, notice: "Player successfully created!"
  else 
    render action: "new"
  end
end

参数

Parameters: {"utf8"=>"✓", 
             "authenticity_token"=>"er0VIcRfywxjROf634bhCEzn1b/GbAOZqnTpFFC9MAk=", 
             "player"=>
               {"handle"=>"angel_from_mexico", 
               "photo"=>
                 #<
                 ActionDispatch::Http::UploadedFile:0x00000103f58b70     
                 @original_filename="pulga!.jpg", 
                 @content_type="image/jpeg", 
                 @headers="Content-Disposition: form-data; 
                   name=\"player[photo]\"; 
                   filename=\"pulga!.jpg\"\r\nContent-Type: image/jpeg\r\n", 
                 @tempfile=#<Tempfile:/var/folders/GP/GPFCwZ6wFS0gEUkdN0ek1E+++TI/-Tmp-/RackMultipart20131014-78956-106x9m0>
                 >, 
               "first_name"=>"pulga", 
               "last_name"=>"nam", 
               "lane"=>"", 
               "sc_race"=>"", 
               "birthday(2i)"=>"2", 
               "birthday(3i)"=>"28", 
               "birthday(1i)"=>"2005", 
               "team_division_id"=>"", 
               "country_id"=>"122",
              "stream_attributes"=>{"name"=>""}, 
              "bio"=>"멍, 멍!", 
              "monitor"=>"acer", 
              "mouse"=>"acer", 
              "processor"=>"rad one", 
              "headset"=>"rad ones", 
              "mousepad"=>"default", 
              "keyboard"=>"garbage", 
              "videocard"=>"good one", 
              "facebook"=>"", 
              "twitter"=>""}}

问题:

创建或编辑播放器时,会创建包含操作和所有数据的新审核,但此时(在审核创建期间)而不是将图像上传到播放器/播放器上传器,我希望图像指向审核和审核上传者。我怎样才能做到这一点?

4

1 回答 1

2

在您的情况下,我会将该操作包装到工厂服务对象中,该对象将做三件事:

  • 从播放器参数中提取图像
  • 保存播放器(有或没有图像,不明白你是否需要它)
  • 使用指定图像保存审核

更多关于工厂类:

http://www.devinterface.com/blog/en/2010/06/design-patterns-in-ruby-abstract-factory/

于 2013-10-14T13:12:30.497 回答