jeremyboles-smart-asset-helper 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +31 -0
- data/Rakefile +40 -0
- data/init.rb +14 -0
- data/lib/action_view/helpers/smart_asset_helper.rb +85 -0
- data/lib/action_view/helpers/smarter_asset_tag_helper.rb +30 -0
- data/lib/asset_manager.rb +87 -0
- data/test/small_asset_helper_test.rb +5 -0
- data/test/test_helper.rb +3 -0
- data/uninstall.rb +1 -0
- metadata +78 -0
data/CHANGELOG.rdoc
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jeremy Boles
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= SmartAssetHelper
|
2
|
+
|
3
|
+
SmartAssetHelper is a utility that add a number of conveniences for dealing with CSS and Javascript files. The library sprung out of a distain for how "dirty" my CSS and Javascript organization would get over time. SmartAssetHelper introduces an opinionated way to organize CSS and Javascript files and also contains a number of convenient helper methods to make your life easier.
|
4
|
+
|
5
|
+
|
6
|
+
== Example
|
7
|
+
|
8
|
+
SmartAssetHelper introduces two new methods for including CSS and Javascript within your <head> tag:
|
9
|
+
|
10
|
+
# app/views/layouts/application.html.erb
|
11
|
+
<html>
|
12
|
+
<head>
|
13
|
+
<%= include_stylesheets 'global' %>
|
14
|
+
<%= include_javascript :default %>
|
15
|
+
</head>
|
16
|
+
<body>
|
17
|
+
<%= yield %>
|
18
|
+
</body>
|
19
|
+
</html>
|
20
|
+
|
21
|
+
The <code>include_stylesheets</code> and <code>include_javascript</code> helper methods will include asset files that have been 'required' by helper methods:
|
22
|
+
|
23
|
+
# app/views/users/new.html.erb
|
24
|
+
<% require_stylesheet 'form %>
|
25
|
+
<% require_javascript 'validations' %>
|
26
|
+
...
|
27
|
+
|
28
|
+
When the <code>include_stylesheets</code> and <code>include_javascript</code> methods are passed the <code>:auto => true</code> parameter, they will automatically include assets based on the request parameters (if the files exists). For example, a request to <code>UsersController#index</code> will automatically include stylesheets at <code>/stylesheets/users.css</code> and <code>/stylesheets/users/index.css</code>, if they exsist.
|
29
|
+
|
30
|
+
|
31
|
+
Copyright (c) 2009 (Jeremy Boles)[http://jeremyboles.com/], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the small_asset_helper plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the small_asset_helper plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'SmallAssetHelper'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
desc %{Update ".manifest" with the latest list of project filenames.}
|
26
|
+
task :manifest do
|
27
|
+
list = `git ls-files --full-name --exclude=*.gemspec --exclude=.*`.chomp.split("\n")
|
28
|
+
|
29
|
+
if spec_file = Dir['*.gemspec'].first
|
30
|
+
spec = File.read spec_file
|
31
|
+
spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
|
32
|
+
assignment = $1
|
33
|
+
bunch = $2 ? list.grep(/^test\//) : list
|
34
|
+
'%s%%w(%s)' % [assignment, bunch.join(' ')]
|
35
|
+
end
|
36
|
+
|
37
|
+
File.open(spec_file, 'w') { |f| f << spec }
|
38
|
+
end
|
39
|
+
File.open('.manifest', 'w') { |f| f << list.join("\n") }
|
40
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'action_view/helpers/smart_asset_helper'
|
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
|
@@ -0,0 +1,85 @@
|
|
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
|
@@ -0,0 +1,30 @@
|
|
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
|
@@ -0,0 +1,87 @@
|
|
1
|
+
class AssetManager
|
2
|
+
|
3
|
+
ASSET_DIR = {
|
4
|
+
:js => ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR,
|
5
|
+
:css => ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR
|
6
|
+
}
|
7
|
+
|
8
|
+
SHORTCUTS = {
|
9
|
+
:css => {},
|
10
|
+
:js => { :defaults => ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES }
|
11
|
+
}
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@denied = { :css => [], :js => [] }
|
15
|
+
@required = { :css => [], :js => [] }
|
16
|
+
end
|
17
|
+
|
18
|
+
# Add assets to the list of required assets
|
19
|
+
def add(asset_type, *assets)
|
20
|
+
@required[asset_type] << assets
|
21
|
+
end
|
22
|
+
|
23
|
+
# Append assets to the front of the list of assets
|
24
|
+
def append(asset_type, *assets)
|
25
|
+
@required[asset_type] = assets + @required[asset_type]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Automaticly add assets based the controller/action params, if they exsist
|
29
|
+
def auto_add(asset_type, request_params)
|
30
|
+
files, prefix = [], ''
|
31
|
+
(request_params[:controller] + '/' + request_params[:action]).split('/').each do |path|
|
32
|
+
path = prefix + path
|
33
|
+
asset_path = "#{path}.#{asset_type}"
|
34
|
+
file_path = "#{ASSET_DIR[asset_type]}/#{asset_path}"
|
35
|
+
|
36
|
+
files << path if File.exists?(file_path)
|
37
|
+
prefix = path + '/'
|
38
|
+
end
|
39
|
+
self.add(asset_type, files)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Prevent an asset from being required
|
43
|
+
def deny(asset_type, *assets)
|
44
|
+
@denied[asset_type] << assets
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns a list of assets that have been required
|
48
|
+
def required(asset_type)
|
49
|
+
files = extract(asset_type)
|
50
|
+
files = process_shortcuts(asset_type, files)
|
51
|
+
files
|
52
|
+
end
|
53
|
+
|
54
|
+
def required_with_extra(asset_type, extra)
|
55
|
+
files = []
|
56
|
+
self.required(asset_type).each do |file|
|
57
|
+
path = "#{file}-#{extra}"
|
58
|
+
asset_path = "#{path}.#{asset_type}"
|
59
|
+
file_path = "#{ASSET_DIR[asset_type]}/#{asset_path}"
|
60
|
+
files << path if File.exists?(file_path)
|
61
|
+
end
|
62
|
+
files
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# Extracts the required files, unique and honoring denied assets
|
68
|
+
def extract(asset_type)
|
69
|
+
seen_files = @denied[asset_type].flatten
|
70
|
+
|
71
|
+
required_files = @required[asset_type].flatten.inject([]) do |required, files|
|
72
|
+
files_to_include = files.to_a - seen_files
|
73
|
+
seen_files += files_to_include
|
74
|
+
required << files_to_include unless files_to_include.empty?
|
75
|
+
required
|
76
|
+
end.flatten
|
77
|
+
end
|
78
|
+
|
79
|
+
# Processes the shortcuts
|
80
|
+
def process_shortcuts(asset_type, files)
|
81
|
+
files.flatten.map do |file|
|
82
|
+
file = SHORTCUTS[asset_type][file] if SHORTCUTS[asset_type].has_key?(file)
|
83
|
+
file
|
84
|
+
end.flatten
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jeremyboles-smart-asset-helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Boles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-19 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
description: SmartAssetHelper helps ease some of the pain with dealing and organizing stylesheet and javascript files.
|
26
|
+
email: jeremy@jeremyboles.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- MIT-LICENSE
|
34
|
+
- CHANGELOG.rdoc
|
35
|
+
files:
|
36
|
+
- CHANGELOG.rdoc
|
37
|
+
- MIT-LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- init.rb
|
41
|
+
- lib/action_view/helpers/smart_asset_helper.rb
|
42
|
+
- lib/action_view/helpers/smarter_asset_tag_helper.rb
|
43
|
+
- lib/asset_manager.rb
|
44
|
+
- test/small_asset_helper_test.rb
|
45
|
+
- test/test_helper.rb
|
46
|
+
- uninstall.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/jeremyboles/smart-asset-helper/wikis
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --main
|
52
|
+
- README.rdoc
|
53
|
+
- --inline-source
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.2.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: A simple Rails plugin for dealing with stylesheet and javascript management and organization.
|
76
|
+
test_files:
|
77
|
+
- test/small_asset_helper_test.rb
|
78
|
+
- test/test_helper.rb
|