Class: Usher::Interface::Rails23

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/interface/rails23.rb,
lib/usher/interface/rails23/mapper.rb

Defined Under Namespace

Classes: Mapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRails23

Returns a new instance of Rails23.



35
36
37
# File 'lib/usher/interface/rails23.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/rails23.rb', line 5

def configuration_files
  @configuration_files
end

Instance Method Details

#add_configuration_file(file) ⇒ Object



39
40
41
# File 'lib/usher/interface/rails23.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/rails23.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/rails23.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/rails23.rb', line 61

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

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

Yields:

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


85
86
87
88
89
# File 'lib/usher/interface/rails23.rb', line 85

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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/usher/interface/rails23.rb', line 105

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



127
128
129
# File 'lib/usher/interface/rails23.rb', line 127

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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/usher/interface/rails23.rb', line 91

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/rails23.rb', line 7

def named_routes
  @router.named_routes
end

#path_for_options(options) ⇒ Object



131
132
133
# File 'lib/usher/interface/rails23.rb', line 131

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

#recognize(request) ⇒ Object



67
68
69
70
71
72
# File 'lib/usher/interface/rails23.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/rails23.rb', line 43

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

#reset!(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/usher/interface/rails23.rb', line 74

def reset!(options={})
  options[:generator] = options[:generator] || Usher::Util::Generators::URL.new
  options[:request_methods] = options[:request_methods] || [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains]
          
  @router = Usher.new(options)
  @configuration_files = []
  @module ||= Module.new
  @controller_route_added = false
  @controller_action_route_added = false
end

#route_countObject



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

def route_count
  routes.size
end

#routesObject



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

def routes
  @router.routes
end