Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# In irb, can type:
# people/6 instead of Person.find(6)
# That is, can paste in urls into irb to find objects.
class ModelProxy
  def initialize(klass)
    @klass = klass
  end
  def /(id)
    @klass.find(id)
  end
end

def define_model_find_shortcuts
  model_files = Dir.glob("app/models/**/*.rb")
  model_names = model_files.map { |f| File.basename(f).split('.')[0..-2].join }
  model_names.each do |model_name|
    Object.instance_eval do
      define_method(model_name.pluralize) do |*args|
        ModelProxy.new(model_name.camelize.constantize)
      end
    end
  end
end