Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a45a28d
specify the file extension when rendering divert template to prevent …
Jul 7, 2017
3fed570
add_redirect_from_view to config
Oct 12, 2017
1ed62b6
change thither column to text
Oct 12, 2017
1f8d0ed
redirect from clientside view when saving to the database
Oct 12, 2017
9c74858
if diverting clientside, make sure layout exists
Oct 12, 2017
b61dfbf
update readme
Oct 12, 2017
a761949
add :divert_redirect_to param
Oct 12, 2017
3a4c4f5
render divert_clientside with a 302 status
Oct 12, 2017
783c7c3
update readme formatting
froesecom Oct 12, 2017
a3247e7
spelling mistake
froesecom Oct 12, 2017
acbaf3b
increment version
Oct 12, 2017
627a793
Merge branch 'master' of github.com:hardhatdigital/divert
Oct 12, 2017
4d45882
add divert_redirect_from parameter when redirecting from clientside
Oct 12, 2017
fcf4bfd
Change status to 301
Jan 17, 2018
c99164e
Merge branch 'development'
Jan 17, 2018
85614f4
Update version
Jan 17, 2018
ad24907
Add 301 redirect status code when redirectin'
Jan 17, 2018
88fce81
Merge branch 'master' into development
Jan 17, 2018
924807d
Update version number
Jan 17, 2018
8e82de9
fix bug where divert records were being created if save_to_db was false
froesecom Mar 21, 2018
0d5e9d5
update version
froesecom Mar 21, 2018
bd2b009
fix bug
froesecom Mar 21, 2018
98a5aec
update version
froesecom Mar 21, 2018
284d838
fix redirect bug when not saving redirects to database
Mar 21, 2018
a7dffb0
Add constraints to divert route
dtcristo Apr 3, 2018
232ea10
Fix typo
dtcristo Apr 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ By default, Divert will hit the database to find or create a record. To turn off
Divert.configure do |config|
config.save_to_db = false
end

== Diverting Clientside

By default, the Divert gem will redirect from the server. However, there are times you might want to redirect from clientside, for example, if you have to do some custom analytics tracking using a clientside script, such as Google Analytics.

To turn on clientside redirects, add this following configuration code in your `config/initializers/divert.rb` :
Divert.configure do |config|
config.redirect_clientside = true
end

Divert expects a view named <tt>[controller_name]/divert_clientside.html.erb</tt>, which you will need to create.

You will need to handle the clientside logic yourself. You have access to the following parameters: <tt>params[:divert_redirect_to]</tt> and <tt>params[:divert_redirect_from]</tt>.



2 changes: 1 addition & 1 deletion db/migrate/20130311224617_create_divert_redirects.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateDivertRedirects < ActiveRecord::Migration
def change
create_table :divert_redirects do |t|
t.string :hither
t.text :hither
t.string :thither
t.integer :hits, default: 0
t.boolean :active, default: true
Expand Down
39 changes: 32 additions & 7 deletions lib/divert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def self.configure
yield(configuration)
end

def hit_path(request)
request.fullpath.chomp("/")
end

def action_missing path = nil
controller = Divert.configuration.controller || controller_name

Expand All @@ -23,28 +27,49 @@ def action_missing path = nil
send path and return
end

#use rails template_exists? method to check if view exists in the view dir
#corresponding to the controller name. If so, render the view.
if template_exists?(path, [controller])
render "#{controller}/#{path}" and return
end

if Divert.configuration.save_to_db && (redirect = Redirect.hit(request.fullpath))
redirect_to redirect

divert = if Divert.configuration.save_to_db
Redirect.find_or_create_by(hither: hit_path(request))
else
Redirect.find_by(hither: hit_path(request))
end

if divert && (redirect = divert.hit)
if Divert.configuration.redirect_clientside

unless template_exists? 'divert_clientside.html.erb', [controller]
render text:"Missing divert view: #{controller}/divert_clientside.html.erb", :status => 404 and return
else
params[:divert_redirect_to] = redirect
params[:divert_redirect_from] = divert.hither
render "#{controller}/divert_clientside.html.erb", :layout => false, :status => 301 and return
end

else
redirect_to redirect, :status => 301
end
else
unless template_exists? 'divert', [controller]
unless template_exists? 'divert.html.erb', [controller]
render text:"Missing divert view: #{controller}/divert.html.erb", :status => 404 and return
end
render "#{controller}/divert", :status => 404 and return
render "#{controller}/divert.html.erb", :status => 404 and return
end
end

alias_method :divert, :action_missing
end

class ActionDispatch::Routing::Mapper
def divert_with controller
def divert_with controller, constraints: nil
# Set controller in configuration if not already set
Divert.configuration.controller ||= controller.to_s
get ':action' => controller.to_s
get '*path' => "#{controller}#action_missing", :format => false
get ':action' => controller.to_s, :constraints => constraints
get '*path' => "#{controller}#action_missing", :format => false, :constraints => constraints
end
end
2 changes: 2 additions & 0 deletions lib/divert/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Divert
class Configuration
attr_accessor :save_to_db
attr_accessor :redirect_clientside
attr_accessor :controller

def initialize
@save_to_db = true
@redirect_clientside = false
end
end
end
2 changes: 1 addition & 1 deletion lib/divert/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Divert
VERSION = "0.9"
VERSION = "0.9.7"
end