Created
December 15, 2010 02:06
-
-
Save chewbranca/741524 to your computer and use it in GitHub Desktop.
For the times when Rails MVC doesn't cut it... MVFuckit!
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 MVFuckit | |
def cmeth *args | |
@util_controller ||= ApplicationController.new | |
@util_controller.send(*args) | |
end | |
def hmeth *args | |
@util_helper ||= MVFuckit::Help.new | |
@util_helper.send(*args) | |
end | |
class Help | |
def initialize | |
helpers_dir = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") | |
extract = /^#{Regexp.quote(helpers_dir)}\/?(.*)_helper.rb$/ | |
helpers = Dir["#{helpers_dir}/**/*_helper.rb"].map { |file| file.sub extract, '\1' } | |
helpers.each do |helper| | |
file_name = helper.to_s.underscore + '_helper' | |
class_name = file_name.camelize | |
begin | |
require_dependency(file_name) | |
rescue LoadError => load_error | |
requiree = / -- (.*?)(\.rb)?$/.match(load_error.message).to_a[1] | |
if requiree == file_name | |
msg = "Missing helper file helpers/#{file_name}.rb" | |
raise LoadError.new(msg).copy_blame!(load_error) | |
else | |
raise | |
end | |
end | |
self.class_eval { include class_name.constantize } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment