Sha256: 1b333a8dc5e882c65440af7b48ddf131b074773e17694c40ee11b5de8d4e9a80

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'formie/engine.rb'

module Formie

  def self.reload
    now = Time.now
    @last_update ||= Time.new(0)
    self.load_formies(::ActionView::Helpers::FormBuilder, 'app/formies/forms')
    self.load_formies(::ActionView::Helpers::TextHelper,  'app/formies/application')
    self.load_formies(::ActionView::Helpers::TextHelper,  'app/formies/templates')
    @last_update = now
  end

  def self.define_formie(where, name, txt)
    formiename = name

    where.send(:define_method, formiename, lambda {|*args, &block|
#p "** called #{where} #{formiename}", args, block
      params = args.extract_options!
      options = {}
      options[:inline] = txt
      options[:locals] = {}
      options[:locals].update params
      options[:locals].update :formiename => formiename,
		:block => block, :form => self, :args => args
      defined?(controller) == 'method' ?
	controller.render_to_body(options) :
	@template.render(options)
    })
#p "** defined  #{where} #{formiename}"
  end


 private
  def self.load_formies(where, dir)
    dir = "#{::Rails.root.to_s}/#{dir}"
    return  unless File.exists?(dir)
    Dir.chdir(dir) {|current_dir|
      Dir.glob('**/*.html.erb').each {|path|
	next  if File.new(path).mtime < @last_update

	name = File.basename(path, '.html.erb')
	self.inject(where, name, File.open(path).read)
      }
    }
  end

  def self.inject(where, name, txt)
#puts "** loading formie '#{where}' - '#{name}'"
    txt = txt.force_encoding('UTF-8')  if txt.respond_to?(:force_encoding)
    where.define_formie name, txt
  end

end


module ActionView::Helpers::TextHelper
  def self.define_formie(name, txt)
    Formie.define_formie(self, name, txt)
  end
end

class ActionView::Helpers::FormBuilder
  def self.define_formie(name, txt)
    Formie.define_formie(self, name, txt)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formie-0.6.6 lib/formie.rb