Skip to content

Instantly share code, notes, and snippets.

@dyoder
Created August 14, 2009 23:56
Show Gist options
  • Save dyoder/168159 to your computer and use it in GitHub Desktop.
Save dyoder/168159 to your computer and use it in GitHub Desktop.
module MetaHelper
# include this anywhere you want to refer to the model, controller, etc., generically
# all methods are prefixed with x_ to avoid name clashes with existing methods
def x_controller_class ; self.class ; end
def x_controller_name ; self.class.name.underscore.gsub('_controller', '') ; end
def x_model_name ; x_controller_name.singularize ; end
def x_model_class ; x_model_name.camelize.constantize ; end
def x_mailer_name ; x_model_name + "_mailer" ; end
def x_mailer_class ; x_mailer_name.camelize.constantize ; end
def x_instance ; instance_variable_get( "@#{x_model_name}" ) ; end
def x_collection ; instance_variable_get( "@#{x_model_name.pluralize}" ) ; end
def x_find( id ) ; instance_variable_set( "@#{x_model_name}", x_model_class.find( id ) ) ; end
def x_find_all ; instance_variable_set( "@#{x_model_name.pluralize}", x_model_class.find( :all ) ) ; end
def t( key, options = {} )
I18n.t(
( key =~ /^\./ ? "#{x_controller_name}.#{params[:action]}#{key}" : key ),
options )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment