jeremyboles-smart-asset-helper 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'asset_manager'
|
2
|
+
|
3
|
+
module SmartAssetHelper
|
4
|
+
|
5
|
+
# Prevent a javscript file from being required
|
6
|
+
def deny_javascript(*javascripts)
|
7
|
+
@_asset_manager.deny(:js, javascripts)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Prevent a stylesheet file from being required
|
11
|
+
def deny_stylesheet(*stylesheets)
|
12
|
+
@_asset_manager.deny(:css, stylesheets)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Print required javascript
|
16
|
+
def include_javascript(*args)
|
17
|
+
options = extract_options(args)
|
18
|
+
javascript = javascript_include_tag(*include_assets(:js, args, options))
|
19
|
+
javascript += ie_assets(:js, args) if options[:ie]
|
20
|
+
javascript.gsub("\n", '')
|
21
|
+
end
|
22
|
+
|
23
|
+
# Print required stylesheets
|
24
|
+
def include_stylesheets(*args)
|
25
|
+
options = extract_options(args)
|
26
|
+
stylesheets = stylesheet_link_tag(*include_assets(:css, args, options))
|
27
|
+
stylesheets += ie_assets(:css, args) if options[:ie]
|
28
|
+
stylesheets.gsub("\n", '')
|
29
|
+
end
|
30
|
+
|
31
|
+
# Require bot Javscript and CSS
|
32
|
+
def require_assets(*assets)
|
33
|
+
require_stylesheet(*assets)
|
34
|
+
require_javascript(*assets)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Request a javascript to be outputed by include_javascript
|
38
|
+
def require_javascript(*javascripts)
|
39
|
+
@_asset_manager.add(:js, javascripts)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Request a stylesheet to be outputed by include_stylesheets
|
43
|
+
def require_stylesheet(*stylesheets)
|
44
|
+
@_asset_manager.add(:css, stylesheets)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def conditional_comment(text, version)
|
50
|
+
"<!--[if IE #{version}]>#{text}<![endif]-->"
|
51
|
+
end
|
52
|
+
|
53
|
+
def extract_options(args)
|
54
|
+
options = args.delete(args.last) if args.last.is_a?(Hash)
|
55
|
+
options || {}
|
56
|
+
end
|
57
|
+
|
58
|
+
def ie_assets(asset_type, args)
|
59
|
+
ie_assets = ''
|
60
|
+
%w{ 6 7 8 }.each do |v|
|
61
|
+
assets = @_asset_manager.required_with_extra(asset_type, "ie#{v}")
|
62
|
+
|
63
|
+
unless assets.blank?
|
64
|
+
assets = case asset_type
|
65
|
+
when :css : stylesheet_link_tag(assets)
|
66
|
+
when :js : javascript_include_tag(assets)
|
67
|
+
end
|
68
|
+
|
69
|
+
ie_assets += conditional_comment(assets.to_s, v)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
ie_assets
|
73
|
+
end
|
74
|
+
|
75
|
+
def include_assets(asset_type, args, options)
|
76
|
+
@_asset_manager.append(asset_type, args) unless args.empty?
|
77
|
+
@_asset_manager.auto_add(asset_type, params) if options[:auto]
|
78
|
+
@_asset_manager.required(asset_type)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
ActionView::Base.class_eval { include SmartAssetHelper }
|
84
|
+
|
85
|
+
ActionController::Base.class_eval do
|
86
|
+
before_filter :create_asset_manager_instance
|
87
|
+
def create_asset_manager_instance
|
88
|
+
@_asset_manager = AssetManager.new
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SmarterAssetTagHelper
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.send :include, InstanceMethods
|
5
|
+
base.send :alias_method_chain, :javascript_include_tag, :query_strings
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
|
10
|
+
# This is to cope with Rails appending '.js' to things like Google maps API and other things
|
11
|
+
# that have query strings
|
12
|
+
def javascript_include_tag_with_query_strings(*sources)
|
13
|
+
query_strings_sources = sources.flatten.select { |s| s =~ /[\?]/ }
|
14
|
+
sources = sources.flatten - query_strings_sources
|
15
|
+
|
16
|
+
script_tag(query_strings_sources) + javascript_include_tag_without_query_strings(*sources)
|
17
|
+
end
|
18
|
+
|
19
|
+
def script_tag(*sources)
|
20
|
+
sources.map do |s|
|
21
|
+
content_tag(:script, '', :src => s, :type => 'text/javascript')
|
22
|
+
end.join
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
ActionView::Base.class_eval { include SmarterAssetTagHelper }
|
data/rails/init.rb
CHANGED
@@ -1,14 +1 @@
|
|
1
|
-
require '
|
2
|
-
require 'action_view/helpers/smarter_asset_tag_helper'
|
3
|
-
|
4
|
-
ActionView::Base.class_eval do
|
5
|
-
include ActionView::Helpers::SmartAssetHelper
|
6
|
-
include ActionView::Helpers::SmarterAssetTagHelper
|
7
|
-
end
|
8
|
-
|
9
|
-
ActionController::Base.class_eval do
|
10
|
-
before_filter :create_asset_manager_instance
|
11
|
-
def create_asset_manager_instance
|
12
|
-
@_asset_manager = AssetManager.new
|
13
|
-
end
|
14
|
-
end
|
1
|
+
require 'smart_asset_helper'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jeremyboles-smart-asset-helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Boles
|
@@ -38,9 +38,9 @@ files:
|
|
38
38
|
- README.rdoc
|
39
39
|
- Rakefile
|
40
40
|
- init.rb
|
41
|
-
- lib/action_view/helpers/smart_asset_helper.rb
|
42
|
-
- lib/action_view/helpers/smarter_asset_tag_helper.rb
|
43
41
|
- lib/asset_manager.rb
|
42
|
+
- lib/smart_asset_helper.rb
|
43
|
+
- lib/smarter_asset_tag_helper.rb
|
44
44
|
- rails/init.rb
|
45
45
|
- test/small_asset_helper_test.rb
|
46
46
|
- test/test_helper.rb
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'asset_manager'
|
2
|
-
|
3
|
-
module ActionView
|
4
|
-
module Helpers
|
5
|
-
module SmartAssetHelper
|
6
|
-
|
7
|
-
# Prevent a javscript file from being required
|
8
|
-
def deny_javascript(*javascripts)
|
9
|
-
@_asset_manager.deny(:js, javascripts)
|
10
|
-
end
|
11
|
-
|
12
|
-
# Prevent a stylesheet file from being required
|
13
|
-
def deny_stylesheet(*stylesheets)
|
14
|
-
@_asset_manager.deny(:css, stylesheets)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Print required javascript
|
18
|
-
def include_javascript(*args)
|
19
|
-
options = extract_options(args)
|
20
|
-
javascript = javascript_include_tag(*include_assets(:js, args, options))
|
21
|
-
javascript += ie_assets(:js, args) if options[:ie]
|
22
|
-
javascript.gsub("\n", '')
|
23
|
-
end
|
24
|
-
|
25
|
-
# Print required stylesheets
|
26
|
-
def include_stylesheets(*args)
|
27
|
-
options = extract_options(args)
|
28
|
-
stylesheets = stylesheet_link_tag(*include_assets(:css, args, options))
|
29
|
-
stylesheets += ie_assets(:css, args) if options[:ie]
|
30
|
-
stylesheets.gsub("\n", '')
|
31
|
-
end
|
32
|
-
|
33
|
-
# Require bot Javscript and CSS
|
34
|
-
def require_assets(*assets)
|
35
|
-
require_stylesheet(*assets)
|
36
|
-
require_javascript(*assets)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Request a javascript to be outputed by include_javascript
|
40
|
-
def require_javascript(*javascripts)
|
41
|
-
@_asset_manager.add(:js, javascripts)
|
42
|
-
end
|
43
|
-
|
44
|
-
# Request a stylesheet to be outputed by include_stylesheets
|
45
|
-
def require_stylesheet(*stylesheets)
|
46
|
-
@_asset_manager.add(:css, stylesheets)
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def conditional_comment(text, version)
|
52
|
-
"<!--[if IE #{version}]>#{text}<![endif]-->"
|
53
|
-
end
|
54
|
-
|
55
|
-
def extract_options(args)
|
56
|
-
options = args.delete(args.last) if args.last.is_a?(Hash)
|
57
|
-
options || {}
|
58
|
-
end
|
59
|
-
|
60
|
-
def ie_assets(asset_type, args)
|
61
|
-
ie_assets = ''
|
62
|
-
%w{ 6 7 8 }.each do |v|
|
63
|
-
assets = @_asset_manager.required_with_extra(asset_type, "ie#{v}")
|
64
|
-
|
65
|
-
unless assets.blank?
|
66
|
-
assets = case asset_type
|
67
|
-
when :css : stylesheet_link_tag(assets)
|
68
|
-
when :js : javascript_include_tag(assets)
|
69
|
-
end
|
70
|
-
|
71
|
-
ie_assets += conditional_comment(assets.to_s, v)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
ie_assets
|
75
|
-
end
|
76
|
-
|
77
|
-
def include_assets(asset_type, args, options)
|
78
|
-
@_asset_manager.append(asset_type, args) unless args.empty?
|
79
|
-
@_asset_manager.auto_add(asset_type, params) if options[:auto]
|
80
|
-
@_asset_manager.required(asset_type)
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module ActionView
|
2
|
-
module Helpers
|
3
|
-
module SmarterAssetTagHelper
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.send :include, InstanceMethods
|
7
|
-
base.send :alias_method_chain, :javascript_include_tag, :query_strings
|
8
|
-
end
|
9
|
-
|
10
|
-
module InstanceMethods
|
11
|
-
|
12
|
-
# This is to cope with Rails appending '.js' to things like Google maps API and other things
|
13
|
-
# that have query strings
|
14
|
-
def javascript_include_tag_with_query_strings(*sources)
|
15
|
-
query_strings_sources = sources.flatten.select { |s| s =~ /[\?]/ }
|
16
|
-
sources = sources.flatten - query_strings_sources
|
17
|
-
|
18
|
-
script_tag(query_strings_sources) + javascript_include_tag_without_query_strings(*sources)
|
19
|
-
end
|
20
|
-
|
21
|
-
def script_tag(*sources)
|
22
|
-
sources.map do |s|
|
23
|
-
content_tag(:script, '', :src => s, :type => 'text/javascript')
|
24
|
-
end.join
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|