controller_helpers 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ # require 'controller_helpers'
@@ -0,0 +1,145 @@
1
+ require 'find'
2
+ require "#{RAILS_ROOT}/app/controllers/application"
3
+
4
+ class Module
5
+
6
+ def include_into_controller(*controller_names)
7
+ controller_names.each do |name|
8
+ name = name.to_s
9
+ mod = "Module"
10
+ if name.match(/\//)
11
+ mod = name.split("/").first
12
+ name = name.split("/").last
13
+ end
14
+ ControllerHelpers::Runner.do_include("#{name.camelcase}Controller", self.to_s, mod.camelcase)
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ module ControllerHelpers
21
+
22
+ class Model
23
+ attr_accessor :file_path
24
+ attr_accessor :file_name
25
+ attr_accessor :directory
26
+ attr_accessor :class_name
27
+ attr_accessor :class_with_module
28
+ attr_accessor :class_name_camel
29
+ attr_accessor :class_with_module_camel
30
+ attr_accessor :module
31
+ attr_accessor :module_camel
32
+
33
+ def initialize(file_path)
34
+ # puts "file_path = #{file_path}"
35
+ self.file_path = file_path
36
+ # puts "self.file_path = #{self.file_path}"
37
+ self.file_name = File.basename(file_path, File.extname(file_path))
38
+ # puts "self.file_name = #{self.file_name}"
39
+ self.directory = File.dirname(file_path)
40
+ # puts "self.directory = #{self.directory}"
41
+ self.class_name = self.file_name
42
+ # puts "self.class_name = #{self.class_name}"
43
+ if self.directory.match(/\/controllers/)
44
+ # handle if they're controllers
45
+ self.class_with_module = self.file_path.gsub("#{RAILS_ROOT}/app/controllers/", "").gsub(File.extname(self.file_path), "")
46
+ elsif self.directory.match(/\/controller_helpers/)
47
+ # handle if they're controller helpers
48
+ self.class_with_module = self.file_path.gsub("#{RAILS_ROOT}/app/controller_helpers/", "").gsub(File.extname(self.file_path), "")
49
+ else
50
+ raise "What the hell!?! #{self.directory}"
51
+ end
52
+ # puts "self.class_with_module = #{self.class_with_module}"
53
+ if self.class_name == "application"
54
+ self.class_name = "application_controller"
55
+ # puts "self.class_name = #{self.class_name}"
56
+ self.class_with_module = self.class_name
57
+ # puts "self.class_with_module = #{self.class_with_module}"
58
+ end
59
+ self.class_name_camel = self.class_name.camelcase
60
+ # puts "self.class_name_camel = #{self.class_name_camel}"
61
+ self.class_with_module_camel = self.class_with_module.camelcase
62
+ # puts "self.class_with_module_camel = #{self.class_with_module_camel}"
63
+ if self.class_with_module.match(/\//)
64
+ self.module = self.class_with_module.gsub("/#{self.class_name}", "")
65
+ # puts "self.module = #{self.module}"
66
+ self.module_camel = self.module.camelcase
67
+ # puts "self.module_camel = #{self.module_camel}"
68
+ else
69
+ self.module = "module"
70
+ self.module_camel = "Module"
71
+ end
72
+ end
73
+
74
+ def to_s
75
+ self.class_with_module_camel
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ class ControllerHelpers::Runner
83
+
84
+ def self.do_include(controller_name, helper_name, mod = "Module")
85
+ begin
86
+ # puts "controller_name = #{controller_name}"
87
+ # puts "helper_name = #{helper_name}"
88
+ # puts "mod = #{mod}"
89
+ mod_const = Module.const_get(mod)
90
+ controller_const = mod_const.const_get(controller_name)
91
+ chelper_const = mod_const.const_get(helper_name.to_s)
92
+ # puts "Including #{helper_name} into #{controller_name}"
93
+ controller_const.send(:include, chelper_const)
94
+ chelper_const.public_instance_methods.each{ |meth|chelper_const.send(:protected, meth) }
95
+ rescue Exception => e
96
+ puts e.message
97
+ puts e.backtrace
98
+ end
99
+ end
100
+
101
+ def self.find_controllers
102
+ (self.find_models("#{RAILS_ROOT}/app/controllers")).flatten
103
+ end
104
+
105
+ def self.find_controller_helpers
106
+ self.find_models("#{RAILS_ROOT}/app/controller_helpers")
107
+ end
108
+
109
+ def self.find_models(dir)
110
+ controllers = []
111
+ # puts "dir = #{dir}"
112
+ Find.find(dir) do |f|
113
+ if FileTest.directory?(f) or f.match(/.svn/)
114
+ else
115
+ if FileTest.file?(f)
116
+ model = ControllerHelpers::Model.new(f)
117
+ r = model.file_path.gsub(File.extname(model.file_path), "")
118
+ # puts "require: #{r}"
119
+ require r
120
+ controllers << model
121
+ end
122
+ end
123
+ end
124
+ controllers
125
+ end
126
+
127
+ def self.run
128
+ controllers = self.find_controllers
129
+
130
+ controller_helpers = self.find_controller_helpers
131
+
132
+ controllers.each do |controller|
133
+ # puts "Let's attach the helper for #{controller}"
134
+ hname = "#{controller.class_name_camel}Helper"
135
+ if controller_helpers.collect{|x| x.to_s }.include?("#{controller}Helper")
136
+ do_include(controller.class_name_camel.to_s, hname.to_s, controller.module_camel)
137
+ else
138
+ puts "Warning: This is no helper (#{hname}) for controller #{controller}"
139
+ end
140
+ end
141
+ end
142
+
143
+ end
144
+
145
+ ControllerHelpers::Runner.run
@@ -0,0 +1,5 @@
1
+ --- !ruby/object:RubyForgeConfig
2
+ gem_name: controller_helpers
3
+ package: controller_helpers
4
+ project: magrathea
5
+ version: 1.0.0
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: controller_helpers
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-09-11 00:00:00 -04:00
8
+ summary: controller_helpers
9
+ require_paths:
10
+ - lib
11
+ - lib
12
+ - lib
13
+ - lib/tasks
14
+ email:
15
+ homepage:
16
+ rubyforge_project: magrathea
17
+ description: "controller_helpers was developed by: markbates"
18
+ autorequire:
19
+ - controller_helpers
20
+ - controller_helpers
21
+ default_executable:
22
+ bindir: bin
23
+ has_rdoc: false
24
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
+ requirements:
26
+ - - ">"
27
+ - !ruby/object:Gem::Version
28
+ version: 0.0.0
29
+ version:
30
+ platform: ruby
31
+ signing_key:
32
+ cert_chain:
33
+ post_install_message:
34
+ authors:
35
+ - markbates
36
+ files:
37
+ - init.rb
38
+ - lib/controller_helpers.rb
39
+ - lib/tasks/rubyforge_config.yml
40
+ test_files: []
41
+
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+