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

Support to_dom_selector for operation selectors #135

Merged
merged 3 commits into from
Jun 8, 2021
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
13 changes: 12 additions & 1 deletion lib/cable_ready/identifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
module CableReady
module Identifiable
def dom_id(record, prefix = nil)
return record.to_dom_selector if record.respond_to?(:to_dom_selector)

prefix = prefix.to_s.strip if prefix

id = if record.is_a?(ActiveRecord::Relation)
id = if record.respond_to?(:to_dom_id)
record.to_dom_id
elsif record.is_a?(ActiveRecord::Relation)
[prefix, record.model_name.plural].compact.join("_")
elsif record.is_a?(ActiveRecord::Base)
ActionView::RecordIdentifier.dom_id(record, prefix)
Expand All @@ -15,5 +19,12 @@ def dom_id(record, prefix = nil)

"##{id}".squeeze("#").strip
end

def identifiable?(obj)
obj.respond_to?(:to_dom_selector) ||
obj.respond_to?(:to_dom_id) ||
obj.is_a?(ActiveRecord::Relation) ||
obj.is_a?(ActiveRecord::Base)
end
end
end
2 changes: 1 addition & 1 deletion lib/cable_ready/operation_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def add_operation_method(name)
options["selector"] = previous_selector if previous_selector && options.exclude?("selector")
if options.include?("selector")
@previous_selector = options["selector"]
options["selector"] = previous_selector.is_a?(ActiveRecord::Base) || previous_selector.is_a?(ActiveRecord::Relation) ? dom_id(previous_selector) : previous_selector
options["selector"] = identifiable?(previous_selector) ? dom_id(previous_selector) : previous_selector
end
@enqueued_operations[name.to_s] << options
self
Expand Down
22 changes: 22 additions & 0 deletions test/lib/cable_ready/cable_car_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,26 @@ class CableReady::CableCarTest < ActiveSupport::TestCase
CableReady::CableCar.instance.inner_html(selector: "#users", html: "<span>winning</span>").dispatch(clear: false)
assert_equal({"inner_html" => [{"selector" => "#users", "html" => "<span>winning</span>"}]}, CableReady::CableCar.instance.instance_variable_get(:@enqueued_operations))
end

test "selectors should accept any object which respond_to? to_dom_selector" do
CableReady::CableCar.instance.reset!
my_object = Struct.new(:id) do
def to_dom_selector
".#{id}"
end
end.new("users")
dispatch = CableReady::CableCar.instance.inner_html(selector: my_object, html: "<span>winning</span>").dispatch
assert_equal({"innerHtml" => [{"selector" => ".users", "html" => "<span>winning</span>"}]}, dispatch)
end

test "selectors should accept any object which respond_to? to_dom_id" do
CableReady::CableCar.instance.reset!
my_object = Struct.new(:id) do
def to_dom_id
id
end
end.new("users")
dispatch = CableReady::CableCar.instance.inner_html(selector: my_object, html: "<span>winning</span>").dispatch
assert_equal({"innerHtml" => [{"selector" => "#users", "html" => "<span>winning</span>"}]}, dispatch)
end
end