Skip to content
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 helper module for defining variants #18

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased
- Add support for slots ([#15](https://github.com/avo-hq/class_variants/pull/15))
- Allow passing additional classes when render ([#17](https://github.com/avo-hq/class_variants/pull/17))
- Add helper module for defining variants ([#18](https://github.com/avo-hq/class_variants/pull/18))

## 0.0.8 (2024-10-24)
- Deprecate usage of positional arguments ([#12](https://github.com/avo-hq/class_variants/pull/12))
Expand Down
1 change: 1 addition & 0 deletions lib/class_variants.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "class_variants/version"
require "class_variants/action_view/helpers"
require "class_variants/instance"
require "class_variants/helper"
require "class_variants/railtie" if defined?(Rails)

module ClassVariants
Expand Down
23 changes: 23 additions & 0 deletions lib/class_variants/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ClassVariants
module Helper
module ClassMethods
def class_variants(...)
@_class_variants_instance = ClassVariants.build(...)
end

def _class_variants_instance
@_class_variants_instance
end
end

def self.included(base)
base.extend(ClassMethods)
end

def class_variants(...)
raise "You must configure class_variants in class definition" unless self.class._class_variants_instance

self.class._class_variants_instance.render(...)
end
end
end
13 changes: 13 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "test_helper"

class HelperTest < Minitest::Test
class DemoClass
include ClassVariants::Helper

class_variants base: "rounded border"
end

def test_call_from_instance
assert_equal "rounded border", DemoClass.new.class_variants
end
end