diff --git a/README.rdoc b/README.rdoc
index abf52d8..da066a8 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -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 [controller_name]/divert_clientside.html.erb, which you will need to create.
+
+You will need to handle the clientside logic yourself. You have access to the following parameters: params[:divert_redirect_to] and params[:divert_redirect_from].
+
+
+
diff --git a/db/migrate/20130311224617_create_divert_redirects.rb b/db/migrate/20130311224617_create_divert_redirects.rb
index a1c9402..0665b54 100644
--- a/db/migrate/20130311224617_create_divert_redirects.rb
+++ b/db/migrate/20130311224617_create_divert_redirects.rb
@@ -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
diff --git a/lib/divert.rb b/lib/divert.rb
index 63aa471..3403441 100644
--- a/lib/divert.rb
+++ b/lib/divert.rb
@@ -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
@@ -23,17 +27,38 @@ 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
@@ -41,10 +66,10 @@ def action_missing path = nil
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
diff --git a/lib/divert/configuration.rb b/lib/divert/configuration.rb
index 5b7c8b9..d454927 100644
--- a/lib/divert/configuration.rb
+++ b/lib/divert/configuration.rb
@@ -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
diff --git a/lib/divert/version.rb b/lib/divert/version.rb
index 2ef2370..0518356 100644
--- a/lib/divert/version.rb
+++ b/lib/divert/version.rb
@@ -1,3 +1,3 @@
module Divert
- VERSION = "0.9"
+ VERSION = "0.9.7"
end