diff --git a/lib/administrate/field/belongs_to.rb b/lib/administrate/field/belongs_to.rb index befe5b68938de1047f39b3b0aaedaee380ec04b4..3053e95ac8f519108ac0648c651524c3de9e29d0 100644 --- a/lib/administrate/field/belongs_to.rb +++ b/lib/administrate/field/belongs_to.rb @@ -24,7 +24,11 @@ module Administrate private def candidate_resources - associated_class.all + associated_class.public_send candidate_scope + end + + def candidate_scope + options.fetch(:scope, :all) end def display_candidate_resource(resource) diff --git a/spec/lib/fields/belongs_to_spec.rb b/spec/lib/fields/belongs_to_spec.rb index ee1eb2143d8f4e1d67ce04ae580da8b8da76c003..0deae1ff5587c2d9cb3640e6e2a288532f6477e4 100644 --- a/spec/lib/fields/belongs_to_spec.rb +++ b/spec/lib/fields/belongs_to_spec.rb @@ -37,4 +37,26 @@ describe Administrate::Field::BelongsTo do end end end + + describe "scope option" do + it "determines which method is called to find candidate resources" do + begin + Foo = Class.new + allow(Foo).to receive(:all).and_return([]) + allow(Foo).to receive(:widgets).and_return([]) + + association = Administrate::Field::BelongsTo. + with_options(class_name: "Foo", scope: :widgets) + field = association.new(:customers, [], :show) + candidates = field.associated_resource_options + + expect(Foo).not_to have_received(:all) + expect(Foo).to have_received(:widgets) + expect(candidates).to eq([nil]) + ensure + remove_constants :Foo + end + end + + end end