Sha256: 6c7b25d1276d63555464d571eb258c24b303ea48dce02d2c416971799bb3e400

Contents?: true

Size: 1.2 KB

Versions: 49

Compression:

Stored size: 1.2 KB

Contents

module ForestLiana
  class HasManyDissociator < ForestLiana::ApplicationController
    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 @data.is_a?(Array)
        record_ids = @data.map { |record| record[:id] }
      elsif @data.dig('attributes').present?
        record_ids = ForestLiana::ResourcesGetter.get_ids_from_request(@params, forest_user)
      else
        record_ids = Array.new
      end

      if !record_ids.nil? && record_ids.any?
        if remove_association
          record_ids.each do |id|
            associated_records.delete(@association.klass.find(id))
          end
        end

        if @with_deletion
          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

49 entries across 49 versions & 1 rubygems

Version Path
forest_liana-7.8.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.4 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.8.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.0.beta.4 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.0.beta.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.0.beta.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-8.0.0.beta.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.8.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.7.3 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.7.2 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.7.1 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.7.0 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.6.14 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.6.13 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.6.12 app/services/forest_liana/has_many_dissociator.rb
forest_liana-7.6.11 app/services/forest_liana/has_many_dissociator.rb