Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions lib/remotipart.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'remotipart/view_helper'
require 'remotipart/request_helper'
require 'remotipart/render_overrides'
require 'remotipart/redirect_to_overrides'
require 'remotipart/middleware'
require 'remotipart/rails' if defined?(Rails)
1 change: 1 addition & 0 deletions lib/remotipart/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Engine < ::Rails::Engine
initializer "remotipart.controller_helper" do
ActionController::Base.send :include, RequestHelper
ActionController::Base.send :include, RenderOverrides
ActionController::Base.send :include, RedirectToOverrides
end

initializer "remotipart.include_middelware" do
Expand Down
1 change: 1 addition & 0 deletions lib/remotipart/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Railtie < ::Rails::Railtie
initializer "remotipart.controller_helper" do
ActionController::Base.send :include, RequestHelper
ActionController::Base.send :include, RenderOverrides
ActionController::Base.send :include, RedirectToOverrides
end

initializer "remotipart.include_middelware" do
Expand Down
38 changes: 38 additions & 0 deletions lib/remotipart/redirect_to_overrides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Overriding the redirect_to for Remotipart. Redirecting the received 'text/html' request to the 'js' format.
require 'uri'

module Remotipart
module RedirectToOverrides
include ERB::Util

def self.included(base)
base.class_eval do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use send instead of class_eval, it will make code faster.

alias_method_chain :redirect_to, :remotipart
end
end

def set_url_format(url, format = :js)
uri, extn = URI.parse(url), '.' + format.to_s
path_fragments = uri.path.split('/')
last_fragment = path_fragments.pop
last_fragment.concat(extn) unless last_fragment.ends_with?(extn)
uri.path = path_fragments.push(last_fragment).join('/')
uri.to_s
end

def redirect_to_with_remotipart *args
if remotipart_submitted?
case redirect_path = args.shift

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to handle the case of something like redirect_to @post

when Hash
redirect_path.merge!(format: :js)
when String
redirect_path = set_url_format(redirect_path, :js)
end
args.unshift(redirect_path)
end

redirect_to_without_remotipart *args
response.body
end
end
end
1 change: 1 addition & 0 deletions remotipart.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Gem::Specification.new do |s|
"lib/remotipart/rails/railtie.rb",
"lib/remotipart/rails/version.rb",
"lib/remotipart/render_overrides.rb",
"lib/remotipart/redirect_to_overrides.rb",
"lib/remotipart/request_helper.rb",
"lib/remotipart/view_helper.rb",
"remotipart.gemspec",
Expand Down