active_assets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/.autotest +55 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +4 -0
  4. data/Rakefile +12 -0
  5. data/active_assets.gemspec +22 -0
  6. data/lib/active_assets/active_expansions/asset.rb +63 -0
  7. data/lib/active_assets/active_expansions/asset_scope.rb +29 -0
  8. data/lib/active_assets/active_expansions/assets.rb +97 -0
  9. data/lib/active_assets/active_expansions/configurable.rb +14 -0
  10. data/lib/active_assets/active_expansions/expansion.rb +65 -0
  11. data/lib/active_assets/active_expansions/expansions.rb +103 -0
  12. data/lib/active_assets/active_expansions/javascripts.rb +9 -0
  13. data/lib/active_assets/active_expansions/railtie.rb +52 -0
  14. data/lib/active_assets/active_expansions/stylesheets.rb +9 -0
  15. data/lib/active_assets/active_expansions/type_inferrable.rb +12 -0
  16. data/lib/active_assets/active_expansions.rb +17 -0
  17. data/lib/active_assets.rb +7 -0
  18. data/lib/rails/active_assets.rb +11 -0
  19. data/lib/tasks/cache.rake +6 -0
  20. data/test/active_assets/active_expansions/asset_test.rb +40 -0
  21. data/test/active_assets/active_expansions/expansions_test.rb +148 -0
  22. data/test/autocolor.rb +3 -0
  23. data/test/fixtures/rails_root/config/application.rb +10 -0
  24. data/test/fixtures/rails_root/config/assets/assets.rb +71 -0
  25. data/test/fixtures/rails_root/public/javascripts/bar/bas.js +1 -0
  26. data/test/fixtures/rails_root/public/javascripts/bas/bar.js +1 -0
  27. data/test/fixtures/rails_root/public/stylesheets/bar/bas.css +1 -0
  28. data/test/fixtures/rails_root/public/stylesheets/bas/bar.css +1 -0
  29. data/test/helper.rb +15 -0
  30. data/test/support/rails_helper.rb +32 -0
  31. metadata +151 -0
