2012-09-06 6 views
0

У меня есть документ Design.rb с встроенным документом Attachment.rb, который выполняет некоторую проверку. Если Attachment.rb терпит неудачу, тогда Design.rb также должен потерпеть неудачу, но это не так.Проверка встроенного документа в Mongoid

Любые предложения по поводу того, как я получаю, чтобы мой класс дизайна терпел неудачу, если встроенный документ вышел из строя?

Design.rb:

class Design 

    embeds_many :attachments, :as => :attachable 

    validates_associated :attachments 

    accepts_nested_attributes_for :attachments, :allow_destroy => true 

    field :description 
    field :title 
    field :tags, type: Array 
    field :featured, :type => Boolean, :default => false 
    field :full_member, :type => Boolean, :default => false 
    field :first_design, :type => Boolean, :default => false 
    field :option, :type => String 
    field :app_store_url, :type => String 
    field :design_in_progress, :type => Boolean 

    attr_accessible :design_in_progress, :tags, :description, :title, :featured, :project_number, :show, :option, :app_store_url, :temp_number, :option, :attachments_attributes 

    validates :description, :tags, :title, :presence => true 

end 

Attachment.rb

require 'carrierwave/mongoid' 

class Attachment 

    embedded_in :attachable, :polymorphic => true, :inverse_of => :attachments 

    field :image 
    field :width, :type => Integer 
    field :height, :type => Integer 
    field :option, :type => String 

    attr_accessible :image, :width, :height, :option, :normal, :url 

    mount_uploader :image, ImageUploader 

    validate :validate_minimum_image_size 

    def validate_minimum_image_size 
     self.option = self.attachable.option 

     case self.option 
     when "iphone" 
      width = 640 
      height = 960 
      type = "iPhone" 
     when "ipad" 
      width = 1536 
      height = 2048 
      type = "iPad" 
     when "icon" 
      width = 1024 
      height = 1024 
      type = "Icon" 
     when "wp" 
      width = 480 
      height = 800 
      type = "Windows Phone" 
     when "android" 
      width = 480 
      height = 800 
      type = "Android" 
     end 

     geometry = self.image.geometry 
     unless geometry.nil? 
      self.width = geometry[0] 
      self.height = geometry[1] 
     end 

     unless (self.width == width && self.height == height) 
      puts "INSIDE ERROR STATEMENT" 
      errors.add :base, "Naughty you... "+type+" designs should be "+width.to_s+"px times "+height.to_s+"px. We want to see your awesome design in retina :-)." 
     end 
    end 
end 

ответ

2

Вы можете попробовать добавить

embeds_many :attachments, :as => :attachable, cascade_callbacks: true 

который будет вызывать валидации, когда изменения сделаны через модель дизайна.

Смежные вопросы