Class: PathRewriter::Rails

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.codecObject

Returns the value of attribute codec.



4
5
6
# File 'lib/path_rewriter/rails.rb', line 4

def codec
  @codec
end

Class Method Details

.enable {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
# File 'lib/path_rewriter/rails.rb', line 6

def enable
  self.install
  self.codec = PathRewriter::UrlCodec.new
  yield self
end

.installObject



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
# File 'lib/path_rewriter/rails.rb', line 12

def install
  ActionController::Base::optimise_named_routes = false
  
  ActionController::Routing::Routes.class_eval do
    def call_with_rewrite(orig)
      env = orig.dup
      begin
        puts PathRewriter::Rails.codec
        env["REQUEST_URI"] = PathRewriter::Rails.codec.decode(env["REQUEST_URI"])
        call_without_rewrite(env)
      rescue ActionController::NotImplemented, ActionController::MethodNotAllowed, ActionController::RoutingError
        call_without_rewrite(orig)
      end
    end
    alias_method_chain :call, :rewrite
  end
  
  [ ActionController::Base, ActionView::Base ].each do |klass|
    klass.class_eval do
      def url_for_with_rewrite(*args)
        url = url_for_without_rewrite(*args)
        PathRewriter::Rails.codec.encode(url)
      end
      alias_method_chain :url_for, :rewrite
    end
  end
end