The project is in a healthy, maintained state
Merges HTML attributes from multiple sources with special handling for Stimulus 2.0 data attributes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

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:

  1. Run bin/setup to install dependencies
  2. Run bundle exec rspec to execute tests
  3. Run bundle exec rubocop to check code style
  4. Run bundle exec standardrb to verify Standard Ruby style

Code Quality

The project uses:

Test Coverage

To generate a coverage report:

COVERAGE=true bundle exec rspec

Open coverage/index.html to view the report.

Contributing

  1. Fork the project
  2. Create a feature branch
  3. Add tests for your changes
  4. Ensure all tests pass and coverage remains at 90%+
  5. Submit a pull request

License

MIT License - see LICENSE.txt