tension 0.9.3 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tension/asset_group.rb +82 -0
- data/lib/tension/controller.rb +18 -0
- data/lib/tension/environment.rb +18 -115
- data/lib/tension/helper.rb +37 -0
- data/lib/tension/railtie.rb +7 -4
- data/lib/tension/version.rb +1 -1
- data/lib/tension.rb +5 -4
- metadata +4 -2
- data/lib/tension/tension_helper.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76c048b5004ade369c07e87cf54a73b3459f7151
|
4
|
+
data.tar.gz: e2494f1c0198761e2464ec130b2107ba05aa1ae4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 599a97dd6849f6a070d7d2cb1ac3a6c7da3e959a4046b9c6558995663fd6afe5a2d6d98138cd2aef8e89ce529ffec28c1ccf8bae42aa35e9da93d9bbd8ad4861
|
7
|
+
data.tar.gz: 4902abbd406722db8bd6c680630ce1285bbc6f7613dd20aeba5e12e01f4349c0fab1083394ce8839a2c4b4c0d8e422550b7c37635a702383035ea86b9954effd
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Tension
|
2
|
+
class AssetGroup
|
3
|
+
|
4
|
+
class << self
|
5
|
+
# Builds a valid routing Path.
|
6
|
+
def path_for(key, action = nil)
|
7
|
+
if key.match( path_regex )
|
8
|
+
return key
|
9
|
+
|
10
|
+
elsif key.include?("#")
|
11
|
+
key, action = key.split("#")
|
12
|
+
end
|
13
|
+
|
14
|
+
unless key.try(:match, controller_regex) && action.try(:match, action_regex)
|
15
|
+
raise ArgumentError, "[Tension] Couldn't build valid controller path!"
|
16
|
+
end
|
17
|
+
|
18
|
+
"#{ key }##{ action }"
|
19
|
+
end
|
20
|
+
|
21
|
+
# Matches strings like "blog/comments#show".
|
22
|
+
def path_regex
|
23
|
+
/([\w\/]+[^#])#(\w+)/
|
24
|
+
end
|
25
|
+
|
26
|
+
# Matches strings like "blog/comments".
|
27
|
+
def controller_regex
|
28
|
+
/([\w\/]+[^#])/
|
29
|
+
end
|
30
|
+
|
31
|
+
# Matches strings like "show".
|
32
|
+
def action_regex
|
33
|
+
/(\w+)/
|
34
|
+
end
|
35
|
+
|
36
|
+
def global_asset(type)
|
37
|
+
assets[ "application.#{type}" ]
|
38
|
+
end
|
39
|
+
|
40
|
+
def assets
|
41
|
+
Rails.application.assets
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
attr_reader :controller, :action
|
47
|
+
|
48
|
+
def initialize(key, action = nil)
|
49
|
+
@controller, @action = self.class.path_for( key, action ).split("#")
|
50
|
+
end
|
51
|
+
|
52
|
+
def css
|
53
|
+
best_asset( Tension::CSS )
|
54
|
+
end
|
55
|
+
|
56
|
+
def js
|
57
|
+
best_asset( Tension::JS )
|
58
|
+
end
|
59
|
+
|
60
|
+
def action_asset(type)
|
61
|
+
self.class.assets[ "#{controller}/#{action}.#{type}" ]
|
62
|
+
end
|
63
|
+
|
64
|
+
def controller_asset(type)
|
65
|
+
self.class.assets[ "#{controller}_common.#{type}" ]
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def best_asset(type)
|
71
|
+
action_asset(type) || controller_asset(type) || self.class.global_asset(type)
|
72
|
+
end
|
73
|
+
|
74
|
+
def method_missing(method_sym, *args)
|
75
|
+
degree, type = method_sym.to_s.split("_")
|
76
|
+
asset_method = "#{degree}_asset"
|
77
|
+
|
78
|
+
respond_to?(asset_method) ? send(asset_method, type) : super
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Tension
|
4
|
+
module Controller
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def action_assets
|
8
|
+
controller = request.symbolized_path_parameters[:controller]
|
9
|
+
action = request.symbolized_path_parameters[:action]
|
10
|
+
|
11
|
+
Tension::Environment.find( controller, action )
|
12
|
+
end
|
13
|
+
|
14
|
+
def assets
|
15
|
+
Tension::Environment
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/tension/environment.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Tension
|
2
2
|
|
3
|
-
# Tension::Environment exposes `
|
3
|
+
# Tension::Environment exposes `assets`, which describes the assets that
|
4
4
|
# can be automatically included in templates.
|
5
5
|
#
|
6
6
|
# It's used automatically on application load (see Tension::Railtie) to populate
|
@@ -11,144 +11,47 @@ module Tension
|
|
11
11
|
|
12
12
|
class << self
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
# This method collects all asset paths for the asset pipeline to
|
20
|
-
# precompile. It determines which additional assets to include in the
|
21
|
-
# pipeline's precompilation process, so they can be automatically added
|
22
|
-
# to templates later (see Tension::Tagger).
|
23
|
-
#
|
24
|
-
# If an application's routes include `resources :people`, `asset_map`
|
25
|
-
# will attempt to locate the following assets:
|
26
|
-
#
|
27
|
-
# + people_common.{js,css}
|
28
|
-
# + people/index.{js,css}
|
29
|
-
# + people/show.{js,css}
|
30
|
-
# + people/new.{js,css}
|
31
|
-
# + people/edit.{js,css}
|
32
|
-
#
|
33
|
-
# Note that Tension ignores Sprockets' interpretation of `index.{js,css}`
|
34
|
-
# as a shared file, and therefore requires the `_common` suffix for
|
35
|
-
# controller-wide assets.
|
36
|
-
#
|
37
|
-
# Returns: an Hash of controller paths, each containing that controller's
|
38
|
-
# routed GET actions mapped to each action's best matching asset. E.g.
|
39
|
-
#
|
40
|
-
# { "people" => {
|
41
|
-
# "index" => {
|
42
|
-
# "js" => "javascripts/people/index.js",
|
43
|
-
# "css" => "javascripts/people_common.css"
|
44
|
-
# },
|
45
|
-
# "show" => {
|
46
|
-
# "js" => "javascripts/application.js",
|
47
|
-
# "css" => "javascripts/people_common.css"
|
48
|
-
# }
|
49
|
-
# }
|
50
|
-
#
|
51
|
-
def asset_map
|
52
|
-
@asset_map = nil if Rails.env.development?
|
53
|
-
|
54
|
-
@asset_map ||= begin
|
55
|
-
map = Hash.new
|
56
|
-
globals = Hash.new
|
57
|
-
|
58
|
-
ASSET_TYPES.each do |type|
|
59
|
-
# Find and store the global asset for this type.
|
60
|
-
global_asset = valid_asset( "application.#{ type[:extension] }" )
|
61
|
-
globals.store( type[:extension], global_asset )
|
62
|
-
end
|
63
|
-
|
64
|
-
# TODO: add support for looking up the tree...
|
65
|
-
search_paths.each_pair do |controller_path, actions|
|
66
|
-
map.store( controller_path, Hash.new )
|
67
|
-
|
68
|
-
ASSET_TYPES.each do |type|
|
69
|
-
# Attempt to locate a common asset for this controller.
|
70
|
-
common_path = "#{ controller_path }#{ COMMON_SUFFIX }.#{ type[:extension] }"
|
71
|
-
common_asset = valid_asset( common_path ) || globals.fetch( type[:extension] )
|
72
|
-
|
73
|
-
actions.each do |action|
|
74
|
-
unless map[ controller_path ].has_key?( action )
|
75
|
-
map.fetch( controller_path ).store( action, Hash.new )
|
76
|
-
end
|
77
|
-
|
78
|
-
action_asset = valid_asset( "#{ [ controller_path, action ].join("/") }.#{ type[:extension] }" )
|
79
|
-
map.fetch( controller_path )
|
80
|
-
.fetch( action )
|
81
|
-
.store( type[:extension], action_asset || common_asset )
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
14
|
+
def [](key, action = nil)
|
15
|
+
fetch( AssetGroup.path_for(key, action) )
|
16
|
+
end
|
17
|
+
alias_method :find, :[]
|
86
18
|
|
87
|
-
|
19
|
+
def eager_load!
|
20
|
+
configured_get_defaults.each do |default|
|
21
|
+
find( default[:controller], default[:action] )
|
88
22
|
end
|
89
23
|
end
|
90
24
|
|
91
|
-
|
92
|
-
|
93
|
-
@asset_paths ||= Set.new.merge( extract_paths(asset_map) ).to_a
|
25
|
+
def assets
|
26
|
+
@assets ||= Hash.new
|
94
27
|
end
|
95
28
|
|
96
|
-
|
97
29
|
private
|
98
30
|
|
99
|
-
|
100
|
-
|
101
|
-
paths = Array.new
|
102
|
-
|
103
|
-
hash.each_pair do |key, value|
|
104
|
-
if value.is_a?(Hash)
|
105
|
-
paths.concat( extract_paths(value) )
|
106
|
-
else
|
107
|
-
paths << value.pathname.to_s
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
paths
|
31
|
+
def fetch(path)
|
32
|
+
assets[path] || store(path)
|
112
33
|
end
|
113
34
|
|
114
|
-
|
115
|
-
|
116
|
-
#
|
117
|
-
# e.g. { "blog" => [ "index", "show" ],
|
118
|
-
# "admin/blog" => [ "index", "show", "edit" ] }
|
119
|
-
#
|
120
|
-
def search_paths
|
121
|
-
@search_paths ||= configured_route_defaults.reduce( Hash.new ) do |accum, route_default|
|
122
|
-
accum[ route_default[:controller] ] ||= Array.new
|
123
|
-
accum[ route_default[:controller] ].push( route_default[:action] )
|
124
|
-
|
125
|
-
accum
|
126
|
-
end
|
35
|
+
def store(path)
|
36
|
+
assets[path] = AssetGroup.new( path )
|
127
37
|
end
|
128
38
|
|
129
39
|
# Routing defaults (including controller path and action name) for all
|
130
40
|
# configured GET routes.
|
131
41
|
#
|
132
|
-
def
|
133
|
-
@
|
42
|
+
def configured_get_defaults
|
43
|
+
@configured_get_defaults ||= begin
|
134
44
|
get_routes = Rails.application.routes.routes.find_all do |route|
|
135
45
|
route.verb.match("GET")
|
136
46
|
end
|
137
47
|
|
138
|
-
@
|
48
|
+
@configured_get_defaults = get_routes.map do |route|
|
139
49
|
route.defaults unless route.defaults.empty?
|
140
50
|
end
|
141
51
|
|
142
|
-
@
|
52
|
+
@configured_get_defaults.compact!
|
143
53
|
end
|
144
54
|
end
|
145
|
-
|
146
|
-
# Returns: a real BundledAsset present in the Sprockets index, or nil
|
147
|
-
# if no asset was found.
|
148
|
-
#
|
149
|
-
def valid_asset asset_path
|
150
|
-
Rails.application.assets.find_asset( asset_path )
|
151
|
-
end
|
152
55
|
end
|
153
56
|
|
154
57
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Tension
|
4
|
+
|
5
|
+
# Helper is included in ActionView::Helpers so it can be called from
|
6
|
+
# templates and layouts.
|
7
|
+
module Helper
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
def best_stylesheet(*args)
|
11
|
+
asset_for( Tension::CSS, *args )
|
12
|
+
end
|
13
|
+
|
14
|
+
def best_javascript(*args)
|
15
|
+
asset_for( Tension::JS, *args )
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def asset_for(type, *args)
|
21
|
+
controller = request.params[:controller]
|
22
|
+
action = request.params[:action]
|
23
|
+
|
24
|
+
asset = Tension::Environment.find( controller, action ).send( type )
|
25
|
+
|
26
|
+
include_method = case type.to_s
|
27
|
+
when "js"
|
28
|
+
:javascript_include_tag
|
29
|
+
when "css"
|
30
|
+
:stylesheet_link_tag
|
31
|
+
end
|
32
|
+
|
33
|
+
send( include_method, asset.logical_path, *args )
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/tension/railtie.rb
CHANGED
@@ -7,12 +7,15 @@ module Tension
|
|
7
7
|
initializer "tension.add_assets_to_precompile_list" do |app|
|
8
8
|
ActiveSupport.on_load :after_initialize do
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
if app.config.cache_classes
|
11
|
+
app.reload_routes!
|
12
|
+
Tension::Environment.eager_load!
|
13
|
+
end
|
12
14
|
|
13
|
-
ActionView::Base.send(:include, Tension::
|
15
|
+
ActionView::Base.send(:include, Tension::Helper)
|
16
|
+
ActionController::Base.send(:include, Tension::Controller)
|
14
17
|
|
15
|
-
|
18
|
+
app.config.assets.precompile << lambda do |path, filename|
|
16
19
|
Tension::Environment.asset_paths.include?( filename )
|
17
20
|
end
|
18
21
|
|
data/lib/tension/version.rb
CHANGED
data/lib/tension.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
+
require 'tension/asset_group'
|
2
|
+
require 'tension/controller'
|
3
|
+
require 'tension/helper'
|
1
4
|
require 'tension/environment'
|
2
5
|
require 'tension/railtie' if defined?(Rails)
|
3
|
-
require 'tension/tension_helper'
|
4
6
|
|
5
7
|
module Tension
|
6
|
-
|
7
|
-
|
8
|
-
end
|
8
|
+
CSS = "css".freeze
|
9
|
+
JS = "js".freeze
|
9
10
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piers Mainwaring
|
@@ -34,9 +34,11 @@ files:
|
|
34
34
|
- LICENSE
|
35
35
|
- README.md
|
36
36
|
- lib/tension.rb
|
37
|
+
- lib/tension/asset_group.rb
|
38
|
+
- lib/tension/controller.rb
|
37
39
|
- lib/tension/environment.rb
|
40
|
+
- lib/tension/helper.rb
|
38
41
|
- lib/tension/railtie.rb
|
39
|
-
- lib/tension/tension_helper.rb
|
40
42
|
- lib/tension/version.rb
|
41
43
|
- tension.gemspec
|
42
44
|
homepage: https://github.com/piersadrian/tension
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
|
-
module Tension
|
4
|
-
|
5
|
-
# Tagger is included in ActionView::Helpers so it can be called from
|
6
|
-
# templates and layouts.
|
7
|
-
module TensionHelper
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
def asset_for type, *args
|
11
|
-
controller = request.params[:controller]
|
12
|
-
action = request.params[:action]
|
13
|
-
|
14
|
-
asset = Tension::Environment.asset_map[ controller ][ action ][ type.to_s ]
|
15
|
-
|
16
|
-
include_method = case type
|
17
|
-
when :js
|
18
|
-
:javascript_include_tag
|
19
|
-
when :css
|
20
|
-
:stylesheet_link_tag
|
21
|
-
end
|
22
|
-
|
23
|
-
send( include_method, asset.logical_path, *args ) unless asset.nil?
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|