From fc45f8f86ca935e8296206e91a8f727ba66a2456 Mon Sep 17 00:00:00 2001 From: iarie Date: Wed, 19 Apr 2017 12:08:36 +0300 Subject: [PATCH] Requested changes: - add fetch default foreign_key to Field::BelongsTo - style fixes and specs --- lib/administrate/field/associative.rb | 2 +- lib/administrate/field/belongs_to.rb | 2 +- spec/lib/fields/belongs_to_spec.rb | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/administrate/field/associative.rb b/lib/administrate/field/associative.rb index 9ea2cc3127..805ea6671f 100644 --- a/lib/administrate/field/associative.rb +++ b/lib/administrate/field/associative.rb @@ -26,7 +26,7 @@ def primary_key end def foreign_key - options.fetch(:foreign_key, nil) + options.fetch(:foreign_key, :"#{attribute}_id") end end end diff --git a/lib/administrate/field/belongs_to.rb b/lib/administrate/field/belongs_to.rb index 5a7b42a0e0..a65725265b 100644 --- a/lib/administrate/field/belongs_to.rb +++ b/lib/administrate/field/belongs_to.rb @@ -8,7 +8,7 @@ def self.permitted_attribute(attr) end def permitted_attribute - foreign_key || self.class.permitted_attribute(attribute) + foreign_key end def associated_resource_options diff --git a/spec/lib/fields/belongs_to_spec.rb b/spec/lib/fields/belongs_to_spec.rb index 4be803c24d..58d80758cd 100644 --- a/spec/lib/fields/belongs_to_spec.rb +++ b/spec/lib/fields/belongs_to_spec.rb @@ -25,8 +25,9 @@ Foo = Class.new allow(Foo).to receive(:all).and_return([]) - association = Administrate::Field::BelongsTo. - with_options(class_name: "Foo") + association = Administrate::Field::BelongsTo.with_options( + class_name: "Foo", + ) field = association.new(:customers, [], :show) candidates = field.associated_resource_options @@ -71,10 +72,11 @@ it "determines what foreign key is used on the relationship for the form" do association = Administrate::Field::BelongsTo.with_options( - foreign_key: "customers_uuid", + foreign_key: "foo_uuid", class_name: "Foo", ) field = association.new(:customers, [], :show) - expect(field.permitted_attribute).to eq("customers_uuid") + permitted_attribute = field.permitted_attribute + expect(permitted_attribute).to eq("foo_uuid") end end end