Created
June 13, 2011 14:56
-
-
Save mariochavez/1022919 to your computer and use it in GitHub Desktop.
Autoload Model data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController | |
include ResourceModel | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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