Skip to content

Commit

Permalink
Merge pull request #18 from javierav/feature/helper
Browse files Browse the repository at this point in the history
Add helper module for defining variants
  • Loading branch information
javierav authored Oct 24, 2024
2 parents 3208fc1 + 1f18fc7 commit 69468f6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
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

0 comments on commit 69468f6

Please sign in to comment.