Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data attributes from html options #47

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/futurism/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,32 @@ def futurize_active_record(records, extends:, placeholder:, **options)
class Element
include ActionView::Helpers

attr_reader :extends, :placeholder, :html_options, :model, :options
attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options

def initialize(extends:, placeholder:, options:)
@extends = extends
@placeholder = placeholder
@html_options = options.delete(:html_options) || {}
@data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params)
@model = options.delete(:model)
@options = options
@options = data_attributes.any? ? options.merge(data: data_attributes) : options
end

def dataset
{
data_attributes.merge({
signed_params: signed_params,
sgid: model && model.to_sgid.to_s
}
})
end

def render
case extends
when :li
content_tag :li, placeholder, {data: dataset, is: "futurism-li"}.merge(html_options)
content_tag :li, placeholder, html_options.deep_merge({data: dataset, is: "futurism-li"})
when :tr
content_tag :tr, placeholder, {data: dataset, is: "futurism-table-row"}.merge(html_options)
content_tag :tr, placeholder, html_options.deep_merge({data: dataset, is: "futurism-table-row"})
else
content_tag :"futurism-element", placeholder, {data: dataset}.merge(html_options)
content_tag :"futurism-element", placeholder, html_options.deep_merge({data: dataset})
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/helper/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class Futurism::HelperTest < ActionView::TestCase
assert_equal "flex justify-center", element.children.first["class"]
end

test "ensures signed_params and sgid are not overwritable" do
post = Post.create title: "Lorem"

element = Nokogiri::HTML.fragment(futurize(post, extends: :div, html_options: {data: {controller: "test", sgid: "test", signed_params: "test"}}) {})

assert_equal post, GlobalID::Locator.locate_signed(element.children.first["data-sgid"])
assert_equal signed_params({data: {controller: "test"}}), element.children.first["data-signed-params"]
end

def signed_params(params)
Rails.application.message_verifier("futurism").generate(params)
end
Expand Down