Skip to content

Instantly share code, notes, and snippets.

@mariochavez
Created June 13, 2011 14:56
Show Gist options
  • Save mariochavez/1022919 to your computer and use it in GitHub Desktop.
Save mariochavez/1022919 to your computer and use it in GitHub Desktop.
Autoload Model data
class ApplicationController
include ResourceModel
end
module ResourceModel
extend ActiveSupport::Concern
module ClassMethods
def resource
model_class = self.name.sub(/Controller/, '').singularize
model_class.constantize
end
end
module InstanceMethods
def find_all
working_resource = Kernel.const_get(self.class.resource.to_s)
values = working_resource.send :all
instance_variable_set("@#{self.class.resource.to_s.downcase.pluralize}", values)
end
end
end
class UsersController << ApplicationController
def index
find_all
puts @users
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment