comfy/comfortable-mexican-sofa — pull request #448 by tatey (2014-04-29 01:20:44+00:00)
Title: Fix asset tag rendering css/js when the site has a path.

The asset controller expects the `cms_path` to be present in the URL if the `Site` has a `path`. The asset tag does not render the `cms_path`. That means when you load your page the stylesheet and javascript 404. The approach I took was to append a query string that sets the `cms_path` when rendering the asset tag. I felt like this was the least amount of friction.

``` plain
{{ cms:asset:default:css }}
```

Becomes...

``` html
/cms-css/1/default.css?cms_path=/foo
```

To give more context I'm including snippets from the assets and base controller.

``` ruby
# app/controllers/comfy/cms/assets_controller.rb
class Comfy::Cms::AssetsController < Comfy::Cms::BaseController
before_action :load_cms_layout

def render_css
render :text => @cms_layout.css, :content_type => 'text/css'
end

# ...

protected

def load_cms_layout
@cms_layout = @cms_site.layouts.find_by_identifier!(params[:identifier])
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => 404
end
end
```

``` ruby
# app/controllers/comfy/cms/base_controller.rb
# ...
if @cms_site
if @cms_site.path.present?
if params[:cms_path] && params[:cms_path].match(/\A#{@cms_site.path}/)
params[:cms_path].gsub!(/\A#{@cms_site.path}/, '')
params[:cms_path] && params[:cms_path].gsub!(/\A\//, '')
else
raise ActionController::RoutingError.new('Site Not Found')
end
end
I18n.locale = @cms_site.locale
else
I18n.locale = I18n.default_locale
raise ActionController::RoutingError.new('Site Not Found')
end
# ...
```

The alternative way of approaching it could be relax the restriction on paths in assets controller. The downside is you can't take advantage of the cached @cms_layout.

Open to any feedback.