Sha256: 7454f9c93761db0737162e29f8dce419c3b10dd50141e3f0c77b4076a4bb704c

Contents?: true

Size: 1.52 KB

Versions: 52

Compression:

Stored size: 1.52 KB

Contents

# 
# Global path prefix (hack with relative_url_root)
# 
GLOBAL_PATH_PREFIX_DELIMITER = '-'
ActionController::Base.send :class_eval do 
  def self.global_path_prefix
    @global_path_prefix ||= []
  end
  
  def self.global_path_prefix= list
    @global_path_prefix = list.collect{|v| v.to_s}
  end
  
  cattr_accessor :_relative_url_root  
end
ActionController::Base._relative_url_root = ActionController::Base.relative_url_root || ""

# 
# Generation
# 

# routes
ActionController::Base.send :class_eval do
  around_filter :hack_relative_url_root
  protected 
  
  # preparing relative_url_root from params
  def hack_relative_url_root
    begin
      update_relative_url_root!
      yield
    ensure
      ActionController::Base.relative_url_root = ActionController::Base._relative_url_root
    end
  end
  
  def update_relative_url_root!
    prefix_params = []
    ActionController::Base.global_path_prefix.each do |name|
      unless (param = params[name]).blank?
        prefix_params << [name, CGI.escape(param)]
      end
    end
    prefix = prefix_params.collect{|name, param|  "#{param}#{GLOBAL_PATH_PREFIX_DELIMITER}#{name}"}.join('/')
    ActionController::Base.relative_url_root = ActionController::Base._relative_url_root + (prefix.blank? ? "" : "/" + prefix)
  end
end

# 
# Usability
# allows to install a global_path_prefix to the route set by calling: map.global_path_prefix 'locale'
ActionController::Routing::RouteSet::Mapper.class_eval do
  def global_path_prefix *args
    ActionController::Base.global_path_prefix = args
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
crystal-ext-0.0.3 lib/rails_ext/action_controller/global_path_prefix.rb
crystal-ext-0.0.2 lib/rails_ext/action_controller/global_path_prefix.rb
crystal-ext-0.0.1 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.29 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.28 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.27 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.25 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.24 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.23 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.22 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.21 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.20 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.19 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.18 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.17 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.16 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.15 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.14 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.13 lib/rails_ext/action_controller/global_path_prefix.rb
rails-ext-0.3.12 lib/rails_ext/action_controller/global_path_prefix.rb