refinerycms-redirections 2.0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. data/README.md +28 -0
  2. data/app/controllers/refinery/redirections/admin/redirections_controller.rb +11 -0
  3. data/app/controllers/refinery/redirections/redirections_controller.rb +8 -0
  4. data/app/models/refinery/redirections/redirection.rb +35 -0
  5. data/app/views/refinery/redirections/admin/redirections/_actions.html.erb +11 -0
  6. data/app/views/refinery/redirections/admin/redirections/_form.html.erb +12 -0
  7. data/app/views/refinery/redirections/admin/redirections/_index.html.erb +6 -0
  8. data/app/views/refinery/redirections/admin/redirections/_inner_form.html.erb +15 -0
  9. data/app/views/refinery/redirections/admin/redirections/_records.html.erb +15 -0
  10. data/app/views/refinery/redirections/admin/redirections/_redirection.html.erb +18 -0
  11. data/app/views/refinery/redirections/admin/redirections/_redirections.html.erb +1 -0
  12. data/app/views/refinery/redirections/admin/redirections/edit.html.erb +3 -0
  13. data/app/views/refinery/redirections/admin/redirections/index.html.erb +1 -0
  14. data/app/views/refinery/redirections/admin/redirections/new.html.erb +2 -0
  15. data/config/locales/en/models/redirections.yml +16 -0
  16. data/config/locales/en/plugin.yml +5 -0
  17. data/config/locales/en/views/admin/redirections.yml +14 -0
  18. data/config/locales/es/models/redirections.yml +16 -0
  19. data/config/locales/es/plugin.yml +5 -0
  20. data/config/locales/es/views/admin/redirections.yml +14 -0
  21. data/config/routes.rb +13 -0
  22. data/db/migrate/20130826114744_create_redirections.rb +11 -0
  23. data/lib/generators/refinery/redirections/redirections_generator.rb +15 -0
  24. data/lib/generators/refinery/redirections/templates/config/initializers/refinery/redirections.rb.erb +17 -0
  25. data/lib/refinery/redirections.rb +28 -0
  26. data/lib/refinery/redirections/configuration.rb +14 -0
  27. data/lib/refinery/redirections/engine.rb +42 -0
  28. data/lib/refinery/redirections/routes_constraint.rb +20 -0
  29. data/lib/refinerycms-redirections.rb +1 -0
  30. metadata +153 -0
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ This a RefineryCMS plugin for managing redirections from the backend.
2
+
3
+ Installing
4
+ ==========
5
+
6
+ In Gemfile `gem "refinerycms-redirections", "~> 2.0.10.0"`
7
+
8
+ Run `bundle install`
9
+
10
+ And then run `rails g refinery:redirections` to install migrations and a configuration initializer.
11
+
12
+ Current status
13
+ ==============
14
+
15
+ The plugin includes a back section for managing the redirections where you can set the original URL, the URL we want to redirect to and the HTTP status code we want to return.
16
+
17
+ It also includes a set of url params which should be avoided when comparing urls (cause we don't want any campaign params to ruin our redirections, right?).
18
+
19
+ Redirections are performed with a mix of rack-rewrite rules and a plain old controller. Each option had its drawbacks so we thought it would be great to have both and being able to disable each of them from the config file.
20
+
21
+ Wishlist
22
+ ========
23
+
24
+ * Redirections are quite regular data and they could be managed through a CSV or XLS file. It would be great *to import such a file with redirections*
25
+
26
+ * *Priority*
27
+
28
+ * Support Regexp redirections: Right now, regexp are supported (I think) via rack rewrite, but if we want regexp supported in the controller version we should research about database regexp support
@@ -0,0 +1,11 @@
1
+ module Refinery
2
+ module Redirections
3
+ module Admin
4
+ class RedirectionsController < ::Refinery::AdminController
5
+
6
+ crudify :'refinery/redirections/redirection', title_attribute: :from_url
7
+
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ class Refinery::Redirections::RedirectionsController < ActionController::Base
2
+
3
+ def redirect
4
+ redirection = Refinery::Redirections::Redirection.from_url(request.fullpath).first
5
+ redirect_to redirection.to_url, status: redirection.status_code unless redirection.nil?
6
+ end
7
+
8
+ end
@@ -0,0 +1,35 @@
1
+ class Refinery::Redirections::Redirection < ActiveRecord::Base
2
+ extend Enumerize
3
+
4
+ attr_accessible :from_url, :status_code, :to_url
5
+
6
+ enumerize :status_code, in: ["301", "302", "303", "307"]
7
+
8
+ validates :from_url, :status_code, :to_url, presence: true
9
+
10
+ before_save :sanitize_path
11
+
12
+ def sanitize_path
13
+ self.from_url = self.class.sanitize_path from_url
14
+ end
15
+
16
+ scope :from_url, ->(path) { where from_url: sanitize_path(path) }
17
+
18
+ # This method gets a path and 'sanitizes it'. This means that all the params that we don't want (Google Analytics params, testing params...) are stripped out and params are sorted so '/en?a=1&b=1' and '/en?b=1&a=1' are the same
19
+ def self.sanitize_path path, ignored_path_params = Refinery::Redirections.ignored_path_params
20
+ path, params = path.split("?")
21
+ if params.blank?
22
+ path
23
+ else
24
+ # We split the params
25
+ params = params.split('&')
26
+ # And then get those ignored and remove them
27
+ not_ignored_params = params.reject{|p| ignored_path_params.detect{|ignored| p.start_with?("#{ignored}=") || (p == ignored)}}
28
+
29
+ not_ignored_params.blank? ? path : "#{path}?#{not_ignored_params.sort.join('&')}"
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,11 @@
1
+ <ul>
2
+ <li>
3
+ <%= link_to t('.create_new'), refinery.new_redirections_admin_redirection_path,
4
+ :class => "add_icon" %>
5
+ </li>
6
+ <li>
7
+ <%= link_to t('.list'), refinery.redirections_admin_redirections_path,
8
+ :class => "manage_icon" %>
9
+ </li>
10
+ </ul>
11
+
@@ -0,0 +1,12 @@
1
+ <%= form_for [refinery, :redirections_admin, object], html: {multipart: true} do |f| -%>
2
+ <%= render '/refinery/admin/error_messages',
3
+ :object => object,
4
+ :include_object_name => true %>
5
+
6
+ <%= render "/refinery/redirections/admin/redirections/inner_form", f: f, object: object %>
7
+
8
+ <%= render '/refinery/admin/form_actions', :f => f,
9
+ :continue_editing => true,
10
+ :delete_title => t('delete', :scope => "refinery.redirections.admin.redirections"),
11
+ :delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => object.from_url) %>
12
+ <% end -%>
@@ -0,0 +1,6 @@
1
+ <section id='records'>
2
+ <%= render 'refinery/redirections/admin/redirections/records', records: records %>
3
+ </section>
4
+ <aside id='actions'>
5
+ <%= render 'actions' %>
6
+ </aside>
@@ -0,0 +1,15 @@
1
+ <div class='field'>
2
+ <%= f.label :from_url -%>
3
+ <%= f.text_field :from_url, :class => 'larger widest' -%>
4
+ </div>
5
+
6
+ <div class='field'>
7
+ <%= f.label :to_url -%>
8
+ <%= f.text_field :to_url, :class => 'larger widest' -%>
9
+ </div>
10
+
11
+ <div class='field'>
12
+ <%= f.label :status_code -%>
13
+
14
+ <%= f.select :status_code, Refinery::Redirections::Redirection.status_code.options, class: 'widest' -%>
15
+ </div>
@@ -0,0 +1,15 @@
1
+ <div class='pagination_container'>
2
+ <% if records.any? %>
3
+
4
+ <%= will_paginate records if controller.class.pageable? %>
5
+ <ul id='sortable_list'>
6
+ <%= render partial: "/refinery/redirections/admin/redirections/redirection", collection: records, as: 'redirection' %>
7
+ </ul>
8
+ <% else %>
9
+ <p>
10
+ <strong>
11
+ <%= t('.no_items_yet') %>
12
+ </strong>
13
+ </p>
14
+ <% end %>
15
+ </div>
@@ -0,0 +1,18 @@
1
+ <li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(redirection) -%>">
2
+ <span class='title'>
3
+ <%= redirection.from_url %>
4
+ </span>
5
+ <span>
6
+ <%= redirection.to_url %>
7
+ </span>
8
+ <span class='actions'>
9
+
10
+ <%= link_to refinery_icon_tag("application_edit.png"), refinery.edit_redirections_admin_redirection_path(redirection),
11
+ :title => t('.edit') %>
12
+ <%= link_to refinery_icon_tag("delete.png"), refinery.redirections_admin_redirection_path(redirection),
13
+ :class => "cancel confirm-delete",
14
+ :title => t('.delete'),
15
+ :confirm => t('message', :scope => 'refinery.admin.delete', :title => redirection.from_url),
16
+ :method => :delete %>
17
+ </span>
18
+ </li>
@@ -0,0 +1 @@
1
+ <%= render 'refinery/redirections/admin/redirections/records', records: @redirections %>
@@ -0,0 +1,3 @@
1
+ <%= render 'refinery/redirections/admin/redirections/form', object: @redirection %>
2
+
3
+
@@ -0,0 +1 @@
1
+ <%= render 'refinery/redirections/admin/redirections/index', records: @redirections %>
@@ -0,0 +1,2 @@
1
+ <%= render 'refinery/redirections/admin/redirections/form', object: @redirection %>
2
+
@@ -0,0 +1,16 @@
1
+ en:
2
+ activerecord:
3
+ attributes:
4
+ refinery/redirections/redirection:
5
+ from_url: Url we redirect from
6
+ to_url: Url we redirect to
7
+ status_code: Redirection type
8
+ models:
9
+ refinery/redirections/redirection: 'Redirection'
10
+ enumerize:
11
+ refinery/redirections/redirection:
12
+ status_code:
13
+ "301": "Permanente redirection (301)"
14
+ "302": "Found (302)"
15
+ "303": "See Other (303)"
16
+ "307": "Temporary redirect (307)"
@@ -0,0 +1,5 @@
1
+ en:
2
+ refinery:
3
+ plugins:
4
+ refinerycms_redirections:
5
+ title: Redirecciones
@@ -0,0 +1,14 @@
1
+ en:
2
+ refinery:
3
+ redirections:
4
+ admin:
5
+ redirections:
6
+ actions:
7
+ create_new: Create new redirection
8
+ list: View all redirections
9
+ records:
10
+ title: Redirections
11
+ no_items_yet: "There are no redirections yet. click on 'Create new redirection' to create your first redirection."
12
+ redirection:
13
+ edit: Edit this redirection
14
+ delete: Delete this redirection
@@ -0,0 +1,16 @@
1
+ es:
2
+ activerecord:
3
+ attributes:
4
+ refinery/redirections/redirection:
5
+ from_url: Url original
6
+ to_url: Url a la que redirigir
7
+ status_code: Tipo de redirección
8
+ models:
9
+ refinery/redirections/redirection: 'Redirección'
10
+ enumerize:
11
+ refinery/redirections/redirection:
12
+ status_code:
13
+ "301": "Redirección permanente (301)"
14
+ "302": "Redirección temporal (302)"
15
+ "303": "Vea Otra (303)"
16
+ "307": "Redirección temporal (307)"
@@ -0,0 +1,5 @@
1
+ es:
2
+ refinery:
3
+ plugins:
4
+ refinerycms_redirections:
5
+ title: Redirecciones
@@ -0,0 +1,14 @@
1
+ es:
2
+ refinery:
3
+ redirections:
4
+ admin:
5
+ redirections:
6
+ actions:
7
+ create_new: Crear nueva redirección
8
+ list: Ver todas las redirecciones
9
+ records:
10
+ title: Redirecciones
11
+ no_items_yet: "No hay redirecciones todavía. Pulsa en 'Crear nuevo redirección' para crear tu primer redirección."
12
+ redirection:
13
+ edit: Editar esta redirección
14
+ delete: Borrar esta redirección para siempre
data/config/routes.rb ADDED
@@ -0,0 +1,13 @@
1
+ Refinery::Core::Engine.routes.prepend do
2
+
3
+ if Refinery::Redirections.enable_route_redirection
4
+ match "/*path" => 'redirections/redirections#redirect', constraints: Refinery::Redirections::RoutesConstraint
5
+ end
6
+
7
+ namespace :redirections, path: '' do
8
+ namespace :admin, :path => 'refinery' do
9
+ resources :redirections, :except => :show
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,11 @@
1
+ class CreateRedirections < ActiveRecord::Migration
2
+ def change
3
+ create_table Refinery::Redirections::Redirection.table_name do |t|
4
+ t.string :from_url
5
+ t.string :to_url
6
+ t.integer :status_code, default: 301
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Refinery
2
+ class RedirectionsGenerator < Rails::Generators::Base
3
+
4
+ source_root File.expand_path("../templates/", __FILE__)
5
+
6
+ def generate_ue_core_initializer
7
+ template "config/initializers/refinery/redirections.rb.erb", File.join(destination_root, "config", "initializers", "refinery", "redirections.rb")
8
+ end
9
+
10
+ def rake_db
11
+ rake("refinery_redirections:install:migrations")
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ Refinery::Redirections.configure do |config|
2
+
3
+ # Here we include params in the path that must be ignored when looking for a redirection
4
+ config.ignored_path_params = <%= Refinery::Redirections.ignored_path_params.inspect %>
5
+
6
+ # Here we enable rack rewrite redirection. This will create rack rewrite rules on startup and enable its redirections.
7
+ # WARNING: Any new redirection in the back requires the app to restart
8
+ # WARNING: Changing this setting requires the app to restart
9
+ config.enable_rack_redirection = <%= Refinery::Redirections.enable_rack_redirection.inspect %>
10
+
11
+ # Here we enable the redirections controller through a route in the config/routes.rb file. This will create a redirection route that will match every request
12
+ # WARNING: Any new redirection will be applied inmediately
13
+ # WARNING: This will execute one database query per request to check if a redirection is present, increasing DB load
14
+ # WARNING: Changing this setting requires the app to restart
15
+ config.enable_route_redirection = <%= Refinery::Redirections.enable_route_redirection.inspect %>
16
+
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'refinerycms-core'
2
+ require 'rack/rewrite'
3
+ require 'enumerize'
4
+
5
+ module Refinery
6
+
7
+ autoload :RedirectionsGenerator, 'generators/refinery/redirections/redirections_generator'
8
+
9
+ module Redirections
10
+
11
+ require 'refinery/redirections/engine'
12
+ require 'refinery/redirections/configuration'
13
+ require 'refinery/redirections/routes_constraint'
14
+
15
+ attr_writer :root
16
+
17
+ class << self
18
+ def root
19
+ @root ||= Pathname.new(File.expand_path('../../../', __FILE__))
20
+ end
21
+
22
+ def factory_paths
23
+ @factory_paths ||= [ root.join("spec/factories").to_s ]
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,14 @@
1
+ module Refinery
2
+ module Redirections
3
+ include ActiveSupport::Configurable
4
+
5
+ config_accessor :ignored_path_params
6
+ config_accessor :enable_rack_redirection
7
+ config_accessor :enable_route_redirection
8
+
9
+ self.ignored_path_params = ["utm_medium","utm_source","utm_campaign","utm_content","utm_term"]
10
+ self.enable_rack_redirection = true
11
+ self.enable_route_redirection = true
12
+
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ module Refinery
2
+ module Redirections
3
+ class Engine < Rails::Engine
4
+ include Refinery::Engine
5
+
6
+ isolate_namespace Refinery::Redirections
7
+
8
+ initializer "register refinerycms_redirections plugin" do
9
+ Refinery::Plugin.register do |plugin|
10
+ plugin.pathname = root
11
+ plugin.name = "refinerycms_redirections"
12
+ plugin.url = proc { Refinery::Core::Engine.routes.url_helpers.redirections_admin_redirections_path }
13
+ plugin.menu_match = /refinery\/redirections\/?/
14
+ plugin.activity = { :class_name => :'refinery/redirections/redirection' }
15
+ end
16
+ end
17
+
18
+ config.after_initialize do
19
+ Refinery.register_engine(Refinery::Redirections)
20
+ end
21
+
22
+
23
+ initializer 'add rack rewrite rules' do |app|
24
+
25
+ if Refinery::Redirections.enable_rack_redirection
26
+ app.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
27
+ Refinery::Redirections::Redirection.all.each do |redirection|
28
+ if self.respond_to? "r#{redirection.status_code}"
29
+ send("r#{redirection.status_code}", redirection.from_url, redirection.to_url)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ paths["config/locales"] << "config/locales/**"
38
+
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module Refinery::Redirections
2
+ class RoutesConstraint
3
+
4
+ # A redirection must be found in the database
5
+ def self.matches?(request)
6
+
7
+ redirection = Refinery::Redirections::Redirection.from_url(request.fullpath).first
8
+
9
+ if redirection.nil?
10
+ Rails.logger.info "[Redirections] Redirection not found for: #{request.fullpath.inspect}"
11
+ else
12
+ Rails.logger.info "[Redirections] Redirection found from: #{request.fullpath.inspect} to #{redirection.to_url} with status #{redirection.status_code}"
13
+ end
14
+
15
+ !redirection.nil?
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1 @@
1
+ require 'refinery/redirections'
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refinerycms-redirections
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.10.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - david.brenes@the-cocktail.com
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: refinerycms-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.10
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.10
30
+ - !ruby/object:Gem::Dependency
31
+ name: refinerycms-i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack-rewrite
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: enumerize
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.7.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.7.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: refinerycms-testing
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.10
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.10
94
+ description: RefineryCMS plugin for managing HTTP redirections
95
+ email:
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files: []
99
+ files:
100
+ - app/models/refinery/redirections/redirection.rb
101
+ - app/views/refinery/redirections/admin/redirections/_actions.html.erb
102
+ - app/views/refinery/redirections/admin/redirections/new.html.erb
103
+ - app/views/refinery/redirections/admin/redirections/_redirections.html.erb
104
+ - app/views/refinery/redirections/admin/redirections/_redirection.html.erb
105
+ - app/views/refinery/redirections/admin/redirections/_inner_form.html.erb
106
+ - app/views/refinery/redirections/admin/redirections/edit.html.erb
107
+ - app/views/refinery/redirections/admin/redirections/index.html.erb
108
+ - app/views/refinery/redirections/admin/redirections/_records.html.erb
109
+ - app/views/refinery/redirections/admin/redirections/_form.html.erb
110
+ - app/views/refinery/redirections/admin/redirections/_index.html.erb
111
+ - app/controllers/refinery/redirections/admin/redirections_controller.rb
112
+ - app/controllers/refinery/redirections/redirections_controller.rb
113
+ - config/routes.rb
114
+ - config/locales/es/models/redirections.yml
115
+ - config/locales/es/plugin.yml
116
+ - config/locales/es/views/admin/redirections.yml
117
+ - config/locales/en/models/redirections.yml
118
+ - config/locales/en/plugin.yml
119
+ - config/locales/en/views/admin/redirections.yml
120
+ - db/migrate/20130826114744_create_redirections.rb
121
+ - lib/refinerycms-redirections.rb
122
+ - lib/generators/refinery/redirections/redirections_generator.rb
123
+ - lib/generators/refinery/redirections/templates/config/initializers/refinery/redirections.rb.erb
124
+ - lib/refinery/redirections.rb
125
+ - lib/refinery/redirections/routes_constraint.rb
126
+ - lib/refinery/redirections/engine.rb
127
+ - lib/refinery/redirections/configuration.rb
128
+ - README.md
129
+ homepage:
130
+ licenses: []
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 1.8.24
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: RefineryCMS plugin for managing HTTP redirections
153
+ test_files: []