Module: CbList

Defined in:
lib/cb_list.rb,
lib/cb_list/railtie.rb,
lib/cb_list/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.name_callbacks_for(klass, name) ⇒ Object



42
43
44
# File 'lib/cb_list.rb', line 42

def self.name_callbacks_for klass, name
  klass.public_send("_#{name}_callbacks")
end

.name_kind_callbacks_for(klass, name, kind) ⇒ Object



46
47
48
49
50
# File 'lib/cb_list.rb', line 46

def self.name_kind_callbacks_for klass, name, kind
  name_callbacks = name_callbacks_for(klass, name).select { |name_callback| name_callback.kind.eql?(kind) }
  name_callbacks.reverse! if kind == :after
  name_callbacks
end


36
37
38
39
40
# File 'lib/cb_list.rb', line 36

def self.print_out str=nil
  prefix = "# "
  result = str ? prefix + str : prefix
  puts result
end

.show(klass) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cb_list.rb', line 7

def self.show klass
  klass = klass.constantize if klass.is_a? String
  names = ActiveRecord::Callbacks::CALLBACKS.map { |cb| cb.to_s.split('_').last }.uniq
  delimiter_1 = "=" * 20
  delimiter_2 = "-" * 20
  
  print_out "#{klass.name} Active Record Callbacks".red

  names.each.with_index(1) do |name, name_index|
    print_out
    print_out "#{name_index}. #{name.upcase}".light_blue
    print_out delimiter_1.light_blue

    ActiveSupport::Callbacks::CALLBACK_FILTER_TYPES.each do |kind|
      kind_name = "#{kind}_#{name}".to_sym
      kind_callbacks = name_kind_callbacks_for(klass, name, kind)
    
      if ActiveRecord::Callbacks::CALLBACKS.include?(kind_name)
        print_out
        print_out "#{kind_name}".blue
        print_out delimiter_2.blue
        kind_callbacks.each.with_index(1) do |callback, callback_index|
          print_out "#{callback_index}. #{callback.raw_filter}".green
        end.presence || print_out("Empty")
      end
    end
  end
end