hmvc.rb
1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module HMVC
# controller_paths by context and controller
mattr_accessor :paths_by_context
self.paths_by_context = {}
module ClassMethods
def hmvc context, options = {}
class_attribute :inherit_templates
class_attribute :hmvc_inheritable
class_attribute :hmvc_context
class_attribute :hmvc_paths
self.inherit_templates = true
self.hmvc_inheritable = true
self.hmvc_context = context
self.hmvc_paths = (HMVC.paths_by_context[self.hmvc_context] ||= {})
class_attribute :hmvc_orders_context
self.hmvc_orders_context = options[:orders_context] || self.superclass.hmvc_orders_context rescue nil
# initialize other context's controllers paths
controllers = [self] + context.controllers.map{ |controller| controller.constantize }
controllers.each do |klass|
context_klass = klass
while ((klass = klass.superclass).hmvc_inheritable rescue false)
self.hmvc_paths[klass.controller_path] ||= context_klass.controller_path
end
end
include InstanceMethods
helper UrlHelpers
end
def hmvc_lookup_path controller_path
self.hmvc_paths[controller_path] || controller_path
end
end
module InstanceMethods
protected
end
module UrlHelpers
def url_for options = {}
return super unless options.is_a? Hash
controller_path = options[:controller]
controller_path ||= self.controller_path
controller_path = controller_path.to_s
dest_controller = self.controller.class.hmvc_lookup_path controller_path
options[:controller] = dest_controller
super
end
end
end