Skip to content

Commit

Permalink
Make cocooned wrappers support custom tag name (#56)
Browse files Browse the repository at this point in the history
* Add support for custom tag name to container helpers
* Update documentation
  • Loading branch information
gael-ian authored Jan 28, 2024
1 parent 62171af commit 3f96bc3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

* Add support for custom tag name to container helpers (#55, #56)
`cocooned_container` and `cocooned_item` now support an optional tag name as their first argument (as `content_tag` do) when the default `<div/>` is not appropriate.
**Warning:** This change is not supposed to break anything as helpers prototypes stays the same and no other positional argument where really expected before. However, depending on how you used these helpers with previous releases, you may encounter unexpected side effects.

### Changed

* Update test matrix (#54)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ And your sub form as follow:
+ <% end %>
```

The `cocooned_container` and `cocooned_item` helpers will set for you the HTML attributes the JavaScript part of Cocooned expect to find to hook on. They will forward any option supported by ActionView's `content_tag`.
The `cocooned_container` and `cocooned_item` helpers will set for you the HTML attributes the JavaScript part of Cocooned expect to find to hook on. They will forward any option supported by ActionView's `content_tag` and accept a tag name as first argument if you don't want to use the default `<div>`.

### 3. Add a way to add a new item to the list

Expand Down
6 changes: 4 additions & 2 deletions lib/cocooned/helpers/containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ module Containers
# will be forwarded.
def cocooned_container(*args, &block)
options = args.extract_options!.dup
name = args.shift || :div
defaults = cocooned_wrapper_defaults(options, %w[cocooned-container], :'cocooned-container')
defaults[:data][:cocooned_options] = options.extract!(:limit, :reorderable).to_json

content_tag(:div, *args, **options.deep_merge(defaults), &block)
content_tag(name, *args, **options.deep_merge(defaults), &block)
end

# Wrap content with the expected markup for a Cocooned item.
Expand All @@ -54,9 +55,10 @@ def cocooned_container(*args, &block)
# be forwarded.
def cocooned_item(*args, &block)
options = args.extract_options!.dup
name = args.shift || :div
defaults = cocooned_wrapper_defaults(options, %w[cocooned-item nested-fields], :'cocooned-item')

content_tag(:div, *args, **options.deep_merge(defaults), &block)
content_tag(name, *args, **options.deep_merge(defaults), &block)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion spec/support/action_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActionViewHelper
def container(string)
Nokogiri::HTML(string).at('div')
Nokogiri::HTML(string).at('body > *')
end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/unit/cocooned/helpers/containers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
let(:template) { ActionView::Base.empty }

shared_examples 'a container helper' do |class_name, attribute_name|
it 'use div as default tag' do
html = method.call { 'any' }
expect(container(html).name).to eq('div')
end

it 'has a default class' do
html = method.call { 'any' }
expect(container(html).attribute('class').value.split).to include(class_name)
Expand All @@ -14,6 +19,11 @@
expect(container(html).attributes.keys).to include(attribute_name)
end

it 'supports custom tag tag' do
html = method.call(:span) { 'any' }
expect(container(html).name).to eq('span')
end

it 'supports additional classes' do
html = method.call(class: 'more') { 'any' }
expect(container(html).attribute('class').value.split).to include(class_name, 'more')
Expand All @@ -23,6 +33,24 @@
html = method.call(data: { attribute: 'value' }) { 'any' }
expect(container(html).attributes.keys).to include(attribute_name, 'data-attribute')
end

context 'with all kind of arguments' do
subject(:parsed) { container(html) }

let(:html) { method.call(:span, class: 'more', data: { attribute: 'value' }) { 'any' } }

it 'uses correct tag' do
expect(parsed.name).to eq('span')
end

it 'has correct classes' do
expect(parsed.attribute('class').value.split).to include(class_name, 'more')
end

it 'has correct data attributes' do
expect(parsed.attributes.keys).to include(attribute_name, 'data-attribute')
end
end
end

describe '#cocooned_container' do
Expand Down

0 comments on commit 3f96bc3

Please sign in to comment.