class Dropkiq::DropMethodAnalyzer

Constants

CHANGEME

Attributes

drop_class_analyzer[RW]
drop_method[RW]
dropkiq_type[RW]
foreign_table_name[RW]
sample_drop[RW]

Public Class Methods

new(drop_class_analyzer, drop_method, sample_drop=nil) click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 10
def initialize(drop_class_analyzer, drop_method, sample_drop=nil)
  self.drop_class_analyzer = drop_class_analyzer
  self.drop_method = drop_method
  self.sample_drop = sample_drop
end

Public Instance Methods

analyze() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 16
def analyze
  self.dropkiq_type = if is_relationship?
    relationship_to_dropkiq_type_classifier
  elsif is_column?
    column_to_dropkiq_type_classifier
  else
    Dropkiq::DropMethodNameClassifier.new(drop_method).classify
  end

  if dropkiq_type.blank?
    result = Dropkiq::DropMethodInstanceSimulator.new(drop_method, sample_drop).classify
    self.dropkiq_type = result.dropkiq_type
    self.foreign_table_name = result.foreign_table_name
  end
end
to_param() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 32
def to_param
  {
    "#{drop_method}" => {
      "type"               => (dropkiq_type.presence || CHANGEME),
      "foreign_table_name" => foreign_table_name
    }
  }
end

Private Instance Methods

column_to_dropkiq_type_classifier() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 87
def column_to_dropkiq_type_classifier
  case columns_hash_value.type
  when :string
    Dropkiq::STRING_TYPE
  when :integer
    Dropkiq::NUMERIC_TYPE
  when :boolean
    Dropkiq::BOOLEAN_TYPE
  when :datetime
    Dropkiq::DATE_TIME_TYPE
  when :date
    Dropkiq::DATE_TIME_TYPE
  when :decimal
    Dropkiq::NUMERIC_TYPE
  when :float
    Dropkiq::NUMERIC_TYPE
  when :text
    Dropkiq::TEXT_TYPE
  when :time
    Dropkiq::DATE_TIME_TYPE
  when :binary
    Dropkiq::NUMERIC_TYPE
  end
end
columns_hash_value() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 79
def columns_hash_value
  active_record_class.columns_hash[drop_method.to_s]
end
is_column?() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 83
def is_column?
  columns_hash_value.present?
end
is_relationship?() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 47
def is_relationship?
  reflect_on_association_value.present?
end
reflect_on_association_value() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 43
def reflect_on_association_value
  active_record_class.reflect_on_association(drop_method)
end
relationship_to_dropkiq_type_classifier() click to toggle source
# File lib/dropkiq/drop_method_analyzer.rb, line 51
def relationship_to_dropkiq_type_classifier
  reflection = reflect_on_association_value

  begin
    self.foreign_table_name = reflection.class_name.constantize.table_name
  rescue
    puts "WARNING: Could not find #{drop_method} on #{active_record_class.name}"
    return
  end

  case reflection
  when ActiveRecord::Reflection::BelongsToReflection
    Dropkiq::HAS_ONE_TYPE
  when ActiveRecord::Reflection::HasOneReflection
    Dropkiq::HAS_ONE_TYPE
  when ActiveRecord::Reflection::HasManyReflection
    Dropkiq::HAS_MANY_TYPE
  when ActiveRecord::Reflection::ThroughReflection
    if reflection.send(:delegate_reflection).is_a?(ActiveRecord::Reflection::HasOneReflection)
      Dropkiq::HAS_ONE_TYPE
    else
      Dropkiq::HAS_MANY_TYPE
    end
  when ActiveRecord::Reflection::HasAndBelongsToManyReflection
    Dropkiq::HAS_MANY_TYPE
  end
end