Method: Array#t

Defined in:
lib/crudboy/ext/array.rb

#t(*attrs, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/crudboy/ext/array.rb', line 16

def t(*attrs, **options)
  if (attrs.present? || options.present? && options[:except]) && present? && first.is_a?(ActiveRecord::Base)
    column_names = first.attribute_names.map(&:to_sym)
    attrs = attrs.flat_map { |e| e.is_a?(Regexp) ? column_names.grep(e) : e }.uniq
    if options.present? && options[:except]
      attrs = column_names if attrs.empty?
      if options[:except].is_a?(Regexp)
        attrs.reject! { |e| e =~ options[:except] }
      else
        attrs -= [options[:except]].flatten
      end
    end
    puts Terminal::Table.new { |t|
      t << attrs
      t << :separator
      each do |e|
        t << e.attributes.values_at(*attrs.map(&:to_s))
      end
    }
  else
    table = Terminal::Table.new { |t|
      v.each { |row| t << (row || :separator)}
    }.to_s

    terminal_width = `tput cols`.to_i
    if table.lines.first.size > terminal_width
      table = table.lines.map(&:chomp)
      puts table[0..2].join("\n")
      puts table[3..-1].join("\n#{'-' * terminal_width}\n")
    else
      puts table
    end
  end
end