Skip to content

Instantly share code, notes, and snippets.

@zhum
Created April 4, 2020 07:21
Show Gist options
  • Save zhum/c64ef311df40af9928a3d8e50f490da8 to your computer and use it in GitHub Desktop.
Save zhum/c64ef311df40af9928a3d8e50f490da8 to your computer and use it in GitHub Desktop.
module Comments
class ModelsList
if Rails.env.production?
def self.to_a
@@to_a ||= load
end
def self.to_a_labels
@@to_a_labels ||= to_a.map { |m| "#{m}|#{eval(m).model_name.human}"}
end
else
def self.to_a
load
end
def self.to_a_labels
to_a.map { |m| "#{m}|#{eval(m).model_name.human}"}
end
end
def self.load
res = []
Rails::Engine.subclasses.map do |r|
mod_name = eval(r.name.split(/::/).first)
res += mod_name.constants.select do |c|
cl = eval("#{mod_name}::#{c}")
cl.is_a?(Class) && cl < ActiveRecord::Base
end.map{ |c| "#{mod_name}::#{c}" }
end
res += Module.constants.select do |c|
cl = eval("#{c}")
cl.is_a?(Class) && cl < ActiveRecord::Base
end.map(&:to_s)
res.sort
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment