Created
November 19, 2010 00:36
-
-
Save mariochavez/705954 to your computer and use it in GitHub Desktop.
Patrones
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
@modelos = Modelo.all(:params => {:client => @client_code}) |
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
id = params[:id] | |
@modelos = Modelo.all(:params => {:id => id, :client => @client_code}) |
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
def fetch_resource! resource, filters = nil | |
fetch! resource, false, :find, filters | |
end | |
def fetch_resources! resource, filters = nil | |
fetch! resource, true, :all, filters | |
end | |
def fetch! resource, pluralize, method, filters = nil | |
options = {:client => @company_code} | |
options.merge! filters if !filters.nil? | |
working_resource = Kernel.const_get(resource.to_s.capitalize) | |
id = params[:id] | |
if method == :all | |
options[:id] = id if !id.nil? | |
values = working_resource.send method, :params => options | |
else | |
values = working_resource.send method, id, :params => options | |
end | |
variable_name = resource.to_s | |
variable_name = variable_name.pluralize if pluralize == true | |
instance_variable_set("@#{variable_name}", values) | |
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
id = params[:id] | |
@modelo = Modelo.find(id, :params => {:client => @client_code}) |
Revisa este otro gist https://gist.github.com/1022919
Hay un modulo que infiere del nombre del controlador el nombre del modelo, y el nombre de la variable de clase a donde cargar los datos. El modulo se incluye en el ApplicationController y todos los controllers que hereden de el podrán hacer uso de la funcionalidad.
Si esto es lo que buscas, te recomiendo la gema InheritedResources que hace lo mismo que mi ejemplo, y otras cosas mas https://github.com/josevalim/inherited_resources
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Estoy intentando implementar algo parecido, para colocarlo en el ApplicationController. Básicamente es un método al que cualquier controlador puede acceder. La idea es obtener, a través del controlador, el modelo correspondiente y aplicar cambios a un atributo en particular. El problema es cómo puedo obtener el modelo a través del controller (o al menos saber qué controller accede al método).
¿Cómo pudiera implementar tu solución a este problema?