Support to_dom_selector for operation selectors#135
Support to_dom_selector for operation selectors#135leastbad merged 3 commits intostimulusreflex:masterfrom
to_dom_selector for operation selectors#135Conversation
|
Thank you, Jared! This is awesome. I cannot fathom why we wouldn't accept this concept for inclusion in v5. We do need to rework this so that it happens inside of
The other consideration is that the original intention of I'd like to propose that we add a method to def identifiable?(obj)
obj.is_a?(ActiveRecord::Relation) || obj.is_a?(ActiveRecord::Base) || obj.respond_to?(:to_dom_selector)
endThis would allow us to change line 35 of options["selector"] = identifiable?(previous_selector) ? dom_id(previous_selector) : previous_selectorAnd that would allow us to add the def dom_id(record, prefix = nil)
prefix = prefix.to_s.strip if prefix
id = if record.respond_to?(:to_dom_selector)
record.to_dom_selector
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)
else
[prefix, record.to_s.strip].compact.join("_")
end
"##{id}".squeeze("#").strip
endThis also solves the What do you think? I should be fine with the docs ( |
|
Makes sense to consolidate inside of
in the case of that being available. I don't think assuming it must be an actual id is warranted. Could be any selector. |
|
stares at Jared's comment for 10-12 minutes before replying I feel like I get where you're coming from, and I thought about ways we could pull this off. For example, what if However, the function is dom_id, and the folks calling it expect a fully formed #id to emerge. They are, after all, already able to pass any selector string they like. That means that instead of making The nice thing about this approach is that while it's slightly less terse and magical, it still checks your box for the object being responsible for where it is sent to. I'm open to exploring either of these paths, or a third! |
|
Would love additional input on this…I guess my dilemma is I've personally used the original Rails' Anyway, if we do keep using IDs only, I'd recommend changing the method name to |
|
We're going to have to agree to disagree on a number of points, because we're not looking to move away from using Similarly, we're not looking to make a break from Good news, though: I have a third path to propose and I think that you'll like it. (I just realized that you probably meant changing the name of Why not support both? It's fair to say that we want this to work for as many use cases as possible: def dom_id(record, prefix = nil)
prefix = prefix.to_s.strip if prefix
id = if record.respond_to?(:to_dom_selector)
return record.to_dom_selector
elsif 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)
else
[prefix, record.to_s.strip].compact.join("_")
end
"##{id}".squeeze("#").strip
endI can sell that! What do you think? |
|
Seems perfectly reasonable to me! I'll redo the PR and see what other feedback we can collect. |
|
Thanks, Jared... this looks great! I am trying to figure out why the test suite is giving us a Do you see anything weird? Did you run |
Strange… All seems a-OK on my side |
|
@leastbad Wow, so apparently earlier versions of Ruby wouldn't allow you to Anyway we might want to redo that |
|
Nice catch! Also: I hate this kind of thing. Also: I can't believe that I've never hit this case before. Even though I wish we could bump to >=2.7, I don't think that this is a strong enough justification. In your opinion, what's the least fugly way to set a flag around line 9 and then exit with the value around line 19 if the flag is true? |
|
I switched it out to a guard statement up top. Let me know what you think :) |
|
Duh, of course! I think it's great. Let me make sure the others agree. |
hopsoft
left a comment
There was a problem hiding this comment.
I like what this accomplishes; though, I am a little wary of mixing dom_selector semantics within the dom_id implementation. Having said that, advanced users should know what they're doing when they implement to_dom_selector for use with CableReady.
|
Sorry for being late to the party! This looks awesome and I love where this is heading! I have one thought to add: if an object defines both |
|
You're right that in the current implementation, dom selector is plucked first. The fuzzy reasoning behind this was to handle the odd case first, and then treat every other branch in the What are the pros and cons of swapping the priority from selector to id? |
Resolves #134
FYI, I added
to_sonto the final path for the updated selector logic, but that might be a breaking change (since previously a non-String object would be converted viato_json, notto_s). There's also the outstanding question of whether we need to issue a warning or exception if something mangled gets through like an inspect string with class name, etc.Happy to work on updated docs as well, just let me know.