Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 8c686ead344f1b9ead3f72bbf0cbc5dad2afc2fb @unixcharles committed Jun 14, 2011
@@ -0,0 +1,2 @@
+.DS_Store
+
@@ -0,0 +1,43 @@
+module Admin
+ class RoutesController < Admin::BaseController
+ before_filter :find_all_locales
+ before_filter :find_locale
+ before_filter :find_routes_to_redirect, :except => [:index, :redirections]
+ before_filter :find_all_pages, :except => [:index, :redirections]
+
+ crudify :route, :sortable => false,
+ :title_attribute => 'url', :xhr_paging => true, :include => [:target]
+
+
+ def redirections
+ @routes = Route.where(:redirect => true)
+ render :index
+ end
+
+ def find_all_locales
+ @locales ||= ::Refinery::I18n.frontend_locales
+ end
+
+ def find_all_pages
+ @pages = Page.all
+ end
+
+ def find_all_routes
+ @routes = Route.where(:locale => @current_locale).order(:url)
+ end
+
+ def find_routes_to_redirect
+ @routes = Route.where(:redirect => [false, nil])
+ end
+
+ def default_locale
+ ::Refinery::I18n.default_frontend_locale
+ end
+
+ protected
+
+ def find_locale
+ @current_locale ||= (params[:route_locale] || default_locale).to_sym
+ end
+ end
+end
@@ -0,0 +1,11 @@
+module RouterHelper
+ def custom_url_for(page, locale = nil)
+ locale ||= Globalize.locale
+
+ if route = Route.where(:target_id => page.id, :target_type => 'Page', :locale => locale).first
+ "/#{route.url}"
+ else
+ page.url
+ end
+ end
+end
@@ -0,0 +1,10 @@
+class Route < ActiveRecord::Base
+
+ acts_as_indexed :fields => [:url, :locale]
+
+ validates :url, :presence => true, :uniqueness => true,
+ :format => { :with => /(^[\da-zA-Z_-]+)/ }
+
+ belongs_to :target, :polymorphic => true
+
+end
@@ -0,0 +1,25 @@
+<ul>
+ <% if Admin::RoutesController.searchable? %>
+ <li>
+ <%= render :partial => "/shared/admin/search",
+ :locals => {
+ :url => admin_routes_url
+ } %>
+ </li>
+ <% end unless action_name == 'redirections' %>
+ <li>
+ <% type = action_name == 'redirections' ? 'redirection' : 'route' %>
+ <%= link_to t(".create_new_#{type}"), new_admin_route_url(:route_locale => @current_locale, :dialog => true, :type => type),
+ :class => "add_icon" %>
+ </li>
+ <%= render 'locale_filters' unless action_name == 'redirections' %>
+ <li>
+ <% if action_name == 'redirections' %>
+ <%= link_to t(".manage_routes"), admin_routes_url,
+ :class => "edit_icon" %>
+ <% else %>
+ <%= link_to t(".manage_redirections"), redirections_admin_routes_url,
+ :class => "edit_icon" %>
+ <% end %>
+ </li>
+</ul>
@@ -0,0 +1,56 @@
+<%= form_for [:admin, @route] do |f| -%>
+ <%= render :partial => "/shared/admin/error_messages", :locals => {
+ :object => @route,
+ :include_object_name => true
+ } %>
+
+ <%= hidden_field_tag 'type', params[:type] -%>
+
+ <div class='url-field'>
+ <%= f.label :url, "#{request.protocol}#{request.domain}/" -%>
+ <%= f.text_field :url -%>
+ </div>
+
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? and params[:type] == 'route' %>
+ <div class='field'>
+ <%= f.label :locale -%>
+ <%= f.select :locale, ::Refinery::I18n.locales.reject {|locale, title| !@locales.include?( locale ) }.invert, :selected => @current_locale -%>
+ </div>
+ <% end %>
+
+ <%= hidden_field_tag 'route[redirect]', params[:type] == 'redirection' -%>
+
+ <div class='field'>
+ <%= f.label :target_id -%>
+ <% if params[:type] == 'route' %>
+ <%= f.select :target_id, @pages.map {|p| [p.title, p.id] } -%>
+ <% target_type = 'Page' %>
+ <% else %>
+ <%= f.select :target_id, grouped_options_for_select(@routes.group_by(&:locale).map {|locale, routes| [locale, routes.map { |r| [r.url, r.id] }] }) -%>
+ <% target_type = 'Route' %>
+ <% end %>
+ <%= hidden_field_tag 'route[target_type]', target_type -%>
+ </div>
+
+ <%= render :partial => "/shared/admin/form_actions",
+ :locals => {
+ :f => f,
+ :continue_editing => false,
+ :delete_title => t('delete', :scope => 'admin.routes.route'),
+ :delete_confirmation => t('message', :scope => 'shared.admin.delete', :title => @route.url)
+ } %>
+<% end -%>
+
+<style type='text/css'>
+.url-field { border: 1px solid #7F9DB9; }
+.url-field label, .url-field input { padding: 5px; margin: 5px; }
+.url-field label {
+ font-size: 1.4em;
+ clear:both; float:left; display:inline;
+ margin-right: 1px; padding-right: 1px; }
+.url-field input {
+ border: 0;
+ margin-left: 1px; display:block;
+ overflow: hidden; zoom:1; position:relative;
+ font-weight: bold; font-size: 1.1em; }
+</style>
@@ -0,0 +1,26 @@
+<% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? %>
+<% locales = ::Refinery::I18n.locales.reject {|locale, title| !@locales.include?( locale ) } %>
+<ul class='collapsible_menu closed'>
+
+ <% if @current_locale %>
+ <% current_locale = @current_locale %>
+ <% current_locale_title = locales[current_locale] %>
+ <li>
+ <%= link_to "#", :style => "background-image: url('/images/refinery/icons/flags/#{current_locale}.png');" do %>
+ <%= current_locale_title.respond_to?(:force_encoding) ?
+ current_locale_title.force_encoding('utf-8') : current_locale_title %>
+ <span><%= t('.change_language') %></span>
+ <span style='display:none;'><%= t('cancel', :scope => 'shared.admin.form_actions') %></span>
+ <% end %>
+ </li>
+ <% end %>
+
+ <% locales.sort_by{|key, value| value}.each do |locale, locale_title| %>
+ <li>
+ <%= link_to locale_title, params.dup.tap { |p| p[:route_locale] = locale },
+ :style => "background-image: url('/images/refinery/icons/flags/#{locale}.png');" %>
+ </li>
+ <% end %>
+</ul>
+
+<% end %>
@@ -0,0 +1,22 @@
+<% if searching? %>
+ <h2><%= t('results_for', :scope => 'shared.admin.search', :query => params[:search]) %></h2>
+<% end %>
+<% if @routes.any? %>
+ <div class='pagination_container'>
+ <%= render :partial => 'routes' %>
+ </div>
+<% else %>
+ <p>
+ <% unless searching? %>
+ <strong>
+ <% if action_name == 'redirections' %>
+ <%= t('.no_redirections_yet') %>
+ <% else %>
+ <%= t('.no_routes_yet') %>
+ <% end %>
+ </strong>
+ <% else %>
+ <%= t('no_results', :scope => 'shared.admin.search') %>
+ <% end %>
+ </p>
+<% end %>
@@ -0,0 +1,28 @@
+<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(redirection) -%>">
+ <span class='title'>
+ <% url = "#{request.protocol}#{request.host}/#{redirection.url}" -%>
+ <%= url %>
+
+ <% if redirection.target.is_a? Route -%>
+ <% target_url = "#{request.protocol}#{request.host}/#{redirection.target.url}" -%>
+ <span class="preview">
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? %>
+ <%= refinery_icon_tag("flags/#{redirection.target.locale}.png", :size => '16x11') %>
+ <% end %>
+ <%= target_url %>
+ </span>
+ <% end %>
+ </span>
+ <span class='actions'>
+ <%= link_to refinery_icon_tag("application_go.png"), url,
+ :title => t('.view_live_html'),
+ :target => "_blank" %>
+ <%= link_to refinery_icon_tag("application_edit.png"), edit_admin_route_path(redirection, :dialog => true, :type => 'redirection'),
+ :title => t('.edit') %>
+ <%= link_to refinery_icon_tag("delete.png"), admin_route_path(redirection),
+ :class => "cancel confirm-delete",
+ :title => t('.delete'),
+ :confirm => t('message', :scope => 'shared.admin.delete', :title => redirection.url),
+ :method => :delete %>
+ </span>
+</li>
@@ -0,0 +1,27 @@
+<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(route) -%>">
+ <span class='title'>
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? %>
+ <%= refinery_icon_tag("flags/#{route.locale}.png", :size => '16x11') %>
+ <% end %>
+ <% url = "#{request.protocol}#{request.host}/#{route.url}" -%>
+ <%= url %>
+
+ <% if route.target.is_a? Page -%>
+ <span class="preview">
+ <%= link_to route.target.title, edit_admin_page_path(route.target, :switch_locale => route.locale) %>
+ </span>
+ <% end %>
+ </span>
+ <span class='actions'>
+ <%= link_to refinery_icon_tag("application_go.png"), url,
+ :title => t('.view_live_html'),
+ :target => "_blank" %>
+ <%= link_to refinery_icon_tag("application_edit.png"), edit_admin_route_path(route, :dialog => true, :type => 'route'),
+ :title => t('.edit') %>
+ <%= link_to refinery_icon_tag("delete.png"), admin_route_path(route),
+ :class => "cancel confirm-delete",
+ :title => t('.delete'),
+ :confirm => t('message', :scope => 'shared.admin.delete', :title => route.url),
+ :method => :delete %>
+ </span>
+</li>
@@ -0,0 +1,7 @@
+<ul>
+ <% if action_name == 'redirections' %>
+ <%= render :partial => 'redirection', :collection => @routes %>
+ <% else %>
+ <%= render :partial => 'route', :collection => @routes %>
+ <% end %>
+</ul>
@@ -0,0 +1 @@
+<%= render :partial => "form" %>
@@ -0,0 +1,6 @@
+<section id='records'>
+ <%= render :partial => 'records' %>
+</section>
+<aside id='actions'>
+ <%= render :partial => 'actions' %>
+</aside>
@@ -0,0 +1 @@
+<%= render :partial => "form" %>
@@ -0,0 +1,24 @@
+en:
+ plugins:
+ refinerycms_routes:
+ title: Routes
+ admin:
+ routes:
+ actions:
+ create_new_route: Add New Route
+ create_new_redirection: Add New Redirection
+ manage_routes: Manage Routes
+ manage_redirections: Manage Redirections
+ locale_filters:
+ change_language: Change language
+ records:
+ title: Routes
+ sorry_no_results: Sorry! There are no results found.
+ no_items_yet: There are no Routes yet. Click "Add New Route" to add your first route.
+ route:
+ view_live_html: View this route live <br/><em>(opens in a new window)</em>
+ edit: Edit this route
+ delete: Remove this route forever
+ routes:
+ show:
+ other: Other Routes
@@ -0,0 +1,11 @@
+Refinery::Application.routes.draw do
+ resources :routes, :only => [:index, :show]
+
+ scope(:path => 'refinery', :as => 'admin', :module => 'admin') do
+ resources :routes, :except => :show do
+ collection do
+ get :redirections
+ end
+ end
+ end
+end
@@ -0,0 +1,26 @@
+class CreateRoutes < ActiveRecord::Migration
+
+ def self.up
+ create_table :routes do |t|
+ t.string :url
+ t.string :locale
+ t.boolean :redirect, :default => false
+ t.integer :target_id
+ t.string :target_type
+
+ t.timestamps
+ end
+
+ add_index :routes, :url
+ add_index :routes, [:target_id, :target_type]
+
+ load(Rails.root.join('db', 'seeds', 'routes.rb'))
+ end
+
+ def self.down
+ UserPlugin.destroy_all({:name => "refinerycms_routes"})
+
+ drop_table :routes
+ end
+
+end
@@ -0,0 +1,28 @@
+User.all.each do |user|
+ if user.plugins.where(:name => 'refinerycms_routes').blank?
+ user.plugins.create(:name => 'refinerycms_routes',
+ :position => (user.plugins.maximum(:position) || -1) +1)
+ end
+end
+
+def url_for(options = {})
+ url = case options
+ when String
+ options.gsub('/', '')
+ when Hash
+ options = options[:path].join('/')
+ end
+ url
+end
+
+Page.all.each do |page|
+ page.translations.map(&:locale).each do |locale|
+ page_url = url_for(page.url)
+
+ unless locale == ::Refinery::I18n.default_frontend_locale
+ page_url = "#{locale}#{ '/' unless page_url.blank? }#{page_url}"
+ end
+
+ Route.create(:url => page_url, :locale => locale, :target => page)
+ end
+end
Oops, something went wrong.

0 comments on commit 8c686ea

Please sign in to comment.