Skip to content

Commit 69468f6

Browse files
authored
Merge pull request avo-hq#18 from javierav/feature/helper
Add helper module for defining variants
2 parents 3208fc1 + 1f18fc7 commit 69468f6

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22
- Add support for slots ([#15](https://github.com/avo-hq/class_variants/pull/15))
33
- Allow passing additional classes when render ([#17](https://github.com/avo-hq/class_variants/pull/17))
4+
- Add helper module for defining variants ([#18](https://github.com/avo-hq/class_variants/pull/18))
45

56
## 0.0.8 (2024-10-24)
67
- Deprecate usage of positional arguments ([#12](https://github.com/avo-hq/class_variants/pull/12))

Diff for: lib/class_variants.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "class_variants/version"
22
require "class_variants/action_view/helpers"
33
require "class_variants/instance"
4+
require "class_variants/helper"
45
require "class_variants/railtie" if defined?(Rails)
56

67
module ClassVariants

Diff for: lib/class_variants/helper.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module ClassVariants
2+
module Helper
3+
module ClassMethods
4+
def class_variants(...)
5+
@_class_variants_instance = ClassVariants.build(...)
6+
end
7+
8+
def _class_variants_instance
9+
@_class_variants_instance
10+
end
11+
end
12+
13+
def self.included(base)
14+
base.extend(ClassMethods)
15+
end
16+
17+
def class_variants(...)
18+
raise "You must configure class_variants in class definition" unless self.class._class_variants_instance
19+
20+
self.class._class_variants_instance.render(...)
21+
end
22+
end
23+
end

Diff for: test/helper_test.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "test_helper"
2+
3+
class HelperTest < Minitest::Test
4+
class DemoClass
5+
include ClassVariants::Helper
6+
7+
class_variants base: "rounded border"
8+
end
9+
10+
def test_call_from_instance
11+
assert_equal "rounded border", DemoClass.new.class_variants
12+
end
13+
end

0 commit comments

Comments
 (0)