data/.autotest ADDED
@@ -0,0 +1,55 @@
1
+ require 'autotest/restart'
2
+ require 'autotest/bundler'
3
+
4
+ Autotest.add_hook :initialize do |at|
5
+ at.testlib = 'test/unit test/autocolor'
6
+
7
+ # Remove the old test unit mappings
8
+ at.clear_mappings
9
+
10
+ # Don't track other dirs, this just burns cpu.
11
+ (File.read('.gitignore').split("\n") + Dir['test/fixtures/**/*']).each do |ignore|
12
+ next if ignore.nil? or ignore.empty?
13
+ at.add_exception ignore
14
+ end
15
+
16
+ # Test::Unit is normally test_, so autotest doesn't have this mapping. Tests
17
+ # want to match themselves, that is, if there's no changes, run them all.
18
+ at.add_mapping(%r%^test/.*_test\.rb$%) { |f, _| f }
19
+
20
+ # Allow for matches of lib files to test files in a flat way
21
+ at.add_mapping(%r%^lib/(.*)\.rb$%) do |f, md|
22
+ at.files_matching( %r%^test/#{md[1]}_test\.rb$%)
23
+ end
24
+
25
+ # Make sure that we run all tests if the helper changes:
26
+ at.add_mapping(%r%^test/helper\.rb$%) do |f, _|
27
+ at.files_matching %r%.*_test\.rb%
28
+ end
29
+
30
+ # If bundle did something, run all tests again
31
+ at.add_mapping(%r%^Gemfile\.lock$%) do |f, _|
32
+ at.files_matching %r%.*_test\.rb%
33
+ end
34
+
35
+ # If update support, run all tests
36
+ at.add_mapping(%r%^test/support/.*\.rb$%) do |f, _|
37
+ at.files_matching %r%.*_test\.rb%
38
+ end
39
+
40
+ def at.path_to_classname(path)
41
+ file = File.basename(path, '.rb')
42
+
43
+ file.gsub!('test_', '')
44
+ file.gsub!('_test', '')
45
+ file.capitalize + 'Test'
46
+ end
47
+
48
+ end
49
+
50
+ # If the Gemfile gets updated, run bundle install
51
+ Autotest.add_hook :updated do |at, *args|
52
+ if args.flatten.grep(%r%^Gemfile$|^.*\.gemspec$%).any?
53
+ system 'bundle'
54
+ end
55
+ end
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .rvmrc
2
+ pkg/*
3
+ *.gem
4
+ .bundle
5
+ Gemfile.lock
6
+
7
+ test/fixtures/rails_root/log
8
+ test/fixtures/rails_root/public/stylesheets/cache
9
+ test/fixtures/rails_root/public/javascripts/cache
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails-assets.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake/testtask'
2
+ require 'bundler'
3
+ require 'bundler/setup'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "active_assets"
4
+ s.version = '0.1.0'
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ["Sam Woodard"]
7
+ s.email = ["sam@wildfireapp.com"]
8
+ s.homepage = ""
9
+ s.summary = %q{A Railtie that provides a full asset management system, including support for development and deployment.}
10
+ s.description = %q{A Railtie that provides a full asset management system, including support for development and deployment. Currently it is designed to manage javascripts and stylesheets but will handle image sprites in the future.}
11
+
12
+ s.rubyforge_project = "rails-assets"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "rails", "3.0.3"
20
+ s.add_development_dependency "test-unit", "> 2.0"
21
+ s.add_development_dependency "ZenTest"
22
+ end
@@ -0,0 +1,63 @@
1
+ module ActiveAssets
2
+ module ActiveExpansions
3
+ class Asset < Struct.new(:path, :type, :expansion_name, :group, :cache)
4
+
5
+ REQUIRED_PROPS = [:path, :type, :expansion_name]
6
+ VALID_TYPES = [:js, :css]
7
+
8
+ class InvalidContext < StandardError
9
+ def initialize(msg = nil)
10
+ msg ||= "You do not have a valid context to create this asset. Some properties are missing. They are: #{REQUIRED_PROPS.join(', ')}."
11
+ super(msg)
12
+ end
13
+ end
14
+
15
+ class AmbiguousContext < InvalidContext
16
+ def initilaize(missing_field)
17
+ super("You do not have a valid context to create this asset. Some properties are missing. Our guess is your didn't specify a #{missing_field}")
18
+ end
19
+ end
20
+
21
+ class ValidationError < InvalidContext
22
+ attr_reader :asset, :missing_fields
23
+ def initialize(asset, missing_fields)
24
+ @asset, @missing_fields = asset, missing_fields
25
+ super("#{asset} is not valid. The following fields are missing, #{missing_fields.join(', ')}.")
26
+ end
27
+ end
28
+
29
+ class InvalidAssetType < StandardError
30
+ attr_reader :invalid_type
31
+ def initilaize(invalid_type)
32
+ @invalid_type = invalid_type
33
+ super("#{invalid_type} is not a recognized asset type.")
34
+ end
35
+ end
36
+
37
+ def initialize(*)
38
+ super
39
+ self.group ||= :all
40
+ self.cache = self.cache == false ? false : true
41
+ end
42
+
43
+ def validation_error
44
+ missing_fields = REQUIRED_PROPS.reject { |meth| send(meth) }
45
+ if missing_fields.any?
46
+ ValidationError.new(self, missing_fields)
47
+ elsif !VALID_TYPES.include?(type)
48
+ InvalidAssetType.new(type)
49
+ end
50
+ end
51
+
52
+ def valid?
53
+ !validation_error
54
+ end
55
+
56
+ def valid!
57
+ e = validation_error
58
+ raise e if e
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ module ActiveAssets
2
+ module ActiveExpansions
3
+ module AssetScope
4
+ def group(*groups, &blk)
5
+ @current_groups = groups
6
+ instance_eval(&blk)
7
+ ensure
8
+ @current_groups = nil
9
+ end
10
+
11
+ def js(&blk)
12
+ current_type :js, &blk
13
+ end
14
+
15
+ def css(&blk)
16
+ current_type :css, &blk
17
+ end
18
+
19
+ private
20
+ def current_type(type, &blk)
21
+ @current_type = type
22
+ instance_eval(&blk)
23
+ ensure
24
+ @current_type = nil
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,97 @@
1
+ require 'fileutils'
2
+
3
+ module ActiveAssets
4
+ module ActiveExpansions
5
+ class Assets
6
+ attr_reader :expansions
7
+
8
+ def initialize(expansions)
9
+ @expansions = expansions
10
+ end
11
+
12
+ def has_expansion?(name)
13
+ @expansions.has_key?(name)
14
+ end
15
+
16
+ def expansion_names
17
+ @expansion.keys
18
+ end
19
+
20
+ def [](expansion_name)
21
+ @expansions[expansion_name]
22
+ end
23
+
24
+ def all(&blk)
25
+ @expansions.values.each(&blk) if block_given?
26
+ @expansions.values
27
+ end
28
+
29
+ def paths(expansion_name)
30
+ self[expansion_name].assets.map(&:path)
31
+ end
32
+
33
+ def asset_type
34
+ raise NoMethodError
35
+ end
36
+
37
+ def register!
38
+ all do |expansion|
39
+ paths = if ActionController::Base.perform_caching
40
+ group_assets = expansion.assets.select do |a|
41
+ Array(a.group).include?(:deploy) ||
42
+ Array(a.group).any? {|e| e.to_s != 'all' && Rails.env.send(:"#{e}?") }
43
+ end
44
+
45
+ group_assets.map(&:path) + [File.join("cache#{"/#{expansion.namespace}" if expansion.namespace}", expansion.name.to_s)]
46
+ else
47
+ expansion.assets.select {|a| a.group == :all || Array(a.group).any? {|e| Rails.env.send(:"#{e}?") } }.map(&:path)
48
+ end
49
+
50
+ cleanse_paths!(paths)
51
+
52
+ ActionView::Helpers::AssetTagHelper.send(:"register_#{asset_type}_expansion", expansion.name => paths)
53
+ end
54
+ end
55
+
56
+ def cache!
57
+ all do |expansion|
58
+ file_path = "#{"#{expansion.namespace}/" if expansion.namespace}#{expansion.name}.#{asset_type_short}"
59
+ file_path = Rails.root.join('public', asset_type.pluralize, 'cache', file_path)
60
+
61
+ paths = expansion.assets.select {|a|
62
+ a.cache &&
63
+ (Array(a.group).include?(:all) ||
64
+ Array(a.group).include?(:deploy) ||
65
+ Array(a.group).any? {|e| Rails.env.send(:"#{e}?") })
66
+ }.map(&:path)
67
+
68
+ cleanse_paths!(paths)
69
+
70
+ FileUtils.mkdir_p(File.dirname(file_path))
71
+
72
+ File.open(file_path, 'w+') do |f|
73
+ paths.each do |path|
74
+ in_file = Rails.root.join('public', asset_type.pluralize, "#{path}.#{asset_type_short}")
75
+ f.puts File.read(in_file)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ private
82
+ def asset_type_short
83
+ asset_type == 'javascript' ? 'js' : 'css'
84
+ end
85
+
86
+ def cleanse_paths!(paths)
87
+ paths.map! do |path|
88
+ if path =~ %r{^https?}
89
+ path
90
+ else
91
+ File.join(File.dirname(path), File.basename(path, ".#{asset_type_short}"))
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveAssets
4
+ module ActiveExpansions
5
+ module Configurable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ config_accessor :precache_assets
10
+ self.precache_assets = false if precache_assets.nil?
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,65 @@
1
+ require 'active_support'
2
+ require 'active_support/hash_with_indifferent_access'
3
+
4
+ module ActiveAssets
5
+ module ActiveExpansions
6
+ class Expansion
7
+ include AssetScope
8
+ include TypeInferrable
9
+
10
+ attr_reader :type, :name, :assets, :namespace
11
+ alias_method :all, :assets
12
+ delegate :empty?, :to => :assets
13
+
14
+ def initialize(name)
15
+ @name = name
16
+ @assets = []
17
+ end
18
+
19
+ def configure(options = {}, &blk)
20
+ @type, @group, @namespace = options.values_at(:type, :group, :namespace)
21
+ instance_eval(&blk) if block_given?
22
+ self
23
+ end
24
+
25
+ def asset(path, options = {})
26
+ options = HashWithIndifferentAccess.new(options)
27
+ options.assert_valid_keys(*Asset.members)
28
+
29
+ inferred_type, extension = inferred_type(path)
30
+
31
+ options.reverse_merge!(
32
+ :type => inferred_type || @current_type || extension || type,
33
+ :expansion_name => name,
34
+ :group => @current_groups
35
+ )
36
+ options.update(:path => path)
37
+
38
+ members = options.values_at(*Asset.members)
39
+ a = Asset.new(*members)
40
+
41
+ a.valid!
42
+ @assets << a
43
+ end
44
+ alias_method :a, :asset
45
+ alias_method :`, :asset
46
+
47
+ def group(*groups, &blk)
48
+ @current_groups = groups
49
+ instance_eval(&blk)
50
+ ensure
51
+ @current_groups = nil
52
+ end
53
+
54
+ def namespace(&blk)
55
+ raise NoMethodError, "Cannot call namespace from within expansion." if block_given?
56
+ @namespace
57
+ end
58
+
59
+ private
60
+ def cleanse_path(path)
61
+ File.join(File.dirname(path) + File.basename(path, ".#{type}"))
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,103 @@
1
+ require 'active_support/configurable'
2
+
3
+ module ActiveAssets
4
+ module ActiveExpansions
5
+ class Expansions
6
+ include AssetScope
7
+ include TypeInferrable
8
+ include ActiveSupport::Configurable
9
+ include Configurable
10
+ ActiveSupport.run_load_hooks(:active_expansions, self)
11
+
12
+ def initialize
13
+ @expansions = Hash.new(&method(:build_expansions_hash_with_defaults))
14
+ end
15
+
16
+ def register(&blk)
17
+ instance_eval(&blk) if block_given?
18
+ self
19
+ end
20
+
21
+ def namespace(name, &blk)
22
+ @current_namespace = name
23
+ instance_eval(&blk)
24
+ ensure
25
+ @current_namespace = nil
26
+ end
27
+
28
+ def asset(path, options = {})
29
+ deferred_expansion_name, deferred_expansion_options = @deferred_expansion_config
30
+ inferred_type, extension = inferred_type(path)
31
+
32
+ options = HashWithIndifferentAccess.new(options).reverse_merge(
33
+ :type => inferred_type || @current_type,
34
+ :expansion_name => deferred_expansion_name,
35
+ :group => @current_groups
36
+ )
37
+
38
+ expansion_options = update_expansion_options(deferred_expansion_options || {}, options[:type])
39
+
40
+ # asset call below is executed in the scope of the ActiveAssets::Expansion
41
+ @expansions[options[:type] || extension][options[:expansion_name]].configure(expansion_options) do
42
+ asset(path, options)
43
+ end
44
+ end
45
+ alias_method :a, :asset
46
+ alias_method :`, :asset
47
+
48
+ def expansion(name, options = {}, &blk)
49
+ options = update_expansion_options(options)
50
+
51
+ if options[:type].present?
52
+ @expansions[options[:type]][name].configure(options, &blk)
53
+ else
54
+ defer_expansion(name, options, &blk)
55
+ end
56
+ end
57
+
58
+ def javascripts
59
+ Javascripts.new(@expansions[:js])
60
+ end
61
+
62
+ def stylesheets
63
+ Stylesheets.new(@expansions[:css])
64
+ end
65
+
66
+ def all
67
+ @expansions[:js].values + @expansions[:css].values
68
+ end
69
+
70
+ def clear
71
+ @expansions.clear
72
+ end
73
+
74
+ private
75
+ def defer_expansion(name, options, &blk)
76
+ @deferred_expansion_config = [name, options]
77
+ instance_eval(&blk)
78
+ ensure
79
+ @deferred_expansion_config = nil
80
+ end
81
+
82
+ def build_expansions_hash_with_defaults(expansions, expansion_type)
83
+ raise Asset::AmbiguousContext.new(:type) if expansion_type.blank?
84
+ raise Asset::InvalidAssetType.new(expansion_type) unless Asset::VALID_TYPES.include?(expansion_type.to_sym)
85
+
86
+ expansions[expansion_type.to_sym] = build_typed_expansion_hash_with_defaults
87
+ end
88
+
89
+ def build_typed_expansion_hash_with_defaults
90
+ Hash.new do |typed_expansions, expansion_name|
91
+ raise Asset::AmbiguousContext.new(:name) if expansion_name.blank?
92
+ typed_expansions[expansion_name] = Expansion.new(expansion_name)
93
+ end
94
+ end
95
+
96
+ def update_expansion_options(options, asset_type = nil)
97
+ o = options.reverse_merge(:type => @current_ytpe, :namespace => @current_namespace)
98
+ o.update(:type => asset_type) if asset_type
99
+ o
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveAssets
2
+ module ActiveExpansions
3
+ class Javascripts < Assets
4
+ def asset_type
5
+ 'javascript'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ require "rails"
2
+ require "rails/active_assets"
3
+ require 'active_support/ordered_options'
4
+
5
+ module ActiveAssets
6
+ module ActiveExpansions
7
+ class Railtie < Rails::Railtie
8
+ rake_tasks do
9
+ Dir[File.expand_path("../../../tasks/*.rake", __FILE__)].each {|f| load f}
10
+ end
11
+
12
+ config.active_expansions = ActiveSupport::OrderedOptions.new
13
+
14
+ initializer :active_assets_extend_application do
15
+ Rails.application.extend(Rails::ActiveAssets)
16
+ end
17
+
18
+ initializer :load_active_assets do
19
+ load_active_assets(Rails.root)
20
+ Rails.application.railties.engines.each {|e| load_active_assets(e.root) }
21
+ end
22
+
23
+ initializer :register_active_assets_expansions do
24
+ Rails.application.expansions.javascripts.register! and Rails.application.expansions.stylesheets.register!
25
+ end
26
+
27
+ initializer :set_active_expansion_configs do
28
+ options = config.active_expansions
29
+ ActiveSupport.on_load(:active_expansions) do
30
+ options.each { |k,v| send("#{k}=", v) }
31
+ end
32
+ end
33
+
34
+ initializer :cache_active_assets do
35
+ if Expansions.precache_assets
36
+ Rails.application.expansions.javascripts.cache! and Rails.application.expansions.stylesheets.cache!
37
+ end
38
+ end
39
+
40
+ private
41
+ def load_active_assets(root)
42
+ if File.exists?(File.join(root, 'config/assets.rb'))
43
+ load File.join(root, 'config/assets.rb')
44
+ elsif File.directory?(File.join(root, 'config/assets'))
45
+ Dir[File.join(root, 'config/assets/*.rb')].each do |f|
46
+ load f
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveAssets
2
+ module ActiveExpansions
3
+ class Stylesheets < Assets
4
+ def asset_type
5
+ 'stylesheet'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveAssets
2
+ module ActiveExpansions
3
+ module TypeInferrable
4
+ def inferred_type(file_path, allowed_extensions = Asset::VALID_TYPES)
5
+ file_ext = File.extname(file_path)
6
+ return [nil, nil] unless file_ext.present? && (3..5).include?(file_ext.size)
7
+ file_ext = file_ext[1..-1].to_sym
8
+ [allowed_extensions.include?(file_ext) && file_ext, file_ext]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support'
2
+
3
+ module ActiveAssets
4
+ module ActiveExpansions
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Asset
8
+ autoload :AssetScope
9
+ autoload :TypeInferrable
10
+ autoload :Configurable
11
+ autoload :Expansion
12
+ autoload :Assets
13
+ autoload :Javascripts
14
+ autoload :Stylesheets
15
+ autoload :Expansions
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support'
2
+
3
+ module ActiveAssets
4
+ extend ActiveSupport::Autoload
5
+
6
+ autoload :ActiveExpansions
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_assets'
2
+
3
+ module Rails
4
+ module ActiveAssets
5
+ def expansions(&blk)
6
+ @expansions ||= ::ActiveAssets::ActiveExpansions::Expansions.new
7
+ @expansions.instance_eval(&blk) if block_given?
8
+ @expansions
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ namespace :activeexpansions do
2
+ desc "Cache the active expansions to the {stylesheets,javascripts} cache directory"
3
+ task :cache => :environment do
4
+ Rails.application.expansions.javascripts.cache! and Rails.application.expansions.stylesheets.cache!
5
+ end
6
+ end
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ class AssetTest < Test::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def teardown
8
+ end
9
+
10
+ def test_fields_are_set_to_defaults
11
+ asset = ActiveAssets::ActiveExpansions::Asset.new
12
+ assert_equal [nil, nil, nil, :all, true], ActiveAssets::ActiveExpansions::Asset.members.map {|member| asset.send(member)}
13
+ end
14
+
15
+ def test_can_set_fields_through_constructor
16
+ asset = ActiveAssets::ActiveExpansions::Asset.new('/path/to/asset', :js, :defaults, [:development, :test], false)
17
+ assert_equal ['/path/to/asset', :js, :defaults, [:development, :test], false], ActiveAssets::ActiveExpansions::Asset.members.map {|member| asset.send(member)}
18
+ end
19
+
20
+ def test_that_cache_will_be_true_unless_set_to_false
21
+ asset = ActiveAssets::ActiveExpansions::Asset.new('/path/to/asset', :js, :defaults, [:development, :test], "not false")
22
+ assert asset.cache
23
+ end
24
+
25
+ def test_validation
26
+ asset = ActiveAssets::ActiveExpansions::Asset.new(nil, :js, :defaults, [:development, :test], "not false")
27
+ assert !asset.valid?
28
+
29
+ asset = ActiveAssets::ActiveExpansions::Asset.new('/path/to/asset', nil, :defaults, [:development, :test], "not false")
30
+ assert !asset.valid?
31
+
32
+ asset = ActiveAssets::ActiveExpansions::Asset.new('/path/to/asset', :js, nil, [:development, :test], "not false")
33
+ assert !asset.valid?
34
+ end
35
+
36
+ def test_valid_bang
37
+ asset = ActiveAssets::ActiveExpansions::Asset.new(nil, :js, :defaults, [:development, :test], "not false")
38
+ assert_raise ActiveAssets::ActiveExpansions::Asset::ValidationError do asset.valid!; end
39
+ end
40
+ end
@@ -0,0 +1,148 @@
1
+ require 'helper'
2
+
3
+ class ExpansionsTest < Test::Unit::TestCase
4
+ def setup
5
+ initialize_application_or_load_assets!
6
+ end
7
+
8
+ def teardown
9
+ delete_cached_assets!
10
+ Rails.application.expansions.clear
11
+ end
12
+
13
+ def test_new
14
+ assert_nothing_raised do
15
+ ActiveAssets::ActiveExpansions::Expansions.new
16
+ end
17
+ end
18
+
19
+ def test_namespace
20
+ Rails.application.expansions.namespace :fubar do
21
+ expansion :foo, :type => :js do
22
+ `bas/bar`
23
+ `bar/bas`
24
+ end
25
+ end
26
+
27
+ assert Rails.application.expansions.all.any? {|e| e.namespace == :fubar}
28
+ end
29
+
30
+ def test_namespace_2
31
+ Rails.application.expansions do
32
+ namespace :fubar do
33
+ expansion :foo, :type => :js do
34
+ `bas/bar`
35
+ `bar/bas`
36
+ end
37
+ end
38
+ end
39
+
40
+ assert Rails.application.expansions.all.any? {|e| e.namespace == :fubar}
41
+ end
42
+
43
+ def test_namespace_3
44
+ Rails.application.expansions do
45
+ expansion :foo, :type => :js, :namespace => :fubar do
46
+ `bas/bar`
47
+ `bar/bas`
48
+ end
49
+ end
50
+
51
+ assert Rails.application.expansions.all.any? {|e| e.namespace == :fubar}
52
+ end
53
+
54
+ def test_asset
55
+ Rails.application.expansions.asset 'bas/bar', :type => :js, :expansion_name => :foo
56
+
57
+ assert Rails.application.expansions.javascripts.has_expansion?(:foo)
58
+ assert_equal :js, Rails.application.expansions.javascripts[:foo].type
59
+ assert Rails.application.expansions.javascripts.all.map(&:assets).flatten.any? {|a| "bar/bas" == a.path}
60
+ end
61
+
62
+ def test_asset_2
63
+ Rails.application.expansions do
64
+ expansion :foo do
65
+ `bas/bar.js`
66
+ end
67
+ end
68
+
69
+ assert Rails.application.expansions.javascripts.has_expansion?(:foo)
70
+ assert_equal :js, Rails.application.expansions.javascripts[:foo].type
71
+ assert Rails.application.expansions.javascripts.all.map(&:assets).flatten.any? {|a| "bar/bas.js" == a.path}
72
+ end
73
+
74
+ def test_asset_3
75
+ Rails.application.expansions do
76
+ asset "bas/bar", :expansion_name => :foo, :type => :js
77
+ end
78
+
79
+ assert Rails.application.expansions.javascripts.has_expansion?(:foo)
80
+ assert_equal :js, Rails.application.expansions.javascripts[:foo].type
81
+ assert Rails.application.expansions.javascripts.all.map(&:assets).flatten.any? {|a| "bar/bas" == a.path}
82
+ end
83
+
84
+ def test_asset_4
85
+ Rails.application.expansions do
86
+ asset "bas/bar.js", :expansion_name => :foo
87
+ end
88
+
89
+ assert Rails.application.expansions.javascripts.has_expansion?(:foo)
90
+ assert_equal :js, Rails.application.expansions.javascripts[:foo].type
91
+ assert Rails.application.expansions.javascripts.all.map(&:assets).flatten.any? {|a| "bar/bas.js" == a.path}
92
+ end
93
+
94
+ def test_asset_5
95
+ Rails.application.expansions do
96
+ js do
97
+ asset "bas/bar", :expansion_name => :foo
98
+ end
99
+ end
100
+
101
+ assert Rails.application.expansions.javascripts.has_expansion?(:foo)
102
+ assert_equal :js, Rails.application.expansions.javascripts[:foo].type
103
+ assert Rails.application.expansions.javascripts.all.map(&:assets).flatten.any? {|a| "bar/bas" == a.path}
104
+ end
105
+
106
+ def test_asset_6
107
+ Rails.application.expansions do
108
+ css do
109
+ asset "bas/bar", :expansion_name => :foo
110
+ end
111
+ end
112
+
113
+ assert Rails.application.expansions.stylesheets.has_expansion?(:foo)
114
+ assert_equal :css, Rails.application.expansions.stylesheets[:foo].type
115
+ assert Rails.application.expansions.stylesheets.all.map(&:assets).flatten.any? {|a| "bar/bas" == a.path}
116
+ end
117
+
118
+ def test_asset_7
119
+ assert Rails.application.expansions.javascripts.has_expansion?(:jazz)
120
+ assert Rails.application.expansions.stylesheets.has_expansion?(:jazz)
121
+ end
122
+
123
+ def test_asset_8
124
+ assert Rails.application.expansions.javascripts.has_expansion?(:dev)
125
+ assert Rails.application.expansions.stylesheets.has_expansion?(:dev)
126
+ end
127
+
128
+ def test_asset_9
129
+ assert_raise ActiveAssets::ActiveExpansions::Asset::InvalidAssetType do
130
+ Rails.application.expansions do
131
+ expansion :foo do
132
+ `bas/bar.pdf`
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ def test_asset_10
139
+ assert_raise ActiveAssets::ActiveExpansions::Asset::AmbiguousContext do
140
+ Rails.application.expansions do
141
+ expansion :foo do
142
+ `vendor/jquery.mousewheel`
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ end
data/test/autocolor.rb ADDED
@@ -0,0 +1,3 @@
1
+ Test::Unit::AutoRunner.setup_option do |auto_runner, opts|
2
+ auto_runner.runner_options[:use_color] = true
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails/all'
2
+ require 'active_assets/active_expansions/railtie'
3
+
4
+ module ActiveAssetsTest
5
+ class Application < Rails::Application
6
+ config.root = File.expand_path('../..', __FILE__)
7
+ end
8
+ end
9
+
10
+ Rails.application.config.active_support.deprecation = :stderr
@@ -0,0 +1,71 @@
1
+ Rails.application.expansions.register do
2
+ expansion :defaults, :type => :js do
3
+ `bas/bar`
4
+ `bar/bas`
5
+ end
6
+
7
+ js do
8
+ expansion :bas do
9
+ `bas/bar`
10
+ `bar/bas`
11
+ end
12
+ end
13
+
14
+ css do
15
+ expansion :bas do
16
+ `bas/bar`
17
+ `bar/bas`
18
+ end
19
+ end
20
+
21
+ # for ExpansionsTest#asset_test_7
22
+ expansion :jazz do
23
+ js do
24
+ `bas/bar`
25
+ `bar/bas`
26
+ end
27
+
28
+ css do
29
+ `bas/bar`
30
+ `bar/bas`
31
+ end
32
+ end
33
+
34
+ # for ExpansionsTest#asset_test_8
35
+ group :development do
36
+ expansion :dev do
37
+ asset 'bas/bar', :type => :js
38
+ asset 'bas/bar', :type => :css
39
+ end
40
+ end
41
+
42
+ group :development do
43
+ expansion :dev2 do
44
+ js do
45
+ `bas/bar`
46
+ `bar/bas`
47
+ end
48
+ end
49
+ end
50
+
51
+ expansion :envs do
52
+ group :development do
53
+ a 'bar/bas', :type => :css
54
+ js do
55
+ `bar/bas`
56
+ end
57
+ end
58
+ end
59
+
60
+ expansion :envs2, :type => :js do
61
+ group :development do
62
+ `bas/bar`
63
+ `bar/bas`
64
+ end
65
+ end
66
+
67
+ expansion :basfoo do
68
+ `bas/bar.js`
69
+ `bar/bas.js`
70
+ end
71
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require 'test/unit'
3
+
4
+ require 'rails/all'
5
+ require 'active_assets'
6
+
7
+ TEST_RAILS_ROOT = File.expand_path('../fixtures/rails_root', __FILE__)
8
+
9
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each {|f| load f }
10
+
11
+ load File.join(TEST_RAILS_ROOT, 'config/application.rb')
12
+
13
+ class Test::Unit::TestCase
14
+ include RailsHelper
15
+ end
@@ -0,0 +1,32 @@
1
+ require 'fileutils'
2
+
3
+ module RailsHelper
4
+ def rails_root
5
+ @rails_root = TEST_RAILS_ROOT
6
+ end
7
+
8
+ def delete_cached_assets!
9
+ Dir[File.join(rails_root, 'public/javascripts/cache'), File.join(rails_root, 'public/stylesheets/cache')].each do |dir|
10
+ FileUtils.rm_rf(dir)
11
+ end
12
+ end
13
+
14
+ def initialize_application_or_load_assets!
15
+ load_assets! if Rails.application && Rails.application.instance_variable_defined?(:@ran)
16
+ initialize_application! unless Rails.application && Rails.application.instance_variable_defined?(:@ran)
17
+ end
18
+
19
+ def load_assets!
20
+ if File.exists?(File.join(rails_root, 'config/assets.rb'))
21
+ load File.join(rails_root, 'config/assets.rb')
22
+ elsif File.directory?(File.join(rails_root, 'config/assets'))
23
+ Dir[File.join(rails_root, 'config/assets/*.rb')].each do |f|
24
+ load f
25
+ end
26
+ end
27
+ end
28
+
29
+ def initialize_application!
30
+ ActiveAssetsTest::Application.initialize!
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_assets
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Sam Woodard
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-23 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ hash: 1
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 3
33
+ version: 3.0.3
34
+ type: :development
35
+ name: rails
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">"
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 2
47
+ - 0
48
+ version: "2.0"
49
+ type: :development
50
+ name: test-unit
51
+ requirement: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ prerelease: false
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ name: ZenTest
65
+ requirement: *id003
66
+ description: A Railtie that provides a full asset management system, including support for development and deployment. Currently it is designed to manage javascripts and stylesheets but will handle image sprites in the future.
67
+ email:
68
+ - sam@wildfireapp.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - .autotest
77
+ - .gitignore
78
+ - Gemfile
79
+ - Rakefile
80
+ - active_assets.gemspec
81
+ - lib/active_assets.rb
82
+ - lib/active_assets/active_expansions.rb
83
+ - lib/active_assets/active_expansions/asset.rb
84
+ - lib/active_assets/active_expansions/asset_scope.rb
85
+ - lib/active_assets/active_expansions/assets.rb
86
+ - lib/active_assets/active_expansions/configurable.rb
87
+ - lib/active_assets/active_expansions/expansion.rb
88
+ - lib/active_assets/active_expansions/expansions.rb
89
+ - lib/active_assets/active_expansions/javascripts.rb
90
+ - lib/active_assets/active_expansions/railtie.rb
91
+ - lib/active_assets/active_expansions/stylesheets.rb
92
+ - lib/active_assets/active_expansions/type_inferrable.rb
93
+ - lib/rails/active_assets.rb
94
+ - lib/tasks/cache.rake
95
+ - test/active_assets/active_expansions/asset_test.rb
96
+ - test/active_assets/active_expansions/expansions_test.rb
97
+ - test/autocolor.rb
98
+ - test/fixtures/rails_root/config/application.rb
99
+ - test/fixtures/rails_root/config/assets/assets.rb
100
+ - test/fixtures/rails_root/public/javascripts/bar/bas.js
101
+ - test/fixtures/rails_root/public/javascripts/bas/bar.js
102
+ - test/fixtures/rails_root/public/stylesheets/bar/bas.css
103
+ - test/fixtures/rails_root/public/stylesheets/bas/bar.css
104
+ - test/helper.rb
105
+ - test/support/rails_helper.rb
106
+ has_rdoc: true
107
+ homepage: ""
108
+ licenses: []
109
+
110
+ post_install_message:
111
+ rdoc_options: []
112
+
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ requirements: []
134
+
135
+ rubyforge_project: rails-assets
136
+ rubygems_version: 1.4.1
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: A Railtie that provides a full asset management system, including support for development and deployment.
140
+ test_files:
141
+ - test/active_assets/active_expansions/asset_test.rb
142
+ - test/active_assets/active_expansions/expansions_test.rb
143
+ - test/autocolor.rb
144
+ - test/fixtures/rails_root/config/application.rb
145
+ - test/fixtures/rails_root/config/assets/assets.rb
146
+ - test/fixtures/rails_root/public/javascripts/bar/bas.js
147
+ - test/fixtures/rails_root/public/javascripts/bas/bar.js
148
+ - test/fixtures/rails_root/public/stylesheets/bar/bas.css
149
+ - test/fixtures/rails_root/public/stylesheets/bas/bar.css
150
+ - test/helper.rb
151
+ - test/support/rails_helper.rb