lib/arql/commands/models.rb in arql-0.3.30 vs lib/arql/commands/models.rb in arql-0.3.31
- old
+ new
@@ -11,24 +11,54 @@
t << [definition[:table], definition[:model].name, definition[:abbr] || '', definition[:comment] || '']
end
t
end
- def models_table(regexp)
- Terminal::Table.new do |t|
- models.each_with_index { |row, idx| t << (row || :separator) if row.nil? ||
- regexp.nil? ||
- idx.zero? ||
- row.any? { |e| e =~ regexp }
- }
+ def models_table(table_regexp=nil, column_regexp=nil)
+ if column_regexp.nil?
+ Terminal::Table.new do |t|
+ models.each_with_index { |row, idx| t << (row || :separator) if row.nil? ||
+ table_regexp.nil? ||
+ idx.zero? ||
+ row.any? { |e| e =~ table_regexp }
+ }
+ end
+ else
+ connection = ::ActiveRecord::Base.connection
+ table = Terminal::Table.new do |t|
+ t << ['PK', 'Table', 'Model', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
+ t << :separator
+ Arql::Definition.models.each do |definition|
+ model_class = definition[:model]
+ matched_columns = model_class.columns.select { |column| column.name =~ column_regexp || column.comment =~ column_regexp }
+ next if matched_columns.empty?
+ matched_columns.each do |column|
+ pk = if [connection.primary_key(definition[:table])].flatten.include?(column.name)
+ 'Y'
+ else
+ ''
+ end
+ t << [pk, definition[:table], model_class.name, column.name, column.sql_type,
+ column.sql_type_metadata.type, column.sql_type_metadata.limit || '',
+ column.sql_type_metadata.precision || '', column.sql_type_metadata.scale || '', column.default || '',
+ column.null, "#{definition[:comment]} - #{column.comment}"]
+ end
+ end
+ end
+ puts table
end
end
end
end
- Pry.commands.block_command 'm' do |regexp|
+ Pry.commands.block_command 'm' do |arg|
puts
- puts Models::models_table(regexp.try { |e| e.start_with?('/') ? eval(e) : Regexp.new(e) })
+ if arg.start_with?('c=') or arg.start_with?('column=')
+ column_regexp = arg.sub(/^c(olumn)?=/, '')
+ Models::models_table(nil, column_regexp.try { |e| e.start_with?('/') ? eval(e) : Regexp.new(e) })
+ else
+ puts Models::models_table(arg.try { |e| e.start_with?('/') ? eval(e) : Regexp.new(e) }, nil)
+ end
end
Pry.commands.alias_command 'l', 'm'
end