Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rails' stylesheet_link_tag and javascript_include_tag in a Liquid Drop

I want to be able to give the users full control and edit the layout. I also want them to be able to include what javascript plugins they want. Therefore, I had to make an interface to allow them to do that.

For example, the default html looks like a more complicated version of this:

<head>
  <title>{{site.name}}</title>
  ...
  {{js_plugins.colorbox}} # this should return the necessary javascript and/or stylesheet tags
</head>

My Liquid JsPlugins drop is like this:

class JsPluginsDrop < Liquid::Drop
  include ActionView::Helpers::AssetTagHelper
  ...
  def colorbox
    javascript_include_tag "/path/to/js"
  end
end

When I run my specs though, I get this error (note that you see @drop["colorbox-1.3.15"] when the code I supplied above acts differently. However, I wanted to simplify my code since that's not the problem, it's the usage of the TagHelper that is the problem):

Failures:
  1) JsPluginsDrop colorbox-1.3.15 should return the correct script tags
     Failure/Error: @drop["colorbox-1.3.15"].stylesheets.should include("/jquery-plugins/colorbox-1.3.15/example1/colorbox.css")
     undefined local variable or method `config' for #<JsPluginsDrop:0xcbfab38>
     # ./app/drops/js_plugins_drop.rb:22:in `stylesheets'
     # ./spec/models/js_plugins_drop_spec.rb:11

I won't be surprised if the problem is caused by the fact that this is separate from my Rails environment, and the drop does not have access to the config of Rails. Since I still want to be able to use these convenience methods and :cache => true that they give, how can I use the stylesheet_link_tag and javascript_include_tag from within a drop, if it's possible at all?

like image 308
Ramon Tayag Avatar asked Nov 05 '22 01:11

Ramon Tayag


1 Answers

It seems that this is possible now when done this way:

class MyDrop < Liquid::Drop

  ...
  def my_js_tag
    helpers.javascript_include_tag '/some/thing'
  end
  ...

  def helpers
    @helpers ||= ActionController::Base.helpers
  end

end
like image 165
Ramon Tayag Avatar answered Nov 12 '22 19:11

Ramon Tayag