Class: ControllerHelpers::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/controller_helpers.rb

Class Method Summary collapse

Class Method Details

.do_include(controller_name, helper_name, mod = "Module") ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/controller_helpers.rb', line 84

def self.do_include(controller_name, helper_name, mod = "Module")
  begin
    # puts "controller_name = #{controller_name}"
    # puts "helper_name = #{helper_name}"
    # puts "mod = #{mod}"
    mod_const = Module.const_get(mod)
    controller_const = mod_const.const_get(controller_name)
    chelper_const = mod_const.const_get(helper_name.to_s)
    # puts "Including #{helper_name} into #{controller_name}"
    controller_const.send(:include, chelper_const)
    chelper_const.public_instance_methods.each{ |meth|chelper_const.send(:protected, meth) }
  rescue Exception => e
    puts e.message
    puts e.backtrace
  end
end

.find_controller_helpersObject



105
106
107
# File 'lib/controller_helpers.rb', line 105

def self.find_controller_helpers
  self.find_models("#{RAILS_ROOT}/app/controller_helpers")
end

.find_controllersObject



101
102
103
# File 'lib/controller_helpers.rb', line 101

def self.find_controllers
  (self.find_models("#{RAILS_ROOT}/app/controllers")).flatten
end

.find_models(dir) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/controller_helpers.rb', line 109

def self.find_models(dir)
  require "#{RAILS_ROOT}/app/controllers/application"
  controllers = []
  # puts "dir = #{dir}"
  Find.find(dir) do |f|
    if FileTest.directory?(f) or f.match(/\.svn/)
    else
      if FileTest.file?(f)
        model = ControllerHelpers::Model.new(f)
        r = model.file_path.gsub(File.extname(model.file_path), "")
        # puts "require: #{r}"
        require r
        controllers << model
      end
    end
  end
  controllers
end

.runObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/controller_helpers.rb', line 128

def self.run
  controllers = self.find_controllers

  controller_helpers = self.find_controller_helpers

  controllers.each do |controller|
    # puts "Let's attach the helper for #{controller}"
    hname = "#{controller.class_name_camel}Helper"
    if controller_helpers.collect{|x| x.to_s }.include?("#{controller}Helper")
      do_include(controller.class_name_camel.to_s, hname.to_s, controller.module_camel)
    else
      puts "Warning: This is no helper (#{hname}) for controller #{controller}"
    end
  end    
end