mirror of
https://github.com/pry/pry-rails.git
synced 2022-11-09 12:36:03 -05:00
Refactor and test mongoid models (w/o color for now)
This commit is contained in:
parent
9208a850a1
commit
02da9d5018
4 changed files with 123 additions and 63 deletions
|
@ -5,11 +5,13 @@ end
|
|||
|
||||
appraise "rails31" do
|
||||
gem "rails", "3.1.6"
|
||||
gem "mongoid"
|
||||
gem "sqlite3"
|
||||
end
|
||||
|
||||
appraise "rails32" do
|
||||
gem "rails", "3.2.6"
|
||||
gem "mongoid"
|
||||
gem "sqlite3"
|
||||
end
|
||||
|
||||
|
|
|
@ -16,66 +16,78 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
|||
def process
|
||||
Rails.application.eager_load!
|
||||
|
||||
if defined?(ActiveRecord::Base)
|
||||
models = ActiveRecord::Base.descendants
|
||||
display_activerecord_models
|
||||
display_mongoid_models
|
||||
end
|
||||
|
||||
display(models.map do |model|
|
||||
model_string = model.to_s + "\n"
|
||||
def display_activerecord_models
|
||||
return unless defined?(ActiveRecord::Base)
|
||||
|
||||
if model.table_exists?
|
||||
model.columns.each do |column|
|
||||
model_string << " #{column.name}: #{column.type.to_s}\n"
|
||||
end
|
||||
models = ActiveRecord::Base.descendants
|
||||
|
||||
models.sort_by(&:to_s).each do |model|
|
||||
model_string = "#{model}\n"
|
||||
|
||||
if model.table_exists?
|
||||
model.columns.each do |column|
|
||||
model_string << " #{column.name}: #{column.type.to_s}\n"
|
||||
end
|
||||
else
|
||||
model_string << " Table doesn't exist\n"
|
||||
end
|
||||
|
||||
model.reflections.each do |model, reflection|
|
||||
model_string << " #{reflection.macro.to_s} #{model}"
|
||||
|
||||
if reflection.options[:through].present?
|
||||
model_string << " through #{reflection.options[:through]}\n"
|
||||
else
|
||||
model_string << " Table doesn't exist\n"
|
||||
end
|
||||
|
||||
model.reflections.each do |model, reflection|
|
||||
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
|
||||
|
||||
model_string
|
||||
end.join)
|
||||
end
|
||||
|
||||
if defined?(Mongoid::Document)
|
||||
models = []
|
||||
ObjectSpace.each_object do |o|
|
||||
if o.is_a?(Class) && o.ancestors.include?(Mongoid::Document)
|
||||
models << o
|
||||
model_string << "\n"
|
||||
end
|
||||
end
|
||||
|
||||
display(models.map do |model|
|
||||
mod = extract_class_name(path)
|
||||
model_string = "\033[1;34m#{mod.to_s}\033[0m\n"
|
||||
begin
|
||||
if mod.constantize.included_modules.include?(Mongoid::Document)
|
||||
model_string << mod.constantize.fields.values.sort_by(&:name).map { |col|
|
||||
" #{col.name}: \033[1;33m#{col.options[:type].to_s.downcase}\033[0m"
|
||||
}.join("\n")
|
||||
mod.constantize.relations.each do |model,ref|
|
||||
model_string << "\n #{kind_of_relation(ref.relation.to_s)} \033[1;34m#{model}\033[0m"
|
||||
model_string << ", autosave" if ref.options[:autosave]
|
||||
model_string << ", autobuild" if ref.options[:autobuild]
|
||||
model_string << ", validate" if ref.options[:validate]
|
||||
model_string << ", dependent-#{ref.options[:dependent]}" if ref.options[:dependent]
|
||||
end
|
||||
else
|
||||
model_string << " Collection doesn't exist"
|
||||
end
|
||||
model_string
|
||||
display model_string
|
||||
end
|
||||
end
|
||||
|
||||
rescue Exception
|
||||
STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}"
|
||||
def display_mongoid_models
|
||||
return unless defined?(Mongoid::Document)
|
||||
|
||||
models = []
|
||||
|
||||
ObjectSpace.each_object do |o|
|
||||
is_model = false
|
||||
|
||||
begin
|
||||
is_model = o.class == Class && o.ancestors.include?(Mongoid::Document)
|
||||
rescue => e
|
||||
# If it's a weird object, it's not what we want anyway.
|
||||
end
|
||||
|
||||
models << o if is_model
|
||||
end
|
||||
|
||||
models.sort_by(&:to_s).each do |model|
|
||||
model_string = "#{model}\n"
|
||||
|
||||
model.fields.values.sort_by(&:name).each do |column|
|
||||
model_string << " #{column.name}: #{column.options[:type]}\n"
|
||||
end
|
||||
|
||||
model.relations.each do |other_model, ref|
|
||||
model_string << " #{kind_of_relation(ref.relation)} #{other_model}"
|
||||
model_string << ", autosave" if ref.options[:autosave]
|
||||
model_string << ", autobuild" if ref.options[:autobuild]
|
||||
model_string << ", validate" if ref.options[:validate]
|
||||
|
||||
if ref.options[:dependent]
|
||||
model_string << ", dependent-#{ref.options[:dependent]}"
|
||||
end
|
||||
end.join("\n"))
|
||||
|
||||
model_string << "\n"
|
||||
end
|
||||
|
||||
display model_string
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -88,14 +100,14 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
|||
output.puts string
|
||||
end
|
||||
|
||||
def kind_of_relation(string)
|
||||
case string.gsub('Mongoid::Relations::', '')
|
||||
when 'Referenced::Many' then 'has_many'
|
||||
when 'Referenced::One' then 'has_one'
|
||||
when 'Referenced::In' then 'belongs_to'
|
||||
when 'Embedded::Many' then 'embeds_many'
|
||||
when 'Embedded::One' then 'embeds_one'
|
||||
when 'Embedded::In' then 'embedded_in'
|
||||
def kind_of_relation(relation)
|
||||
case relation.to_s.sub(/^Mongoid::Relations::/, '')
|
||||
when 'Referenced::Many' then 'has_many'
|
||||
when 'Referenced::One' then 'has_one'
|
||||
when 'Referenced::In' then 'belongs_to'
|
||||
when 'Embedded::Many' then 'embeds_many'
|
||||
when 'Embedded::One' then 'embeds_one'
|
||||
when 'Embedded::In' then 'embedded_in'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -57,3 +57,23 @@ class Pokemon < ActiveRecord::Base
|
|||
belongs_to :hacker
|
||||
has_many :beers, through: :hacker
|
||||
end
|
||||
|
||||
begin
|
||||
require 'mongoid'
|
||||
|
||||
class Artist
|
||||
include Mongoid::Document
|
||||
|
||||
field :name, type: String
|
||||
embeds_one :beer
|
||||
embeds_many :instruments
|
||||
end
|
||||
|
||||
class Instrument
|
||||
include Mongoid::Document
|
||||
|
||||
field :name, type: String
|
||||
embedded_in :artist
|
||||
end
|
||||
rescue LoadError # Mongoid doesn't support Rails 3.0 or 4.0
|
||||
end
|
||||
|
|
|
@ -3,8 +3,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe "show-models" do
|
||||
it "should print a list of ActiveRecord models" do
|
||||
mock_pry('show-models', 'exit-all').must_equal <<OUTPUT
|
||||
it "should print a list of models" do
|
||||
output = mock_pry('show-models', 'exit-all')
|
||||
|
||||
ar_models = <<MODELS
|
||||
Beer
|
||||
id: integer
|
||||
name: string
|
||||
|
@ -26,7 +28,27 @@ Pokemon
|
|||
abilities: string
|
||||
belongs_to hacker
|
||||
has_many beers through hacker
|
||||
OUTPUT
|
||||
MODELS
|
||||
|
||||
mongoid_models = <<MODELS
|
||||
Artist
|
||||
_id: Moped::BSON::ObjectId
|
||||
_type: String
|
||||
name: String
|
||||
embeds_one beer, validate
|
||||
embeds_many instruments, validate
|
||||
Instrument
|
||||
_id: Moped::BSON::ObjectId
|
||||
_type: String
|
||||
name: String
|
||||
embedded_in artist
|
||||
MODELS
|
||||
|
||||
if defined?(Mongoid)
|
||||
output.must_equal [ar_models, mongoid_models].join
|
||||
else
|
||||
output.must_equal ar_models
|
||||
end
|
||||
end
|
||||
|
||||
it "should highlight the given phrase with --grep" do
|
||||
|
@ -34,5 +56,9 @@ OUTPUT
|
|||
|
||||
output.must_include "\e[0;31mBeer\e[0m"
|
||||
output.must_include "has_many \e[0;31mbeer\e[0ms through hacker"
|
||||
|
||||
if defined?(Mongoid)
|
||||
output.must_include "embeds_one \e[0;31mbeer\e[0m"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue