refinerycms-routes 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ module Admin
2
+ class RoutesController < Admin::BaseController
3
+ before_filter :find_all_locales
4
+ before_filter :find_locale
5
+ before_filter :find_routes_to_redirect, :except => [:index, :redirections]
6
+ before_filter :find_all_pages, :except => [:index, :redirections]
7
+
8
+ crudify :route, :sortable => false,
9
+ :title_attribute => 'url', :xhr_paging => true, :include => [:target]
10
+
11
+
12
+ def redirections
13
+ @routes = Route.where(:redirect => true)
14
+ render :index
15
+ end
16
+
17
+ def find_all_locales
18
+ @locales ||= ::Refinery::I18n.frontend_locales
19
+ end
20
+
21
+ def find_all_pages
22
+ @pages = Page.all
23
+ end
24
+
25
+ def find_all_routes
26
+ @routes = Route.where(:locale => @current_locale).order(:url)
27
+ end
28
+
29
+ def find_routes_to_redirect
30
+ @routes = Route.where(:redirect => [false, nil])
31
+ end
32
+
33
+ def default_locale
34
+ ::Refinery::I18n.default_frontend_locale
35
+ end
36
+
37
+ protected
38
+
39
+ def find_locale
40
+ @current_locale ||= (params[:route_locale] || default_locale).to_sym
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ module RouterHelper
2
+ def custom_url_for(page, locale = nil)
3
+ locale ||= Globalize.locale
4
+
5
+ if route = Route.where(:target_id => page.id, :target_type => 'Page', :locale => locale).first
6
+ "/#{route.url}"
7
+ else
8
+ page.url
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class Route < ActiveRecord::Base
2
+
3
+ acts_as_indexed :fields => [:url, :locale]
4
+
5
+ validates :url, :presence => true, :uniqueness => true,
6
+ :format => { :with => /(^[\da-zA-Z_-]+)/ }
7
+
8
+ belongs_to :target, :polymorphic => true
9
+
10
+ end
@@ -0,0 +1,25 @@
1
+ <ul>
2
+ <% if Admin::RoutesController.searchable? %>
3
+ <li>
4
+ <%= render :partial => "/shared/admin/search",
5
+ :locals => {
6
+ :url => admin_routes_url
7
+ } %>
8
+ </li>
9
+ <% end unless action_name == 'redirections' %>
10
+ <li>
11
+ <% type = action_name == 'redirections' ? 'redirection' : 'route' %>
12
+ <%= link_to t(".create_new_#{type}"), new_admin_route_url(:route_locale => @current_locale, :dialog => true, :type => type),
13
+ :class => "add_icon" %>
14
+ </li>
15
+ <%= render 'locale_filters' unless action_name == 'redirections' %>
16
+ <li>
17
+ <% if action_name == 'redirections' %>
18
+ <%= link_to t(".manage_routes"), admin_routes_url,
19
+ :class => "edit_icon" %>
20
+ <% else %>
21
+ <%= link_to t(".manage_redirections"), redirections_admin_routes_url,
22
+ :class => "edit_icon" %>
23
+ <% end %>
24
+ </li>
25
+ </ul>
@@ -0,0 +1,56 @@
1
+ <%= form_for [:admin, @route] do |f| -%>
2
+ <%= render :partial => "/shared/admin/error_messages", :locals => {
3
+ :object => @route,
4
+ :include_object_name => true
5
+ } %>
6
+
7
+ <%= hidden_field_tag 'type', params[:type] -%>
8
+
9
+ <div class='url-field'>
10
+ <%= f.label :url, "#{request.protocol}#{request.domain}/" -%>
11
+ <%= f.text_field :url -%>
12
+ </div>
13
+
14
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? and params[:type] == 'route' %>
15
+ <div class='field'>
16
+ <%= f.label :locale -%>
17
+ <%= f.select :locale, ::Refinery::I18n.locales.reject {|locale, title| !@locales.include?( locale ) }.invert, :selected => @current_locale -%>
18
+ </div>
19
+ <% end %>
20
+
21
+ <%= hidden_field_tag 'route[redirect]', params[:type] == 'redirection' -%>
22
+
23
+ <div class='field'>
24
+ <%= f.label :target_id -%>
25
+ <% if params[:type] == 'route' %>
26
+ <%= f.select :target_id, @pages.map {|p| [p.title, p.id] } -%>
27
+ <% target_type = 'Page' %>
28
+ <% else %>
29
+ <%= f.select :target_id, grouped_options_for_select(@routes.group_by(&:locale).map {|locale, routes| [locale, routes.map { |r| [r.url, r.id] }] }) -%>
30
+ <% target_type = 'Route' %>
31
+ <% end %>
32
+ <%= hidden_field_tag 'route[target_type]', target_type -%>
33
+ </div>
34
+
35
+ <%= render :partial => "/shared/admin/form_actions",
36
+ :locals => {
37
+ :f => f,
38
+ :continue_editing => false,
39
+ :delete_title => t('delete', :scope => 'admin.routes.route'),
40
+ :delete_confirmation => t('message', :scope => 'shared.admin.delete', :title => @route.url)
41
+ } %>
42
+ <% end -%>
43
+
44
+ <style type='text/css'>
45
+ .url-field { border: 1px solid #7F9DB9; }
46
+ .url-field label, .url-field input { padding: 5px; margin: 5px; }
47
+ .url-field label {
48
+ font-size: 1.4em;
49
+ clear:both; float:left; display:inline;
50
+ margin-right: 1px; padding-right: 1px; }
51
+ .url-field input {
52
+ border: 0;
53
+ margin-left: 1px; display:block;
54
+ overflow: hidden; zoom:1; position:relative;
55
+ font-weight: bold; font-size: 1.1em; }
56
+ </style>
@@ -0,0 +1,26 @@
1
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? %>
2
+ <% locales = ::Refinery::I18n.locales.reject {|locale, title| !@locales.include?( locale ) } %>
3
+ <ul class='collapsible_menu closed'>
4
+
5
+ <% if @current_locale %>
6
+ <% current_locale = @current_locale %>
7
+ <% current_locale_title = locales[current_locale] %>
8
+ <li>
9
+ <%= link_to "#", :style => "background-image: url('/images/refinery/icons/flags/#{current_locale}.png');" do %>
10
+ <%= current_locale_title.respond_to?(:force_encoding) ?
11
+ current_locale_title.force_encoding('utf-8') : current_locale_title %>
12
+ <span><%= t('.change_language') %></span>
13
+ <span style='display:none;'><%= t('cancel', :scope => 'shared.admin.form_actions') %></span>
14
+ <% end %>
15
+ </li>
16
+ <% end %>
17
+
18
+ <% locales.sort_by{|key, value| value}.each do |locale, locale_title| %>
19
+ <li>
20
+ <%= link_to locale_title, params.dup.tap { |p| p[:route_locale] = locale },
21
+ :style => "background-image: url('/images/refinery/icons/flags/#{locale}.png');" %>
22
+ </li>
23
+ <% end %>
24
+ </ul>
25
+
26
+ <% end %>
@@ -0,0 +1,22 @@
1
+ <% if searching? %>
2
+ <h2><%= t('results_for', :scope => 'shared.admin.search', :query => params[:search]) %></h2>
3
+ <% end %>
4
+ <% if @routes.any? %>
5
+ <div class='pagination_container'>
6
+ <%= render :partial => 'routes' %>
7
+ </div>
8
+ <% else %>
9
+ <p>
10
+ <% unless searching? %>
11
+ <strong>
12
+ <% if action_name == 'redirections' %>
13
+ <%= t('.no_redirections_yet') %>
14
+ <% else %>
15
+ <%= t('.no_routes_yet') %>
16
+ <% end %>
17
+ </strong>
18
+ <% else %>
19
+ <%= t('no_results', :scope => 'shared.admin.search') %>
20
+ <% end %>
21
+ </p>
22
+ <% end %>
@@ -0,0 +1,28 @@
1
+ <li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(redirection) -%>">
2
+ <span class='title'>
3
+ <% url = "#{request.protocol}#{request.host}/#{redirection.url}" -%>
4
+ <%= url %> →
5
+
6
+ <% if redirection.target.is_a? Route -%>
7
+ <% target_url = "#{request.protocol}#{request.host}/#{redirection.target.url}" -%>
8
+ <span class="preview">
9
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? %>
10
+ <%= refinery_icon_tag("flags/#{redirection.target.locale}.png", :size => '16x11') %>
11
+ <% end %>
12
+ <%= target_url %>
13
+ </span>
14
+ <% end %>
15
+ </span>
16
+ <span class='actions'>
17
+ <%= link_to refinery_icon_tag("application_go.png"), url,
18
+ :title => t('.view_live_html'),
19
+ :target => "_blank" %>
20
+ <%= link_to refinery_icon_tag("application_edit.png"), edit_admin_route_path(redirection, :dialog => true, :type => 'redirection'),
21
+ :title => t('.edit') %>
22
+ <%= link_to refinery_icon_tag("delete.png"), admin_route_path(redirection),
23
+ :class => "cancel confirm-delete",
24
+ :title => t('.delete'),
25
+ :confirm => t('message', :scope => 'shared.admin.delete', :title => redirection.url),
26
+ :method => :delete %>
27
+ </span>
28
+ </li>
@@ -0,0 +1,27 @@
1
+ <li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(route) -%>">
2
+ <span class='title'>
3
+ <% if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? %>
4
+ <%= refinery_icon_tag("flags/#{route.locale}.png", :size => '16x11') %>
5
+ <% end %>
6
+ <% url = "#{request.protocol}#{request.host}/#{route.url}" -%>
7
+ <%= url %> →
8
+
9
+ <% if route.target.is_a? Page -%>
10
+ <span class="preview">
11
+ <%= link_to route.target.title, edit_admin_page_path(route.target, :switch_locale => route.locale) %>
12
+ </span>
13
+ <% end %>
14
+ </span>
15
+ <span class='actions'>
16
+ <%= link_to refinery_icon_tag("application_go.png"), url,
17
+ :title => t('.view_live_html'),
18
+ :target => "_blank" %>
19
+ <%= link_to refinery_icon_tag("application_edit.png"), edit_admin_route_path(route, :dialog => true, :type => 'route'),
20
+ :title => t('.edit') %>
21
+ <%= link_to refinery_icon_tag("delete.png"), admin_route_path(route),
22
+ :class => "cancel confirm-delete",
23
+ :title => t('.delete'),
24
+ :confirm => t('message', :scope => 'shared.admin.delete', :title => route.url),
25
+ :method => :delete %>
26
+ </span>
27
+ </li>
@@ -0,0 +1,7 @@
1
+ <ul>
2
+ <% if action_name == 'redirections' %>
3
+ <%= render :partial => 'redirection', :collection => @routes %>
4
+ <% else %>
5
+ <%= render :partial => 'route', :collection => @routes %>
6
+ <% end %>
7
+ </ul>
@@ -0,0 +1 @@
1
+ <%= render :partial => "form" %>
@@ -0,0 +1,6 @@
1
+ <section id='records'>
2
+ <%= render :partial => 'records' %>
3
+ </section>
4
+ <aside id='actions'>
5
+ <%= render :partial => 'actions' %>
6
+ </aside>
@@ -0,0 +1 @@
1
+ <%= render :partial => "form" %>
@@ -0,0 +1,24 @@
1
+ en:
2
+ plugins:
3
+ refinerycms_routes:
4
+ title: Routes
5
+ admin:
6
+ routes:
7
+ actions:
8
+ create_new_route: Add New Route
9
+ create_new_redirection: Add New Redirection
10
+ manage_routes: Manage Routes
11
+ manage_redirections: Manage Redirections
12
+ locale_filters:
13
+ change_language: Change language
14
+ records:
15
+ title: Routes
16
+ sorry_no_results: Sorry! There are no results found.
17
+ no_items_yet: There are no Routes yet. Click "Add New Route" to add your first route.
18
+ route:
19
+ view_live_html: View this route live <br/><em>(opens in a new window)</em>
20
+ edit: Edit this route
21
+ delete: Remove this route forever
22
+ routes:
23
+ show:
24
+ other: Other Routes
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ Refinery::Application.routes.draw do
2
+ resources :routes, :only => [:index, :show]
3
+
4
+ scope(:path => 'refinery', :as => 'admin', :module => 'admin') do
5
+ resources :routes, :except => :show do
6
+ collection do
7
+ get :redirections
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class RefinerycmsRoutesGenerator < Refinery::Generators::EngineInstaller
2
+
3
+ source_root File.expand_path('../../../', __FILE__)
4
+ engine_name "refinerycms_routes"
5
+
6
+ class_option :page_controller, :type => :boolean, :default => true, :description => "Include a default custom page controller"
7
+
8
+ def generate
9
+ super
10
+ copy_file "#{self.class.source_root}/lib/generators/templates/pages_controller.rb",
11
+ "app/controllers/pages_controller.rb" if options.page_controller?
12
+ end
13
+
14
+ end
@@ -0,0 +1,42 @@
1
+ class PagesController < ApplicationController
2
+
3
+ # This action is usually accessed with the root path, normally '/'
4
+ def home
5
+ error_404 unless (@page = Page.where(:link_url => '/').first).present?
6
+ end
7
+
8
+ # This is where the custom routing happen.
9
+ def show
10
+ # Try to find a routes that match with the request path
11
+ if route = Route.where(:url => "#{params[:path]}").first
12
+ route_target = route.target
13
+ case route_target
14
+ when Page then
15
+ # If the target of the route is a page, render that page.
16
+ @page = route.target
17
+ Globalize.locale = route.locale
18
+ when Route
19
+ # If the target of the route is another route, redirect to that page.
20
+ url = URI.parse(request.url)
21
+ url.path = '/' + route.target.url
22
+ redirect_to url.to_s, :status => :moved_permanently and return
23
+ end
24
+ end
25
+
26
+ # Fallback to refinerycms default if no routes is found.
27
+ @page ||= Page.find("#{params[:path]}/#{params[:id]}".split('/').last)
28
+
29
+ # proceed as usual...
30
+ if @page.try(:live?) || (refinery_user? && current_user.authorized_plugins.include?("refinery_pages"))
31
+ # if the admin wants this to be a "placeholder" page which goes to its first child, go to that instead.
32
+ if @page.skip_to_first_child && (first_live_child = @page.children.order('lft ASC').live.first).present?
33
+ redirect_to first_live_child.url
34
+ elsif @page.link_url.present?
35
+ redirect_to @page.link_url and return
36
+ end
37
+ else
38
+ error_404
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,33 @@
1
+ require 'refinerycms-base'
2
+ require 'refinerycms-pages'
3
+
4
+ module Refinery
5
+ module Routes
6
+ class Engine < Rails::Engine
7
+ initializer "static assets" do |app|
8
+ app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public"
9
+ end
10
+
11
+ config.to_prepare do
12
+ ::ApplicationController.helper(RouterHelper)
13
+
14
+ Page.module_eval do
15
+ has_many :routes, :dependent => :destroy
16
+ end
17
+ end
18
+
19
+ config.after_initialize do
20
+ Refinery::Plugin.register do |plugin|
21
+ plugin.name = 'refinerycms_routes'
22
+ plugin.url = {:controller => '/admin/routes', :action => 'index'}
23
+ plugin.menu_match = /routes/
24
+ plugin.activity = {
25
+ :class => Route,
26
+ :title => 'Url'
27
+ }
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ namespace :refinery do
2
+
3
+ namespace :routes do
4
+
5
+ desc "List custom routes"
6
+ task :list => :environment do
7
+ Route.order(:locale).all.each do |route|
8
+ route_type = route.redirect? ? 'Redirection' : 'Map'
9
+ puts %(GET | /#{route.url} {#{route_type} => #{route.target.class} id: #{route.target.id} #{' locale: ' + route.locale unless route.redirect?}})
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refinerycms-routes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ version: "1.0"
10
+ platform: ruby
11
+ authors:
12
+ - Charles Barbier
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-06-14 00:00:00 Z
18
+ dependencies: []
19
+
20
+ description: Custom routing for Refinery CMS
21
+ email: unixcharles@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - lib/generators/refinerycms_routes_generator.rb
30
+ - lib/generators/templates/pages_controller.rb
31
+ - lib/refinerycms-routes.rb
32
+ - lib/tasks/routes.rake
33
+ - config/locales/en.yml
34
+ - config/routes.rb
35
+ - app/controllers/admin/routes_controller.rb
36
+ - app/helpers/router_helper.rb
37
+ - app/models/route.rb
38
+ - app/views/admin/routes/_actions.html.erb
39
+ - app/views/admin/routes/_form.html.erb
40
+ - app/views/admin/routes/_locale_filters.html.erb
41
+ - app/views/admin/routes/_records.html.erb
42
+ - app/views/admin/routes/_redirection.html.erb
43
+ - app/views/admin/routes/_route.html.erb
44
+ - app/views/admin/routes/_routes.html.erb
45
+ - app/views/admin/routes/edit.html.erb
46
+ - app/views/admin/routes/index.html.erb
47
+ - app/views/admin/routes/new.html.erb
48
+ homepage: http://github.com/unixcharles/refinerycms-copywriting
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Refinery CMS engine that allow you to manage custom localized routes and redirection.
81
+ test_files: []
82
+