Variety of small changes (mostly to properties) plus a few "in the
[selectricity] / vendor / plugins / engines / lib / bundles.rb
1 require 'bundles/require_resource'
2
3 # The 'require_bundle' method is used in views to declare that certain stylesheets and javascripts should
4 # be included by the 'resource_tags' (used in the layout) for the view to function properly.
5 module Bundles
6   def require_bundle(name, *args)
7     method = "bundle_#{name}"
8     send(method, *args)
9   end
10   
11   def require_bundles(*names)
12     names.each { |name| require_bundle(name) }
13   end
14 end
15
16 ActionView::Base.send(:include, Bundles)
17
18 # Registers a module within the Bundles module by renaming the module's 'bundle' method (so it doesn't
19 # clash with other methods named 'bundle') and by including any Controller or Helper modules within
20 # their respective Rails base classes.
21 #
22 # For example, if you have a module such as
23 #   module Bundles::Calendar; end
24 #
25 # then within that Calendar module there *must* be a method named "bundle" which groups the
26 # bundle's resources together.  Example:
27 #   module Bundles::Calendar
28 #     def bundle
29 #       require_relative_to Engines.current.public_dir do
30 #         require_stylesheet "/stylesheets/calendar.css"
31 #         require_javascript "/javascripts/calendar.js"
32 #       end
33 #     end
34 #   end
35 #
36 # You may optionally define a Controller or Helper sub-module if you need any methods available to
37 # the applications controllers or views.  Example:
38 #
39 #   module Bundles::Calendar
40 #     module Helper
41 #       def calendar_date_select(*args
42 #         # ... output some HTML
43 #       end
44 #     end
45 #   end
46 #
47 # The calendar_date_select method will now be available within the scope of the app's views because the
48 # register_bundle method will inject the Helper module's methods in to ActionView::Base for you.
49 #
50 # Similarly, you can make methods available to controllers by adding a Controller module.
51 def register_bundle(name)
52   require "bundles/#{name}"
53   
54   # Rename the generic 'bundle' method in to something that doesn't conflict with
55   # the other module method names.
56   bundle_module = Bundles.const_get(name.to_s.camelize)
57   bundle_module.module_eval "alias bundle_#{name} bundle"
58   bundle_module.send :undef_method, :bundle
59
60   # Then include the bundle module in to the base module, so that the methods will
61   # be available inside ActionView::Base
62   ActionView::Base.send(:include, bundle_module)
63
64   # Check for optional Controller module
65   if bundle_module.const_defined? 'Controller'
66     controller_addon = bundle_module.const_get('Controller')
67     RAILS_DEFAULT_LOGGER.debug "Including #{name} bundle's Controller module"
68     ActionController::Base.send(:include, controller_addon)
69   end
70
71   # Check for optional Helper module
72   if bundle_module.const_defined? 'Helper'
73     helper_addon = bundle_module.const_get('Helper')
74     RAILS_DEFAULT_LOGGER.debug "Including #{name} bundle's Helper module"
75     ActionView::Base.send(:include, helper_addon)
76   end
77 end

Benjamin Mako Hill || Want to submit a patch?