-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for post process classes
- Loading branch information
Showing
11 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module ClassVariants | ||
class Configuration | ||
def process_classes_with(&block) | ||
if block_given? | ||
@process_classes_with = block | ||
else | ||
@process_classes_with | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Description: | ||
Generates initializer file for configure ClassVariants in your application. |
11 changes: 11 additions & 0 deletions
11
lib/generators/class_variants/install/install_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module ClassVariants | ||
module Generators | ||
class InstallGenerator < Rails::Generators::Base | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
def copy_initializer | ||
template "class_variants.rb", "config/initializers/class_variants.rb" | ||
end | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
lib/generators/class_variants/install/templates/class_variants.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
# ClassVariants.configure do |config| | ||
# # allow to post process classes with an external utility like TailwindMerger | ||
# config.process_classes_with do |classes| | ||
# TailwindMerge::Merger.new.merge(classes) | ||
# end | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "test_helper" | ||
|
||
class ConfigurationTest < Minitest::Test | ||
def teardown | ||
ClassVariants.configuration.instance_variable_set(:@process_classes_with, nil) | ||
end | ||
|
||
def test_configuration_process_classes_with_default | ||
refute ClassVariants.configuration.process_classes_with | ||
end | ||
|
||
def test_configure | ||
ClassVariants.configure do |config| | ||
config.process_classes_with { |classes| classes } | ||
end | ||
|
||
assert_respond_to ClassVariants.configuration.process_classes_with, :call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "test_helper" | ||
|
||
class ProcessClassesWithTest < Minitest::Test | ||
def teardown | ||
ClassVariants.configuration.instance_variable_set(:@process_classes_with, nil) | ||
end | ||
|
||
def test_without_classes_processor | ||
assert_equal "border rounded px-2 py-1 p-5", ClassVariants.build(base: "border rounded px-2 py-1 p-5").render | ||
end | ||
|
||
def test_with_classes_processor | ||
ClassVariants.configure do |config| | ||
config.process_classes_with do |classes| | ||
merger.merge(classes) | ||
end | ||
end | ||
|
||
assert_equal "border rounded p-5", ClassVariants.build(base: "border rounded px-2 py-1 p-5").render | ||
end | ||
|
||
private | ||
|
||
def merger | ||
require "tailwind_merge" | ||
TailwindMerge::Merger.new | ||
rescue LoadError | ||
Class.new do | ||
def merge(classes) | ||
classes.gsub("px-2 py-1 ", "") | ||
end | ||
end.new | ||
end | ||
end |