Class: Lcms::Engine::BulkEditResourcesService

Inherits:
Object
  • Object
show all
Defined in:
app/services/lcms/engine/bulk_edit_resources_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(resources, params = nil) ⇒ BulkEditResourcesService

Returns a new instance of BulkEditResourcesService.



6
7
8
9
# File 'app/services/lcms/engine/bulk_edit_resources_service.rb', line 6

def initialize(resources, params = nil)
  @resources = resources
  @params = params
end

Instance Method Details

#edit!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/lcms/engine/bulk_edit_resources_service.rb', line 17

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.['grade'] = after.['grade']
      resource.tag_list = after.tag_list
      resource.resource_type_list = after.resource_type_list

      resource.save!
    end
  end
end

#init_sampleObject



11
12
13
14
15
# File 'app/services/lcms/engine/bulk_edit_resources_service.rb', line 11

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