Skip to content

Instantly share code, notes, and snippets.

@jszmajda
Created April 16, 2010 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jszmajda/368498 to your computer and use it in GitHub Desktop.
Save jszmajda/368498 to your computer and use it in GitHub Desktop.
<%# bar baz %>
You put your <%= bar %> in my <%= baz %>!
module FooHelper
include PartialMethods
end
<%= p_some_partial("chocolate", "peanut butter") %>
require 'find'
module PartialMethods
def self.included(base)
path = "#{Rails.root}/app/views/#{base.name.sub(/Helper$/,"").downcase}"
Find.find(path) do |file|
if file =~ /#{path}\/_(.+)/
partial = $~[1].split(/\./)[0]
required_arguments = []
header = File.readlines(file)[0]
if header =~ /^<%# +?(.*?) +?%>$/
puts "header: #{header}"
required_arguments = $~[1].split(/ +/)
end
method = Proc.new do |*args|
if args.length != required_arguments.length
raise ArgumentError, "wrong number of arguments (#{args.length} for #{required_argumentss.length})"
end
options = {}
required_arguments.each_with_index do |arg, i|
options[arg.to_sym] = args[i]
end
render partial.to_s, options
end
define_method "p_#{partial}".to_sym, &method
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment