Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10350 (closed defect: fixed)

Opened 5 months ago

Last modified 2 months ago

[PATCH] Allow custom javascript/stylesheet expansion symbols

Reported by: lotswholetime Assigned to: core
Priority: normal Milestone: 2.1
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc: chuyeow, stepheneb

Description

This patch adds methods to ActionView to register custom asset expansions which work just like :defaults. This will allow plugins which use multiple asset files an easy way for developers to insert them into their application.

This could be a replacement for the current register_javascript_include_default, as the extra asset files may not want to be loaded at all times.

ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey, "head", "body", "tail"
ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey, "head", "body", "tail"

javascript_include_tag :monkey
stylesheet_link_tag :monkey

Attachments

custom_asset_expansions.diff (10.5 kB) - added by lotswholetime on 12/03/07 21:28:08.
custom_asset_expansions.2.diff (7.4 kB) - added by lotswholetime on 12/03/07 22:57:21.
keep application.js at the end
custom_asset_expansions.3.diff (9.0 kB) - added by lotswholetime on 03/05/08 03:20:54.
Added documentation
custom_asset_expansions.4.diff (9.9 kB) - added by lotswholetime on 03/08/08 16:24:56.
Changed register method to take a hash, raise exception on unknown symbol
custom_asset_expansions.5.diff (16.9 kB) - added by lotswholetime on 03/20/08 03:50:38.
Fixed problem with :all expansion not keeping the default sources at the start

Change History

12/03/07 21:28:08 changed by lotswholetime

  • attachment custom_asset_expansions.diff added.

12/03/07 22:22:42 changed by brandon

Cool idea. My only problem with it is that it slightly changes the order of :defaults.

Previously:

expand_javascript_sources :defaults, 'lowpro'
#=> ['prototype', 'effects', 'dragdrop', 'controls', 'lowpro', 'application']

Now:

expand_javascript_sources :defaults, 'lowpro'
#=> ['prototype', 'effects', 'dragdrop', 'controls', 'application', 'lowpro']

This will break for people who were expecting application.js to be included after everything else (people like me).

12/03/07 22:57:21 changed by lotswholetime

  • attachment custom_asset_expansions.2.diff added.

keep application.js at the end

12/03/07 22:58:32 changed by lotswholetime

Thanks for the input Brandon. I updated the patch to keep application.js at the end.

12/04/07 02:56:33 changed by brandon

Looks good to me now.

+1

12/04/07 03:27:25 changed by eigentone

This tastes more like a plugin to me.

12/04/07 03:30:35 changed by lotswholetime

Why is that eigentone? One of the main purposes for this change is to give plugins an unobtrusive hook into the core framework.

12/04/07 17:20:41 changed by railsjitsu

How is this supposed to be tested? I applied your diff, created a monkey.js file and added to my layout

<% javascript_include_tag :monkey %>

And I get the symbols error.

12/04/07 17:35:54 changed by lotswholetime

railsjitsu:

You would want to do something like this...

# environment.rb
ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey, "head", "body", "tail"

# create public/javascripts/head.js
# create public/javascripts/body.js
# create public/javascripts/tail.js

# application.html.erb
<% javascript_include_tag :monkey %>

You should see that this puts in javascript tags for "head", "body" and "tail"

12/05/07 01:53:58 changed by chuyeow

  • cc set to chuyeow.
  • summary changed from Allow custom javascript/stylesheet expansion symbols to [PATCH] Allow custom javascript/stylesheet expansion symbols.

12/05/07 21:39:50 changed by norbert

  • milestone changed from 2.0 to 2.x.

As stated in the description of report {68}, the 2.0 milestone should only be used by core members for real show stoppers.

12/06/07 02:01:35 changed by zdennis

+1 for me as well.

12/06/07 02:10:04 changed by zdennis

