mirror of
https://github.com/pry/pry-rails.git
synced 2022-11-09 12:36:03 -05:00
Refactor AR show-model and test --grep
This commit is contained in:
parent
72694e070f
commit
9208a850a1
2 changed files with 50 additions and 24 deletions
|
@ -17,21 +17,42 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
||||||
Rails.application.eager_load!
|
Rails.application.eager_load!
|
||||||
|
|
||||||
if defined?(ActiveRecord::Base)
|
if defined?(ActiveRecord::Base)
|
||||||
models = ActiveRecord::Base.descendants.map do |mod|
|
models = ActiveRecord::Base.descendants
|
||||||
model_string = mod.to_s + "\n"
|
|
||||||
if mod.table_exists?
|
display(models.map do |model|
|
||||||
model_string << mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n")
|
model_string = model.to_s + "\n"
|
||||||
|
|
||||||
|
if model.table_exists?
|
||||||
|
model.columns.each do |column|
|
||||||
|
model_string << " #{column.name}: #{column.type.to_s}\n"
|
||||||
|
end
|
||||||
else
|
else
|
||||||
model_string << " Table doesn't exist"
|
model_string << " Table doesn't exist\n"
|
||||||
end
|
end
|
||||||
mod.reflections.each do |model,ref|
|
|
||||||
model_string << "\n #{ref.macro.to_s} #{model}"
|
model.reflections.each do |model, reflection|
|
||||||
model_string << " through #{ref.options[:through]}" unless ref.options[:through].nil?
|
model_string << " #{reflection.macro.to_s} #{model}"
|
||||||
|
|
||||||
|
if reflection.options[:through].present?
|
||||||
|
model_string << " through #{reflection.options[:through]}\n"
|
||||||
|
else
|
||||||
|
model_string << "\n"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
model_string
|
model_string
|
||||||
end.join("\n")
|
end.join)
|
||||||
elsif defined?(Mongoid::Document)
|
end
|
||||||
models = get_files.map do |path|
|
|
||||||
|
if defined?(Mongoid::Document)
|
||||||
|
models = []
|
||||||
|
ObjectSpace.each_object do |o|
|
||||||
|
if o.is_a?(Class) && o.ancestors.include?(Mongoid::Document)
|
||||||
|
models << o
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
display(models.map do |model|
|
||||||
mod = extract_class_name(path)
|
mod = extract_class_name(path)
|
||||||
model_string = "\033[1;34m#{mod.to_s}\033[0m\n"
|
model_string = "\033[1;34m#{mod.to_s}\033[0m\n"
|
||||||
begin
|
begin
|
||||||
|
@ -54,19 +75,17 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
||||||
rescue Exception
|
rescue Exception
|
||||||
STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}"
|
STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}"
|
||||||
end
|
end
|
||||||
end.join("\n")
|
end.join("\n"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def display(string)
|
||||||
|
if opts.present?(:G)
|
||||||
|
regexp = Regexp.new(opts[:G], Regexp::IGNORECASE)
|
||||||
|
string = string.gsub(regexp) { |s| text.red(s) }
|
||||||
end
|
end
|
||||||
|
|
||||||
models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } unless opts[:G].nil?
|
output.puts string
|
||||||
output.puts models
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_files(prefix ='')
|
|
||||||
Dir.glob(prefix << "app/models/**/*.rb")
|
|
||||||
end
|
|
||||||
|
|
||||||
def extract_class_name(filename)
|
|
||||||
filename.split('/')[2..-1].collect { |i| i.camelize }.join('::').chomp(".rb")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def kind_of_relation(string)
|
def kind_of_relation(string)
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe 'show-models' do
|
describe "show-models" do
|
||||||
it 'should print a list of ActiveRecord models' do
|
it "should print a list of ActiveRecord models" do
|
||||||
mock_pry('show-models', 'exit-all').must_equal <<OUTPUT
|
mock_pry('show-models', 'exit-all').must_equal <<OUTPUT
|
||||||
Beer
|
Beer
|
||||||
id: integer
|
id: integer
|
||||||
|
@ -28,4 +28,11 @@ Pokemon
|
||||||
has_many beers through hacker
|
has_many beers through hacker
|
||||||
OUTPUT
|
OUTPUT
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should highlight the given phrase with --grep" do
|
||||||
|
output = mock_pry('show-models --grep beer', 'exit-all')
|
||||||
|
|
||||||
|
output.must_include "\e[0;31mBeer\e[0m"
|
||||||
|
output.must_include "has_many \e[0;31mbeer\e[0ms through hacker"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue