-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for post process classes #16
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about generating the initializer uncommented and leave only the
process_classes_with
option commented out, but I think I like this approach better.We are not configuring anything with it, we might as well not load it.
Good call!