Sha256: 2ff88c32b9d2e9516628786689daa64756f21f212061540195e75ed0b9df3b7d

Contents?: true

Size: 1 KB

Versions: 69

Compression:

Stored size: 1 KB

Contents

module ForestLiana
  class HasManyDissociator
    def initialize(resource, association, params)
      @resource = resource
      @association = association
      @params = params
      @with_deletion = @params[:delete].to_s == 'true'
      @data = params['data']
    end

    def perform
      @record = @resource.find(@params[:id])
      associated_records = @resource.find(@params[:id]).send(@association.name)

      remove_association = !@with_deletion || @association.macro == :has_and_belongs_to_many

      if remove_association
        if @data.is_a?(Array)
          @data.each do |record_deleted|
            associated_records.delete(@association.klass.find(record_deleted[:id]))
          end
        end
      end

      if @with_deletion
        if @data.is_a?(Array)
          record_ids = @data.map { |record| record[:id] }
          record_ids = record_ids.select { |record_id| @association.klass.exists?(record_id) }
          @association.klass.destroy(record_ids)
        end
      end
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
forest_liana-4.2.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.1.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.1.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.1.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.1.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.0.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.0.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.0.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.0.0.pre.beta.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.3.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-4.0.0.pre.beta.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.2.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.1.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.1.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.0.7 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.0.6 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.0.5 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.0.4 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.0.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-3.0.2 app/services/forest_liana/has_many_dissociator.rb