Sha256: 1e4a4638b2265be7b59298cac16813107a2ac5bac4dce01265e1be6aac19dc78

Contents?: true

Size: 1.16 KB

Versions: 16

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    class BulkEditResourcesService
      def initialize(resources, params = nil)
        @resources = resources
        @params = params
      end

      def init_sample
        resource = Resource.new
        resource.standard_ids = @resources.map(&:standard_ids).inject { |memo, ids| memo & ids }
        resource
      end

      def edit!
        before = init_sample
        after = Resource.new(@params)

        Resource.transaction do
          @resources.each do |resource|
            # Standards
            resource.resource_standards
              .where(standard_id: before.standard_ids)
              .where.not(standard_id: after.standard_ids)
              .destroy_all

            (after.standard_ids - before.standard_ids).each do |standard_id|
              resource.resource_standards.find_or_create_by!(standard_id: standard_id)
            end

            resource.metadata['grade'] = after.metadata['grade']
            resource.tag_list = after.tag_list
            resource.resource_type_list = after.resource_type_list

            resource.save!
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.5.4 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.5.3 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.5.2 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.5.1 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.5.0 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.4.2 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.4.1 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.4.0 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.3.1 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.1.4 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.3.0 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.1.3 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.2.0 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.1.2 app/services/lcms/engine/bulk_edit_resources_service.rb
lcms-engine-0.1.0 app/services/lcms/engine/bulk_edit_resources_service.rb