Sha256: a7d4be543b20051818c4f4bab6ce4536f4472b02cb7dd19f31d6878cd038c89a

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module Tim
  class TargetImage < Tim::Base
    belongs_to :image_version
    belongs_to :provider_type, :class_name => Tim.provider_type_class
    has_many :provider_images

    accepts_nested_attributes_for :image_version
    accepts_nested_attributes_for :provider_images

    attr_accessible :image_version_attributes
    attr_accessible :provider_images_attributes
    attr_accessible :status, :status_detail, :progress #, :as => :image_factory
    attr_accessible :target

    attr_protected :id

    after_create :create_factory_target_image

    def template
      image_version.base_image.template
    end

    private
    def create_factory_target_image
      begin
        target_image = ImageFactory::TargetImage.new(:template => template.xml,
                                                        :target => target,
                                                        :parameters => nil)
                                                        # A bug in ARes adds parameters twice to the resulting json
                                                        # when a map is provided in mass assign
        target_image.parameters =  { :callbacks => ["#{ImageFactory::TargetImage.callback_url}/#{self.id}"] }
        target_image.save!

        populate_factory_fields(target_image)
        self.save
      rescue => e
        # TODO Add proper error handling
        raise e
      end
    end

    def populate_factory_fields(factory_target_image)
      self.status = factory_target_image.status
      self.factory_id = factory_target_image.id
      self.status_detail = factory_target_image.status_detail.activity
      self.progress = factory_target_image.percent_complete
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tim-0.1.2 app/models/tim/target_image.rb
tim-0.1.1 app/models/tim/target_image.rb
tim-0.0.1 app/models/tim/target_image.rb