This enhances functionality that is already in core, that is why I think it should be committed. I can already include my own javascript files, but it's tedious when there is a common set of scripts I always use. This is not adding superfluous functionality into core, it's merely adding more value to a feature that already exists.

I can see where it would be useful to build a plugin on top of this which makes the register_javascript_expansion call for you.

01/19/08 17:45:37 changed by christocracy

+1

01/19/08 18:45:17 changed by lotswholetime

  • keywords set to verified.

01/19/08 18:52:32 changed by lifofifo

Looks like a good idea. But the patch is lacking documentation.

01/20/08 23:18:52 changed by nzkoz

Looks good guys, just a few thoughts though:

1) are there any plugins which need this functionality? What do their authors think? 2) If we're going to make symbols work outside of :defaults, it'd be nice to raise a nice exception for :some_random_symbol.

01/25/08 23:45:26 changed by lifofifo

  • keywords deleted.

Please add "verified" keyword after adding documentation as suggested here. I reckon javascript_include_tag and stylesheet_link_tag are good places to add the relevant documentation/examples.

Thanks.

03/05/08 03:20:54 changed by lotswholetime

  • attachment custom_asset_expansions.3.diff added.

Added documentation

03/05/08 03:21:09 changed by lotswholetime

  • keywords set to verified.

03/08/08 12:52:09 changed by lifofifo

  • keywords deleted.

Two points :

1. Why can't it get rid of JAVASCRIPT_DEFAULT_SOURCES constant ? 2. As Koz said, it should raise proper exception for :some_random_symbol

Looks good apart from that. One more thing though,

ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"]

ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"], :funky => ["head2", "tail2"]

May be the above format is a bit more intuitive and allows more than registering one expansions with a single call.

03/08/08 15:55:39 changed by lotswholetime

The only reason I left JAVASCRIPT_DEFAULT_SOURCES, register_javascript_include_default, and reset_javascript_include_default was for backwards compatibility. If we are comfortable with breaking that, I can get rid of one or all of those things.

03/08/08 16:24:56 changed by lotswholetime

  • attachment custom_asset_expansions.4.diff added.

Changed register method to take a hash, raise exception on unknown symbol

03/08/08 16:28:23 changed by lifofifo

  • keywords set to verified.

03/13/08 02:31:48 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [9016]) Added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets (closes #10350) [lotswholetime]

03/19/08 19:49:55 changed by bitsweat

  • keywords deleted.
  • status changed from closed to reopened.
  • resolution deleted.
  • milestone changed from 2.x to 2.1.

This breaks asset caching pretty hardcore since it doesn't keep the default sources first. Prototype is included alphabetically in the cache, breaking anything that precedes & relies on it.

I don't understand how this was verified, guys. These +1 are way out of hand.

03/19/08 19:56:10 changed by bitsweat

(In [9063]) Revert [9106]. References #10350.

03/19/08 20:04:27 changed by lotswholetime

I am not experiencing the same problem you are. I have this in my view...

<%= javascript_include_tag :defaults, :dhtml_calendar, :cache => "combined" %>

My combined.js file is in the proper order.

What is the scenario you have where this breaks?

03/19/08 21:53:18 changed by bitsweat

javascript_include_tag :all

03/20/08 03:49:01 changed by lotswholetime

Thanks for the bug report bitsweat. I have updated the patch to include a test for the expected behavior of keeping the defaults first (this was untested before), and updated the implementation accordingly.

03/20/08 03:50:38 changed by lotswholetime

  • attachment custom_asset_expansions.5.diff added.

Fixed problem with :all expansion not keeping the default sources at the start

03/20/08 08:38:27 changed by bitsweat

Thank you!

03/20/08 16:26:08 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [9065]) Re-added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets. Closes #10350.

03/20/08 16:51:10 changed by bitsweat

(In [9066]) Missed adds. References #10350.

03/22/08 23:30:52 changed by stepheneb

  • cc changed from chuyeow to chuyeow, stepheneb.