Skip to content
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
2 changes: 1 addition & 1 deletion lib/administrate/field/polymorphic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.permitted_attribute(attr, _options = {})

def associated_resource_grouped_options
classes.map do |klass|
[klass.to_s, candidate_resources_for(klass).map do |resource|
[klass.model_name.human, candidate_resources_for(klass).map do |resource|
[display_candidate_resource(resource), resource.to_global_id]
end]
end
Expand Down
60 changes: 60 additions & 0 deletions spec/lib/fields/polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "administrate/field/polymorphic"
require "support/constant_helpers"
require "support/field_matchers"
require "ostruct"

describe Administrate::Field::Polymorphic do
include FieldMatchers
Expand Down Expand Up @@ -99,4 +100,63 @@ def display_resource(*)
end
end
end

describe "#associated_resource_grouped_options" do
it "returns grouped options with display names and global IDs" do
Comment = Class.new
instance = described_class.new(:comment, Comment.new, :show, resource: {})

Article = Class.new do
def self.model_name
OpenStruct.new(human: "Article")
end
end

Recipe = Class.new do
def self.model_name
OpenStruct.new(human: "Recipe")
end
end

resource1 = double("Hello World", to_global_id: "gid://app/Article/1")
resource2 = double("Today's journal", to_global_id: "gid://app/Article/2")
resource3 = double("Lasagna", to_global_id: "gid://app/Recipe/3")

allow(instance).to receive(:classes)
.and_return([Article, Recipe])
allow(instance).to receive(:candidate_resources_for)
.with(Article)
.and_return([resource1, resource2])
allow(instance).to receive(:candidate_resources_for)
.with(Recipe)
.and_return([resource3])
allow(instance).to receive(:display_candidate_resource)
.with(resource1)
.and_return("Hello World")
allow(instance).to receive(:display_candidate_resource)
.with(resource2)
.and_return("Today's journal")
allow(instance).to receive(:display_candidate_resource)
.with(resource3)
.and_return("Lasagna")

result = instance.associated_resource_grouped_options

expected_result = [
["Article",
[
["Hello World", "gid://app/Article/1"],
["Today's journal", "gid://app/Article/2"]
]],
["Recipe",
[
["Lasagna", "gid://app/Recipe/3"]
]]
]

expect(result).to eq(expected_result)
ensure
remove_constants :Comment, :Artcile, :Recipe
end
end
end