1
0
Fork 0
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:
Ryan Fitzgerald 2012-09-22 18:11:58 -07:00
parent 9208a850a1
commit 02da9d5018
4 changed files with 123 additions and 63 deletions

View file

@ -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

View file

@ -16,66 +16,78 @@ 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
models = ActiveRecord::Base.descendants display_mongoid_models
end
display(models.map do |model| def display_activerecord_models
model_string = model.to_s + "\n" return unless defined?(ActiveRecord::Base)
if model.table_exists? models = ActiveRecord::Base.descendants
model.columns.each do |column|
model_string << " #{column.name}: #{column.type.to_s}\n" models.sort_by(&:to_s).each do |model|
end 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 else
model_string << " Table doesn't exist\n" model_string << "\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
end end
end end
display(models.map do |model| display model_string
mod = extract_class_name(path) end
model_string = "\033[1;34m#{mod.to_s}\033[0m\n" end
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
rescue Exception def display_mongoid_models
STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}" 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
end.join("\n"))
model_string << "\n"
end
display model_string
end end
end end
@ -88,14 +100,14 @@ 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'
when 'Embedded::Many' then 'embeds_many' when 'Embedded::Many' then 'embeds_many'
when 'Embedded::One' then 'embeds_one' when 'Embedded::One' then 'embeds_one'
when 'Embedded::In' then 'embedded_in' when 'Embedded::In' then 'embedded_in'
end end
end end
end end

View file

@ -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

View file

@ -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