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
|
appraise "rails31" do
|
||||||
gem "rails", "3.1.6"
|
gem "rails", "3.1.6"
|
||||||
|
gem "mongoid"
|
||||||
gem "sqlite3"
|
gem "sqlite3"
|
||||||
end
|
end
|
||||||
|
|
||||||
appraise "rails32" do
|
appraise "rails32" do
|
||||||
gem "rails", "3.2.6"
|
gem "rails", "3.2.6"
|
||||||
|
gem "mongoid"
|
||||||
gem "sqlite3"
|
gem "sqlite3"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,11 +16,17 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
||||||
def process
|
def process
|
||||||
Rails.application.eager_load!
|
Rails.application.eager_load!
|
||||||
|
|
||||||
if defined?(ActiveRecord::Base)
|
display_activerecord_models
|
||||||
|
display_mongoid_models
|
||||||
|
end
|
||||||
|
|
||||||
|
def display_activerecord_models
|
||||||
|
return unless defined?(ActiveRecord::Base)
|
||||||
|
|
||||||
models = ActiveRecord::Base.descendants
|
models = ActiveRecord::Base.descendants
|
||||||
|
|
||||||
display(models.map do |model|
|
models.sort_by(&:to_s).each do |model|
|
||||||
model_string = model.to_s + "\n"
|
model_string = "#{model}\n"
|
||||||
|
|
||||||
if model.table_exists?
|
if model.table_exists?
|
||||||
model.columns.each do |column|
|
model.columns.each do |column|
|
||||||
|
@ -40,42 +46,48 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
model_string
|
display model_string
|
||||||
end.join)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if defined?(Mongoid::Document)
|
def display_mongoid_models
|
||||||
|
return unless defined?(Mongoid::Document)
|
||||||
|
|
||||||
models = []
|
models = []
|
||||||
|
|
||||||
ObjectSpace.each_object do |o|
|
ObjectSpace.each_object do |o|
|
||||||
if o.is_a?(Class) && o.ancestors.include?(Mongoid::Document)
|
is_model = false
|
||||||
models << o
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
display(models.map do |model|
|
models << o if is_model
|
||||||
mod = extract_class_name(path)
|
end
|
||||||
model_string = "\033[1;34m#{mod.to_s}\033[0m\n"
|
|
||||||
begin
|
models.sort_by(&:to_s).each do |model|
|
||||||
if mod.constantize.included_modules.include?(Mongoid::Document)
|
model_string = "#{model}\n"
|
||||||
model_string << mod.constantize.fields.values.sort_by(&:name).map { |col|
|
|
||||||
" #{col.name}: \033[1;33m#{col.options[:type].to_s.downcase}\033[0m"
|
model.fields.values.sort_by(&:name).each do |column|
|
||||||
}.join("\n")
|
model_string << " #{column.name}: #{column.options[:type]}\n"
|
||||||
mod.constantize.relations.each do |model,ref|
|
end
|
||||||
model_string << "\n #{kind_of_relation(ref.relation.to_s)} \033[1;34m#{model}\033[0m"
|
|
||||||
|
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 << ", autosave" if ref.options[:autosave]
|
||||||
model_string << ", autobuild" if ref.options[:autobuild]
|
model_string << ", autobuild" if ref.options[:autobuild]
|
||||||
model_string << ", validate" if ref.options[:validate]
|
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
|
|
||||||
|
|
||||||
rescue Exception
|
if ref.options[:dependent]
|
||||||
STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}"
|
model_string << ", dependent-#{ref.options[:dependent]}"
|
||||||
end
|
end
|
||||||
end.join("\n"))
|
|
||||||
|
model_string << "\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
display model_string
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -88,8 +100,8 @@ PryRails::Commands.create_command "show-models", "Show all defined models." do
|
||||||
output.puts string
|
output.puts string
|
||||||
end
|
end
|
||||||
|
|
||||||
def kind_of_relation(string)
|
def kind_of_relation(relation)
|
||||||
case string.gsub('Mongoid::Relations::', '')
|
case relation.to_s.sub(/^Mongoid::Relations::/, '')
|
||||||
when 'Referenced::Many' then 'has_many'
|
when 'Referenced::Many' then 'has_many'
|
||||||
when 'Referenced::One' then 'has_one'
|
when 'Referenced::One' then 'has_one'
|
||||||
when 'Referenced::In' then 'belongs_to'
|
when 'Referenced::In' then 'belongs_to'
|
||||||
|
|
|
@ -57,3 +57,23 @@ class Pokemon < ActiveRecord::Base
|
||||||
belongs_to :hacker
|
belongs_to :hacker
|
||||||
has_many :beers, through: :hacker
|
has_many :beers, through: :hacker
|
||||||
end
|
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'
|
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 models" do
|
||||||
mock_pry('show-models', 'exit-all').must_equal <<OUTPUT
|
output = mock_pry('show-models', 'exit-all')
|
||||||
|
|
||||||
|
ar_models = <<MODELS
|
||||||
Beer
|
Beer
|
||||||
id: integer
|
id: integer
|
||||||
name: string
|
name: string
|
||||||
|
@ -26,7 +28,27 @@ Pokemon
|
||||||
abilities: string
|
abilities: string
|
||||||
belongs_to hacker
|
belongs_to hacker
|
||||||
has_many beers through 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
|
end
|
||||||
|
|
||||||
it "should highlight the given phrase with --grep" do
|
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 "\e[0;31mBeer\e[0m"
|
||||||
output.must_include "has_many \e[0;31mbeer\e[0ms through hacker"
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue