1 require 'bundles/require_resource'
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.
6 def require_bundle(name, *args)
7 method = "bundle_#{name}"
11 def require_bundles(*names)
12 names.each { |name| require_bundle(name) }
16 ActionView::Base.send(:include, Bundles)
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.
22 # For example, if you have a module such as
23 # module Bundles::Calendar; end
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
29 # require_relative_to Engines.current.public_dir do
30 # require_stylesheet "/stylesheets/calendar.css"
31 # require_javascript "/javascripts/calendar.js"
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:
39 # module Bundles::Calendar
41 # def calendar_date_select(*args
42 # # ... output some HTML
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.
50 # Similarly, you can make methods available to controllers by adding a Controller module.
51 def register_bundle(name)
52 require "bundles/#{name}"
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
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)
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)
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)