tension 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2013 Piers Mainwaring
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ Tension
2
+ =======
3
+ _Tighten up Rails's asset pipeline for CSS & JS._
4
+
5
+ Rails's asset pipeline is smart and well–implemented under the hood, but it can
6
+ be a challenge to organize your CSS and JavaScript so that
@@ -0,0 +1,62 @@
1
+ module Tension
2
+ class Application
3
+
4
+ class << self
5
+
6
+ # This method collects all asset paths for the asset pipeline to
7
+ # precompile. It's called from `config/application.rb` when determining
8
+ # which additional assets to include in the pipeline's precompilation
9
+ # process. Tension will assume that any subdirectories in your assets
10
+ # directory are module scopes. You can also explicitly set module scopes:
11
+ #
12
+ # Rails.application.config.tension_modules = %W( blog account )
13
+ #
14
+ # Tension::Application will then search for all javascripts and stylesheets
15
+ # one filesystem level deep in those scopes. The search paths become:
16
+ #
17
+ # app/assets/{javascripts,stylesheets}/*.{js,css}
18
+ # app/assets/{javascripts,stylesheets}/{blog,account}/**/*.{js,css}
19
+ #
20
+ # Any assets in these paths will be added to the pipeline and compiled.
21
+
22
+ def collect_assets
23
+ assets = %W(stylesheets javascripts).map do |type|
24
+ glob_within_asset_path( type ) + module_scopes.map do |scope|
25
+ glob_within_asset_path( type, scope )
26
+ end
27
+ end
28
+
29
+ assets.flatten
30
+ end
31
+
32
+
33
+ private
34
+
35
+ def module_scopes
36
+ # find dirs in app/assets
37
+ end
38
+
39
+ # Loads the file paths within a given subdirectory of "app/assets/".
40
+ def glob_within_asset_path type, *path_parts
41
+ # Only recursively find children if a particular subdirectory of an
42
+ # asset `type` is given. That way we can specify WHICH subdirectories
43
+ # of a `type` have assets that matter.
44
+ path_parts << "**" if path_parts.present?
45
+
46
+ root_path = File.join(Rails.root, "app", "assets", type)
47
+ pattern = File.expand_path( File.join(*path_parts, "*.*"), root_path )
48
+
49
+ paths = Dir.glob( pattern ).map do |file_path|
50
+ # Remove extra, pre-compilation file extensions and any part of the
51
+ # filepath at or above the asset `type`.
52
+ file_path.gsub( file_path.match(/(\.css|\.js)(.*)/)[2], '' )
53
+ .gsub( root_path + "/", '' ) rescue nil
54
+ end
55
+
56
+ paths.compact
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,17 @@
1
+ require "tension"
2
+
3
+ module Tension
4
+ require "rails"
5
+
6
+ class Railtie < Rails::Railtie
7
+
8
+ initializer "tension.asset_pipeline" do |app|
9
+ ActiveSupport.on_load :rails do
10
+ if !Rails.env.development? && !Rails.env.test?
11
+ Rails.application.config.assets.precompile += Tension::Collector.collect_assets
12
+ end
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,82 @@
1
+ module Pipeline
2
+ class Builder
3
+
4
+ GLOBAL_ASSET_NAME = "application".freeze
5
+ SHARED_SUFFIX = "_common".freeze
6
+ ASSET_SPECIFICITY_ORDER = [ :action, :controller, :module ].freeze
7
+
8
+ # Determines which JS/CSS files should be included based on the current
9
+ # controller, action, and which files actually exist. If the current
10
+ # controller is "OrcaHealth::PagesController" and action is "products",
11
+ # this method (called with `type = :js`) will attempt to include
12
+ #
13
+ # + orca_health/pages/products.js
14
+ # + orca_health/pages_common.js
15
+ # + orca_health.js
16
+ #
17
+ # in that order. ONLY THE FIRST ASSET LOCATED WILL BE INCLUDED. Hence,
18
+ # you should always use Sprockets directives to include dependencies.
19
+ #
20
+ # Any files that don't exist will not be included in the page. Pass
21
+ # `{ except: :application }` to exclude `application.{js,css}`.
22
+
23
+ def self.asset_paths type, controller_path, action_name, options = {}
24
+ options[:except] = [ options[:except] ] unless options[:except].is_a? Array
25
+
26
+ # Check if the best asset's already been loaded and stored.
27
+ asset = known_asset_paths[ controller_path ].try(:fetch, action_name, nil).try(:fetch, type, nil)
28
+
29
+ if asset.nil?
30
+ path_components = controller_path.split("/")
31
+ controller_name = path_components.pop
32
+ module_path = path_components.join("/")
33
+
34
+ possible_paths = {
35
+ module: module_path,
36
+ controller: [ module_path, controller_name + SHARED_SUFFIX ].join("/"),
37
+ action: [ module_path, controller_name, action_name ].join("/")
38
+ }
39
+
40
+ # Find and store the best possible asset for this controller/action combo.
41
+ asset = most_specific_asset_path( type, possible_paths )
42
+
43
+ known_asset_paths[ controller_path ] ||= Hash.new
44
+ known_asset_paths[ controller_path ][ action_name ] ||= Hash.new
45
+ known_asset_paths[ controller_path ][ action_name ][ type ] = asset
46
+ end
47
+
48
+ assets = if options[:except].include? :application
49
+ [ asset ]
50
+ else
51
+ [ GLOBAL_ASSET_NAME, asset ]
52
+ end
53
+
54
+ assets.compact
55
+ end
56
+
57
+
58
+ private
59
+
60
+ def self.known_asset_paths
61
+ @known_asset_paths ||= Hash.new
62
+ end
63
+
64
+ def self.most_specific_asset_path filetype, paths
65
+ ASSET_SPECIFICITY_ORDER.each do |name|
66
+ path = paths[name]
67
+
68
+ # Using the Sprockets `#index` helps performance in production.
69
+ asset = if Rails.env.production?
70
+ Rails.application.assets.index.find_asset([ path, filetype ].join("."))
71
+ else
72
+ Rails.application.assets.find_asset([ path, filetype ].join("."))
73
+ end
74
+
75
+ # This loop is relatively expensive, so return the asset path as soon
76
+ # as possible rather than continue looping.
77
+ break path if asset.present?
78
+ end
79
+ end
80
+
81
+ end
82
+ end
data/lib/tension.rb ADDED
File without changes
data/tension.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "tension"
3
+
4
+ s.version = "0.3"
5
+ s.date = "2013-02-22"
6
+
7
+ s.summary = "Tighten up Rails's asset pipeline for CSS & JS."
8
+ s.description = "Tension brings some sanity to CSS & JS organization for modern front–end development."
9
+
10
+ s.authors = [ "Piers Mainwaring" ]
11
+ s.email = "piers@impossibly.org"
12
+ s.files = `git ls-files`.split("\n")
13
+ s.homepage = "https://github.com/piersadrian/tension"
14
+ s.require_paths = [ "lib" ]
15
+
16
+ s.add_dependency "activerecord", "~> 3.2.0"
17
+ s.add_dependency "activesupport", "~> 3.2.0"
18
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tension
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piers Mainwaring
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ prerelease: false
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ none: false
23
+ type: :runtime
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 3.2.0
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ prerelease: false
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.0
38
+ none: false
39
+ type: :runtime
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 3.2.0
45
+ none: false
46
+ description: Tension brings some sanity to CSS & JS organization for modern front–end
47
+ development.
48
+ email: piers@impossibly.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - LICENSE
54
+ - README.md
55
+ - lib/tension.rb
56
+ - lib/tension/application.rb
57
+ - lib/tension/railtie.rb
58
+ - lib/tension/tagger.rb
59
+ - tension.gemspec
60
+ homepage: https://github.com/piersadrian/tension
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ none: false
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.24
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Tighten up Rails's asset pipeline for CSS & JS.
84
+ test_files: []