From b2bba8c606d9235be2ab7a9920227e4fd65822ac Mon Sep 17 00:00:00 2001
From: Marc Villanueva
Date: Thu, 24 Apr 2014 15:58:24 -0700
Subject: [PATCH] Add number_of_splits and max_selectors configurable
---
Gemfile | 2 +-
Gemfile.lock | 20 ++++---------------
.../css_splitter/application_helper.rb | 8 ++++++--
lib/css_splitter.rb | 12 +++++++++++
lib/css_splitter/engine.rb | 4 +++-
lib/css_splitter/sprockets_engine.rb | 10 ++++++----
test/css_splitter_test.rb | 15 ++++++++++++++
.../css_splitter/application_helper_test.rb | 11 ++++++++++
8 files changed, 58 insertions(+), 24 deletions(-)
diff --git a/Gemfile b/Gemfile
index 4042fd6..fa755b2 100644
--- a/Gemfile
+++ b/Gemfile
@@ -6,7 +6,7 @@ source "http://rubygems.org"
gemspec
group :development do
- gem "pry-debugger"
+ gem "byebug"
end
gem "sass-rails"
gem "jquery-rails"
diff --git a/Gemfile.lock b/Gemfile.lock
index 12db0ff..de2a156 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -36,14 +36,11 @@ GEM
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
- coderay (1.0.9)
+ byebug (2.7.0)
+ columnize (~> 0.3)
+ debugger-linecache (~> 1.2)
columnize (0.3.6)
- debugger (1.5.0)
- columnize (>= 0.3.1)
- debugger-linecache (~> 1.2.0)
- debugger-ruby_core_source (~> 1.2.0)
debugger-linecache (1.2.0)
- debugger-ruby_core_source (1.2.0)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
@@ -57,17 +54,9 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
- method_source (0.8.1)
mime-types (1.25)
multi_json (1.7.1)
polyglot (0.3.3)
- pry (0.9.12)
- coderay (~> 1.0.5)
- method_source (~> 0.8)
- slop (~> 3.4)
- pry-debugger (0.2.2)
- debugger (~> 1.3)
- pry (~> 0.9.10)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
@@ -98,7 +87,6 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
- slop (3.4.4)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
@@ -118,8 +106,8 @@ PLATFORMS
ruby
DEPENDENCIES
+ byebug
css_splitter!
jquery-rails
- pry-debugger
sass-rails
uglifier
diff --git a/app/helpers/css_splitter/application_helper.rb b/app/helpers/css_splitter/application_helper.rb
index 644a071..16422b4 100644
--- a/app/helpers/css_splitter/application_helper.rb
+++ b/app/helpers/css_splitter/application_helper.rb
@@ -4,7 +4,11 @@ def split_stylesheet_link_tag(*sources)
original_sources = sources.dup
options = sources.extract_options!
- sources.collect!{ |source| "#{source}_split2" }
+ sources = sources.each_with_object([]) do |source, collection|
+ for i in 2..CssSplitter.config.number_of_splits
+ collection << "#{source}_split#{i}"
+ end
+ end
sources << options
[
@@ -15,4 +19,4 @@ def split_stylesheet_link_tag(*sources)
].join("\n").html_safe
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/css_splitter.rb b/lib/css_splitter.rb
index 7a19c44..34a672e 100644
--- a/lib/css_splitter.rb
+++ b/lib/css_splitter.rb
@@ -3,4 +3,16 @@
require "css_splitter/splitter"
module CssSplitter
+ def self.config(&block)
+ @@config ||= begin
+ engine = Engine::Configuration.new
+ engine.number_of_splits = 2
+ engine.max_selectors = 4095
+ engine
+ end
+
+ yield @@config if block
+
+ return @@config
+ end
end
diff --git a/lib/css_splitter/engine.rb b/lib/css_splitter/engine.rb
index 4352159..a10811a 100644
--- a/lib/css_splitter/engine.rb
+++ b/lib/css_splitter/engine.rb
@@ -3,7 +3,9 @@ class Engine < ::Rails::Engine
isolate_namespace CssSplitter
initializer 'css_splitter.sprockets_engine', after: 'sprockets.environment', group: :all do |app|
- app.assets.register_engine '.split2', CssSplitter::SprocketsEngine
+ for i in 2..CssSplitter.config.number_of_splits
+ app.assets.register_engine ".split#{i}", CssSplitter::SprocketsEngine
+ end
end
initializer 'css_splitter.action_controller' do |app|
diff --git a/lib/css_splitter/sprockets_engine.rb b/lib/css_splitter/sprockets_engine.rb
index 9f40c03..34ea6b8 100644
--- a/lib/css_splitter/sprockets_engine.rb
+++ b/lib/css_splitter/sprockets_engine.rb
@@ -11,9 +11,11 @@ def prepare
end
def evaluate(scope, locals, &block)
- split = scope.pathname.extname =~ /(\d+)$/ && $1 || 0 # determine which is the current split (e.g. split2, split3)
- CssSplitter::Splitter.split_string data, split.to_i
+ split = if scope.pathname.extname =~ /(\d+)$/; $1
+ elsif scope.pathname.basename.to_s =~ /_split(\d+)\.css/; $1
+ else 2
+ end
+ CssSplitter::Splitter.split_string data, split.to_i, CssSplitter.config.max_selectors
end
end
-
-end
\ No newline at end of file
+end
diff --git a/test/css_splitter_test.rb b/test/css_splitter_test.rb
index 7fd8634..1cb25f8 100644
--- a/test/css_splitter_test.rb
+++ b/test/css_splitter_test.rb
@@ -4,4 +4,19 @@ class CssSplitterTest < ActiveSupport::TestCase
test "truth" do
assert_kind_of Module, CssSplitter
end
+
+ test "config" do
+ assert_kind_of CssSplitter::Engine::Configuration, CssSplitter.config
+ end
+
+ test "config_set_options" do
+ CssSplitter.config.max_selectors = 10
+
+ assert_equal 10, CssSplitter.config.max_selectors
+ end
+
+ test "config_default_options" do
+ assert_equal 4095, CssSplitter.config.max_selectors
+ assert_equal 2, CssSplitter.config.number_of_splits
+ end
end
diff --git a/test/unit/helpers/css_splitter/application_helper_test.rb b/test/unit/helpers/css_splitter/application_helper_test.rb
index 1fedb0a..428a38a 100644
--- a/test/unit/helpers/css_splitter/application_helper_test.rb
+++ b/test/unit/helpers/css_splitter/application_helper_test.rb
@@ -13,5 +13,16 @@ class ApplicationHelperTest < ActionView::TestCase
assert_equal "\n\n", output
end
+ test "should work with multiple split files" do
+ CssSplitter.config.number_of_splits = 3
+ CssSplitter.config.max_selectors = 2000
+
+ output = split_stylesheet_link_tag("too_big_stylesheet")
+
+ CssSplitter.config.number_of_splits = 2
+ CssSplitter.config.max_selectors = 4095
+
+ assert_equal "\n", output
+ end
end
end