Improved performance by caching the result process wide instead of globing and accessing the database multiple times.

This commit is contained in:
Thorsten Eckel 2018-01-24 14:30:04 +01:00
parent 38d757c65b
commit 57294646a9

View file

@ -25,34 +25,37 @@ returns
=end =end
def self.all def self.all
all = {} @all ||= begin
dir = Rails.root.join('app', 'models').to_s all = {}
Dir.glob("#{dir}/**/*.rb" ) do |entry| dir = Rails.root.join('app', 'models').to_s
next if entry.match?(/application_model/i) tables = ActiveRecord::Base.connection.tables
next if entry.match?(%r{channel/}i) Dir.glob("#{dir}/**/*.rb") do |entry|
next if entry.match?(%r{observer/}i) next if entry.match?(/application_model/i)
next if entry.match?(%r{store/provider/}i) next if entry.match?(%r{channel/}i)
next if entry.match?(%r{models/concerns/}i) next if entry.match?(%r{observer/}i)
next if entry.match?(%r{store/provider/}i)
next if entry.match?(%r{models/concerns/}i)
entry.gsub!(dir, '') entry.gsub!(dir, '')
entry = entry.to_classname entry = entry.to_classname
model_class = load_adapter(entry) model_class = load_adapter(entry)
next if !model_class next if !model_class
next if !model_class.respond_to? :new next if !model_class.respond_to? :new
next if !model_class.respond_to? :table_name next if !model_class.respond_to? :table_name
table_name = model_class.table_name # handle models where not table exists, pending migrations table_name = model_class.table_name # handle models where not table exists, pending migrations
next if !ActiveRecord::Base.connection.tables.include?(table_name) next if !tables.include?(table_name)
model_object = model_class.new model_object = model_class.new
next if !model_object.respond_to? :attributes next if !model_object.respond_to? :attributes
all[model_class] = {} all[model_class] = {}
all[model_class][:attributes] = model_class.attribute_names all[model_class][:attributes] = model_class.attribute_names
all[model_class][:reflections] = model_class.reflections all[model_class][:reflections] = model_class.reflections
all[model_class][:table] = model_class.table_name all[model_class][:table] = model_class.table_name
#puts model_class #puts model_class
#puts "rrrr #{all[model_class][:attributes]}" #puts "rrrr #{all[model_class][:attributes]}"
#puts " #{model_class.attribute_names.inspect}" #puts " #{model_class.attribute_names.inspect}"
end
all
end end
all
end end
=begin =begin