Skip to content

Instantly share code, notes, and snippets.

@gzigzigzeo
Created May 24, 2012 10:07
Show Gist options
  • Save gzigzigzeo/2780614 to your computer and use it in GitHub Desktop.
Save gzigzigzeo/2780614 to your computer and use it in GitHub Desktop.
# Ебический, ебический модуль эмуляции динамических роутов из базы.
# Альтернатива reload_routes!
# Это блять пиздец ебануться какой ебический модуль.
# Но зато - теперь будем клонировать.
# Нужно его гемофицировать.
module Megatron
class Rewrite
REWRITE_CACHE_KEY = 'megatron.rewrite_cache'
HELPERS_CACHE_KEY = 'megatron.helpers_cache'
def initialize(app)
@app = app
end
def call(env)
request = ActionDispatch::Request.new(env)
path = request.path_info.dup
cache = Rails.cache.read(REWRITE_CACHE_KEY) || {}
cache_key = "#{request.host_with_port}#{path}"
cached = cache[cache_key]
# Если ранее этот адрес уже переписывали
if cached.present?
translation_id, page_id, rewrite_to = cached
# И попали в страницу (не статический файл) - переписываем заново как раньше
if page_id.present?
translation = Translation.find(translation_id)
page = Page.find(page_id)
rewrite(env, translation, page, path)
end
else
translation = Translation.for_request(request)
page = Page.reverse_sorted.translated(translation).published.all.find { |page| path.starts_with?(page.absolute_path) }
# Если страница нашлась - сохраняем параметры в кеше
cache[cache_key] = if page.present?
rewrite_to = rewrite(env, translation, page, path)
[translation.id, page.id, rewrite_to]
else
true # Статика
end
Rails.cache.write(REWRITE_CACHE_KEY, cache)
end
@app.call(env)
end
private
def rewrite(env, translation, page, path)
env['megatron.translation'] = translation if translation.present?
env['megatron.page'] = page
env['megatron.original_path'] = path
rewrite_to = if page.rewrite_to.present?
"#{page.rewrite_to}/#{path[page.absolute_path.length..-1]}"#rewrite_to[0, page.absolute_path.length] = page.rewrite_to
else
"/page/#{path}"
end
env['PATH_INFO'] = rewrite_to
end
# Все тут чтобы было в одном месте
module Helpers
class << self
def define_url_helper(tag, method)
define_method :"#{method}_path" do |*args|
cache = Rails.cache.read(HELPERS_CACHE_KEY) || {}
cache_key = :"#{@current_translation.id}_#{tag}"
cached = cache[cache_key]
absolute_path, rewrite_to = if cached.present?
cached
else
page = Page.translated(@current_translation).find_by_tag(tag)
return if page.nil?
[page.absolute_path, page.rewrite_to].tap do |cached|
cache[cache_key] = cached
Rails.cache.write(HELPERS_CACHE_KEY, cache)
end
end
send(:"#{method}_base_path", *args).tap do |path|
path[0, rewrite_to.length] = absolute_path
end
end
define_method :"#{method}_url" do |*args|
url_for(send(:"#{method}_path", *args))
end
end
end
{
'tournaments' => %w(tournaments tournaments_past tournaments_feature tournament tournament_table tournament_pay tournament_video),
'fighters' => %w(fighter fighters donations donate create_donation),
'fighter_request' => %w(fighter_request)
}.each do |tag, pages|
pages.each { |page| define_url_helper(tag, page) }
end
end
end
end
Rails.application.config.middleware.use Megatron::Rewrite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment