ViewAttributeMerge
A Ruby gem for merging HTML view attributes with support for Stimulus 2.0 conventions.
Features
- Merges HTML attributes from multiple sources
- Handles and concatenates Stimulus 2.0 data attributes:
data-controller
data-action
data-[controller]-target
- Supports nested data/aria attributes
- Properly concatenates class attributes
- Maintains attribute precedence
Installation
Add to your Gemfile:
gem 'view_attribute_merge'
Usage
attributes = ViewAttributeMerge.attr_merge(
{ class: "btn", data: { controller: "modal" } },
{ class: "btn-primary", "data-action": "click->modal#open" },
{ "data-modal-target": "dialog" }
)
# Returns:
# {
# class: ["btn", "btn-primary"],
# data: {
# controller: "modal",
# action: "click->modal#open",
# target: "dialog"
# }
# }
Stimulus 2.0 Support
The gem follows Stimulus 2.0 conventions:
-
data-controller
values are concatenated -
data-action
values are concatenated -
data-[controller]-target
values are concatenated
# Proper Stimulus 2.0 format:
{ "data-controller": "controller", "data-controller-target": "element" }
# Legacy format (not supported):
{ "data-target": "controller.element" }
Development
After checking out the repo:
- Run
bin/setup
to install dependencies - Run
bundle exec rspec
to execute tests - Run
bundle exec rubocop
to check code style - Run
bundle exec standardrb
to verify Standard Ruby style
Code Quality
The project uses:
- Rubocop for linting
- Standard for formatting
- SimpleCov for test coverage (minimum 90%)
- Solargraph for documentation
Test Coverage
To generate a coverage report:
COVERAGE=true bundle exec rspec
Open coverage/index.html
to view the report.
Contributing
- Fork the project
- Create a feature branch
- Add tests for your changes
- Ensure all tests pass and coverage remains at 90%+
- Submit a pull request
License
MIT License - see LICENSE.txt