Apr 01
This all came up b/c I wanted to write a shared mixin module to have attachment_fu ready actions for all my controllers that have attachments. No need to put that crap in every controller. So in a mixin module when you want to do some fun reflection/introspection like so:
…
@this_obj = self.controller_name.classify.constantize.find_safe (id,logged_in_user.id)
…
In Dev this worked great. Tests were good, my mostly view/template developer partner, approved. Deploy to test and WHAMMO!
…
"NameError ("Blogs | ListAll" is not a valid constant name!):
…”
What gives? Mmm, seems when you include a mixin, that is included in a controller. The self.controller_name will report incorrectly in Test and Prod. So I had to adjust the code a little using a new method:
@this_obj = self.this_controller_name.classify.constantize.find_safe( id,logged_in_user.id)
Below is the method(which is basically David’s code for controller_name).
###############################
#This method is here b/c it is apparent that Rails #ActionController.controller_name
# is unreliable in Test and Production for some reason.
# attachment_symbol_name: Blogs | List_all_attachments
# attachment_model ERROR: "Blogs | ListAllAttachment" #is not a valid constant name
###############################
def this_controller_name
self_class = self.class.to_s.sub(/Controller$/, '').underscore
end
