Class: Usher::Interface::Rails23Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/interface/rails2_3_interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRails23Interface

Returns a new instance of Rails23Interface.



35
36
37
# File 'lib/usher/interface/rails2_3_interface.rb', line 35

def initialize
  reset!
end

Instance Attribute Details

#configuration_filesObject (readonly)

Returns the value of attribute configuration_files.



5
6
7
# File 'lib/usher/interface/rails2_3_interface.rb', line 5

def configuration_files
  @configuration_files
end

Instance Method Details

#add_configuration_file(file) ⇒ Object



39
40
41
# File 'lib/usher/interface/rails2_3_interface.rb', line 39

def add_configuration_file(file)
  @configuration_files << file
end

#add_named_route(name, route, options = {}) ⇒ Object



11
12
13
# File 'lib/usher/interface/rails2_3_interface.rb', line 11

def add_named_route(name, route, options = {})
  @router.add_route(route, options).name(name)
end

#add_route(path, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/usher/interface/rails2_3_interface.rb', line 15

def add_route(path, options = {})
  if !@controller_action_route_added && path =~ %r{^/?:controller/:action/:id$}
    add_route('/:controller/:action', options.dup)
    @controller_action_route_added = true 
  end

  if !@controller_route_added && path =~ %r{^/?:controller/:action$}
    add_route('/:controller', options.merge({:action => 'index'}))
    @controller_route_added = true 
  end
  
  options[:action] = 'index' unless options[:action]

  path[0, 0] = '/' unless path[0] == ?/
  route = @router.add_route(path, options).to(options)
  
  raise "your route must include a controller" unless (route.paths.first.dynamic_keys && route.paths.first.dynamic_keys.include?(:controller)) || route.destination.include?(:controller)
  route
end

#call(env) ⇒ Object



61
62
63
64
65
# File 'lib/usher/interface/rails2_3_interface.rb', line 61

def call(env)
  request = ActionController::Request.new(env)
  app = recognize(request)
  app.call(env).to_a
end

#draw {|ActionController::Routing::RouteSet::Mapper.new(self)| ... } ⇒ Object

Yields:

  • (ActionController::Routing::RouteSet::Mapper.new(self))


82
83
84
85
86
# File 'lib/usher/interface/rails2_3_interface.rb', line 82

def draw
  reset!
  yield ActionController::Routing::RouteSet::Mapper.new(self)
  install_helpers
end

#generate(options, recall = {}, method = :generate, route_name = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/usher/interface/rails2_3_interface.rb', line 102

def generate(options, recall = {}, method = :generate, route_name = nil)
  route = if(route_name)
    @router.named_routes[route_name]
  else
    merged_options = options
    merged_options[:controller] = recall[:controller] unless options.key?(:controller)
    unless options.key?(:action)
      options[:action] = ''
    end
    path_for_options(merged_options)
  end
  case method
  when :generate
    merged_options ||= recall.merge(options)
    url = generate_url(route, merged_options)
    url.slice!(-1) if url[-1] == ?/
    url 
  else
    raise "method #{method} not recognized"
  end
end

#generate_url(route, params) ⇒ Object



124
125
126
# File 'lib/usher/interface/rails2_3_interface.rb', line 124

def generate_url(route, params)
  @router.generator.generate(route, params)
end

#install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/usher/interface/rails2_3_interface.rb', line 88

def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
  #*_url and hash_for_*_url
  Array(destinations).each do |d| d.module_eval { include Helpers } 
    @router.named_routes.keys.each do |name|
      @module.module_eval <<-end_eval # We use module_eval to avoid leaks
        def #{name}_url(options = {})
          ActionController::Routing::UsherRoutes.generate(options, {}, :generate, :#{name})
        end
      end_eval
    end
    d.__send__(:include, @module)
  end
end

#named_routesObject



7
8
9
# File 'lib/usher/interface/rails2_3_interface.rb', line 7

def named_routes
  @router.named_routes
end

#path_for_options(options) ⇒ Object



128
129
130
# File 'lib/usher/interface/rails2_3_interface.rb', line 128

def path_for_options(options)
  @router.path_for_options(options)
end

#recognize(request) ⇒ Object



67
68
69
70
71
72
# File 'lib/usher/interface/rails2_3_interface.rb', line 67

def recognize(request)
  response = @router.recognize(request)
  request.path_parameters = (response.params.empty? ? response.path.route.destination : response.path.route.destination.merge(response.params.inject({}){|h,(k,v)| h[k]=v; h })).with_indifferent_access
  response.params.each { |pair| request.path_parameters[pair.first] = pair.last }
  "#{request.path_parameters[:controller].camelize}Controller".constantize
end

#reload!Object Also known as: reload



43
44
45
46
47
48
49
50
# File 'lib/usher/interface/rails2_3_interface.rb', line 43

def reload!
  if configuration_files.any?
    configuration_files.each { |config| load(config) }
  else
    add_route ":controller/:action/:id"
  end
  
end

#reset!Object



74
75
76
77
78
79
80
# File 'lib/usher/interface/rails2_3_interface.rb', line 74

def reset!
  @router = Usher.new(:generator => Usher::Util::Generators::URL.new)
  @configuration_files = []
  @module ||= Module.new
  @controller_route_added = false
  @controller_action_route_added = false
end

#route_countObject



53
54
55
# File 'lib/usher/interface/rails2_3_interface.rb', line 53

def route_count
  routes.size
end

#routesObject



57
58
59
# File 'lib/usher/interface/rails2_3_interface.rb', line 57

def routes
  @router.routes
end