Class: BulkDependencyEraser::Base

Inherits:
Object
  • Object
show all
Extended by:
Utils
Includes:
Utils
Defined in:
lib/bulk_dependency_eraser/base.rb

Constant Summary collapse

POLY_KLASS_NAME =
"<POLY>"
DEFAULT_SCOPE_WRAPPER =

Default Custom Scope for all classes, no effect.

->(query) { nil }
DEFAULT_KLASS_MAPPED_SCOPE_WRAPPER =

Default Custom Scope for mapped-by-name classes, no effect.

->(query) { query }
DEFAULT_DB_READ_WRAPPER =
->(block) {
  begin
    ActiveRecord::Base.connected_to(role: :reading) do
      block.call
    end
  rescue ActiveRecord::ConnectionNotEstablished
    # No role: :reading setup, use regular connection
    block.call
  end
}
DEFAULT_DB_WRITE_WRAPPER =
->(block) {
  begin
    ActiveRecord::Base.connected_to(role: :writing) do
      block.call
    end
  rescue ActiveRecord::ConnectionNotEstablished
    # No role: :writing setup, use regular connection
    block.call
  end
}
DEFAULT_DB_BLANK_WRAPPER =
->(block) { block.call }
DEFAULT_OPTS =
{
  # Applied to all queries. Useful for taking advantage of specific indexes
  # - not indexed by klass name. Proc would handle the logic for that.
  # - 3rd, and lowest, priority of scopes
  # - accepts rails query as parameter
  # - return nil if no applicable scope.
  proc_scopes: self::DEFAULT_SCOPE_WRAPPER,
  # Applied to all queries. Useful for taking advantage of specific indexes
  # - 2nd highest priority of scopes
  proc_scopes_per_class_name: {},
}.freeze
DEPENDENCY_NULLIFY =
%i[
  nullify
].freeze
DEPENDENCY_RESTRICT =

Abort deletion if assoc dependency value is any of these.

  • exception if the :force_destroy_restricted option set true

%i[
  restrict_with_error
  restrict_with_exception
].freeze
DEPENDENCY_DESTROY =
(
  %i[
    destroy
    delete_all
    destroy_async
  ] + self::DEPENDENCY_RESTRICT
).freeze
DEPENDENCY_DESTROY_IGNORE_REFLECTION_TYPES =
[
  # Rails 6.1, when a has_and_delongs_to_many <assoc>, dependent: :destroy,
  # will ignore the destroy. Will neither destroy the join table record nor the association record
  # We will do the same, mirror the fuctionality, by ignoring any :dependent options on these types.
  'ActiveRecord::Reflection::HasAndBelongsToManyReflection'
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Methods

#deep_freeze

Constructor Details

#initialize(opts: {}) ⇒ Base

Returns a new instance of Base.



76
77
78
79
80
81
82
# File 'lib/bulk_dependency_eraser/base.rb', line 76

def initialize opts: {}
  filtered_opts = opts.slice(*self.class::DEFAULT_OPTS.keys)
  @opts_c = options_container.new(
    self.class::DEFAULT_OPTS.merge(filtered_opts)
  )
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



74
75
76
# File 'lib/bulk_dependency_eraser/base.rb', line 74

def errors
  @errors
end

Instance Method Details

#executeObject

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/bulk_dependency_eraser/base.rb', line 84

def execute
  raise NotImplementedError
end

#merge_errors(errors, prefix = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bulk_dependency_eraser/base.rb', line 94

def merge_errors errors, prefix = nil
  local_errors = errors.dup

  unless local_errors.any?
    local_errors << '<NO ERRORS FOUND TO MERGE>'
  end

  if prefix
    local_errors = errors.map { |error| prefix + error }
  end
  @errors += local_errors
end

#report_error(msg) ⇒ Object



88
89
90
91
92
# File 'lib/bulk_dependency_eraser/base.rb', line 88

def report_error msg
  # remove new lines, surrounding white space, replace with semicolon delimiters
  n = msg.strip.gsub(/\s*\n\s*/, ' ')
  @errors << n
end