Skip to content

Commit

Permalink
fix path handling for subdir mounting (fixes phusion#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
stbuehler committed Aug 17, 2013
1 parent 4c0641f commit a4cbb29
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ reinstallBehavior = (context)->
textarea.delayedObserver ->
callback = (html)->
$('.preview .content', form).html(html)
$.get('/admin/comments/preview', content: textarea.val(), callback)
$.get('./admin/comments/preview', content: textarea.val(), callback)

This comment has been minimized.

Copy link
@FooBarWidget

FooBarWidget Aug 21, 2013

You should use root_path here instead of ./.

This comment has been minimized.

Copy link
@stbuehler

stbuehler Aug 21, 2013

Author Owner

No; assets can't depend on request data (also I don't think this is going through erb or similar, so it actually can't access it).

This script is only loaded in the admin interface; HTML pages for the admin layout should all go through app/views/layouts/_custom.html.erb, which is modified below to set the document.baseURL with a <base> element to root_path; that should make this relative link work (and it does for me).


$(document).ready ->
reinstallBehavior()
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/help/_code.txt.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}

var result =
'<%= request.protocol %><%= request.host_with_port %>/api/show_topic.js' +
'//<%= request.host_with_port %><%= root_path %>api/show_topic.js' +
'?_c=' + window._juviaRequestCounter +
'&' + makeQueryString(options);
window._juviaRequestCounter++;
Expand Down
4 changes: 2 additions & 2 deletions app/views/api/base.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ if (!Juvia) {
var $container = $(form).closest('.juvia-container');
this.setSubmitting($container, true);
this.saveCommentBox($container);
this.loadScript('/api/add_comment', {
this.loadScript('<%= root_path %>api/add_comment', {
site_key : $container.data('site-key'),
topic_key : $container.data('topic-key'),
topic_title : $container.data('topic-title'),
Expand All @@ -242,7 +242,7 @@ if (!Juvia) {
Juvia.previewComment = function(formElement) {
var $container = $(formElement).closest('.juvia-container');
this.saveCommentBox($container);
this.loadScript('/api/preview_comment', {
this.loadScript('<%= root_path %>api/preview_comment', {
site_key : $container.data('site-key'),
topic_key: $container.data('topic-key'),
content : this.compress($('textarea[name="content"]', $container).val())
Expand Down
11 changes: 6 additions & 5 deletions app/views/layouts/_custom.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en-us" class="no-js"> <!--<![endif]-->

<head>
<base href="<%= root_path %>">
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" />
<meta charset="utf-8" />

<title><% if @title %><%= @title %> - <% end %>Juvia</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="shortcut icon" href="<%= root_path %>favicon.ico" />

<!-- The Columnal Grid (1140px wide base, load first), Type and image presets, and mobile stylesheet -->
<%= stylesheet_link_tag 'columnal' %>
Expand Down Expand Up @@ -50,9 +51,9 @@
<div class="row">
<div id="site_logo" class="col_5 clearleft">
<a href="http://www.phusion.nl/" class="product_creator">Phusion</a>
<a href="/" class="product_logo"><%= image_tag 'logo-96.png', :width => 96, :height => 61, :alt => "Logo" %></a>
<a href="/" class="product_name">Juvia</a>
<a href="/" class="product_desc">commenting system</a>
<a href="<%= root_path %>" class="product_logo"><%= image_tag 'logo-96.png', :width => 96, :height => 61, :alt => "Logo" %></a>
<a href="<%= root_path %>" class="product_name">Juvia</a>
<a href="<%= root_path %>" class="product_desc">commenting system</a>
</div>
<nav class="col_7 omega">
<% if user_signed_in? %>
Expand All @@ -61,7 +62,7 @@
<% else %>
<div><%= link_to 'Login', new_user_session_path %></div>
<% end %>
<div><a href="/admin/help" class="<%= maybe_active(:help) %>">Help</a></div>
<div><a href="<%= admin_help_path %>" class="<%= maybe_active(:help) %>">Help</a></div>
<% if user_signed_in? %>
<div><%= link_to 'Logout', destroy_user_session_path, :method => :delete %></div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
put 'admin/dashboard/create_admin', :to => 'admin/dashboard#create_admin'
get 'admin/dashboard/new_site', :to => 'admin/dashboard#new_site'
put 'admin/dashboard/create_site', :to => 'admin/dashboard#create_site'
get 'admin/help(/:action)', :to => 'admin/help'
get 'admin/help(/:action)', :to => 'admin/help', :as => :admin_help

match 'test/:action', :to => 'test' if Rails.env.test?

Expand Down
15 changes: 15 additions & 0 deletions lib/app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,18 @@ module RailsExtensions
attr_accessor :config
end
end

# Fix asset paths to work in subdir mounted apps
module ActionView
class AssetPaths
# setup url helper usage
include ActionController::UrlFor
include Rails.application.routes.url_helpers
delegate :env, :request, :to => :controller

# fix relative url root
def relative_url_root
root_path.chomp('/')

This comment has been minimized.

Copy link
@FooBarWidget

FooBarWidget Aug 21, 2013

What does this exactly fix?

This comment has been minimized.

Copy link
@stbuehler

stbuehler Aug 21, 2013

Author Owner

The original definition for that function is (action-pack: lib/action_view/asset_paths.rb):

    def relative_url_root
      config.relative_url_root
    end

So you'd have to configure the subdirectory you mounted juvia at explicitly. The fix should make it work at any path, without configuration.
Perhaps there are more elegant ways of doing this...

For example you can mount juvia at different urls at the same time, like http://j.example.com/ and http://example.com/juvia/

end
end
end

0 comments on commit a4cbb29

Please sign in to comment.