ruby Clone Stand产品throght活动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Clone Stand产品throght活动相关的知识,希望对你有一定的参考价值。

#Esta tarea duplica los productos de los stands de un eventto hacia otro evento mas reciente.
#Normalmente se usa en conjunto con la tarea de duplicacion de stands.
#Relaciona los stands del evento anterior con el siguiente evento usando el nombre de la compañia que los reservo.
#No es transaccional
task :duplicate_product_services => [:environment] do
    from_event = Event.find(3376)
    from_event = Event.find(2189)
    to_event = Event.find(3660) #Target
    skipped, errors, repeated = [], [], []
    from_product_id = StandReservationProductField.get_public_ids(from_event.id).first
    to_product_id   = StandReservationProductField.get_public_ids(to_event.id).first
    product_fields_match = {}
    from_event.stand_reservation_product_fields.select(:id, :name).each do |field|
        to_field = StandReservationProductField.where(event_id: to_event.id, archived: false, draft: false).where("lower(trim(name)) = ?", field.name.strip.downcase).first
        product_fields_match[field.id.to_s] = to_field.id.to_s if to_field.present?
    end
    from_event.stands.each do |from_stand|
        from_reservation = from_stand.current_reservation
        next if from_reservation.blank? or from_reservation.stand_reservation_product_services.blank?
        to_stand = to_event.stands.joins(current_reservation: [:company]).where("lower(trim(companies.name)) = ?", from_reservation.company.name.strip.downcase).first
        if to_stand.blank? or to_stand.current_reservation.blank?
            skipped << "stand not found #{from_stand.identifier}"
            next
        end
        new_reservation = to_stand.current_reservation
        to_stand_product_list = new_reservation.stand_reservation_product_services.present? ? new_reservation.stand_reservation_product_services.map{|p| p.values[to_product_id.to_s]} : []
        to_stand_product_list = to_stand_product_list.map{|name| name.strip.downcase if name.present? } if to_stand_product_list.present?
        from_reservation.stand_reservation_product_services.each do |from_product|
            next if from_product.values.blank?
            product_name = from_product.values[from_product_id.to_s]
            product_name = product_name.strip.downcase if product_name.present?
            puts "-------------------"
            puts "-------------------"
            puts "-------------------"
            puts "-------------------"
            puts to_stand_product_list
            if to_stand_product_list.include? product_name
                skipped << "Product #{from_stand.identifier} #{product_name}"
                next
            end
            new_product =  StandReservationProductService.create_draft(new_reservation.id)
            from_values = from_product.values
            from_product_name = from_values.values.first
            new_values = Hash[from_values.map{|k,v| [product_fields_match[k.to_s], v]}]
            new_product.values = new_values
            new_product.draft = false
            if !new_product.save
                errors << "Stand: #{from_stand.identifier} product: #{from_product_name} #{new_product.errors.full_messages.join(',')}"
            else
                create_images = ModelAsset.get_images_for_entity(from_product.id, ModelAsset::RELATED_ENTITY_TYPE[:stand_reservation_product_services])
                create_images.each do |create_img|
                    next if !create_img.attachment.exists?
                    url = create_img.attachment.url
                    file = ModelAsset.new(related_entity_id: new_product.id, related_entity:  ModelAsset::RELATED_ENTITY_TYPE[:stand_reservation_product_services])
                    file.asset_type = ModelAsset::ASSET_TYPE[:image]
                    file.attachment = URI.parse(url)
                    errors << "Image for product Stand: #{from_stand.identifier} product: #{from_product_name} #{file.errors.full_messages.join(',')}" if !file.save
                end
            end
        end
    end
end

以上是关于ruby Clone Stand产品throght活动的主要内容,如果未能解决你的问题,请参考以下文章

ruby中的复制 dup clone

ruby 使用ssh键的git clone

Ruby:检查数组中所有对象的属性是不是相等

Ruby dup/clone 递归

在 Ruby 类上定义哪种方法来为其实例提供 dup / clone?

每天站立会议(Daily Stand-up Meeting)