Module: NoFlyList::TaskHelpers

Defined in:
lib/no_fly_list/task_helpers.rb

Constant Summary collapse

COLORS =
{
  mysql2: "\e[38;5;33m",
  postgresql: "\e[38;5;31m",
  sqlite: "\e[38;5;245m",
  reset: "\e[0m",
  green: "\e[32m",
  red: "\e[31m",
  yellow: "\e[33m"
}.freeze
REQUIRED_COLUMNS =
{
  tag: [ "name" ],
  tagging: %w[tag_id taggable_id context]
}.freeze

Class Method Summary collapse

Class Method Details

.adapter_color(klass) ⇒ Object



20
21
22
23
# File 'lib/no_fly_list/task_helpers.rb', line 20

def self.adapter_color(klass)
  color_key = klass.connection.adapter_name.downcase.to_sym
  COLORS[color_key] || COLORS[:sqlite]
end

.check_class(class_name) ⇒ Object



55
56
57
58
59
# File 'lib/no_fly_list/task_helpers.rb', line 55

def self.check_class(class_name)
  Object.const_get(class_name)
rescue NameError
  nil
end

.check_table(klass) ⇒ Object



29
30
31
32
33
34
# File 'lib/no_fly_list/task_helpers.rb', line 29

def self.check_table(klass)
  klass.table_exists?
  [ true, "#{colorize('✓', :green)} Table exists: #{klass.table_name}" ]
rescue StandardError => e
  [ false, "#{colorize('✗', :red)} Error: #{e.message}" ]
end

.colorize(text, color) ⇒ Object



25
26
27
# File 'lib/no_fly_list/task_helpers.rb', line 25

def self.colorize(text, color)
  "#{COLORS[color]}#{text}#{COLORS[:reset]}"
end

.find_tag_classesObject



68
69
70
71
72
73
# File 'lib/no_fly_list/task_helpers.rb', line 68

def self.find_tag_classes
  Rails.application.eager_load!
  ActiveRecord::Base.descendants.select do |klass|
    klass.included_modules.any? { |mod| mod.in?([ NoFlyList::ApplicationTag, NoFlyList::TagRecord ]) }
  end
end

.find_taggable_classesObject



61
62
63
64
65
66
# File 'lib/no_fly_list/task_helpers.rb', line 61

def self.find_taggable_classes
  Rails.application.eager_load!
  ActiveRecord::Base.descendants.select do |klass|
    klass.included_modules.any? { |mod| mod.in?([ NoFlyList::TaggableRecord ]) }
  end
end

.format_columns(klass) ⇒ Object



50
51
52
53
# File 'lib/no_fly_list/task_helpers.rb', line 50

def self.format_columns(klass)
  return unless klass.table_exists?
  "#{colorize('✓', :green)} All columns: #{klass.column_names.join(', ')}"
end

.verify_columns(klass, type) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/no_fly_list/task_helpers.rb', line 36

def self.verify_columns(klass, type)
  return unless klass.table_exists?

  existing_columns = klass.column_names
  required_columns = REQUIRED_COLUMNS[type]
  missing_columns = required_columns - existing_columns

  if missing_columns.empty?
    "#{colorize('✓', :green)} Required columns present"
  else
    "#{colorize('!', :yellow)} Missing required columns: #{missing_columns.join(', ')}"
  end
end