Skip to content

Commit

Permalink
Requested changes:
Browse files Browse the repository at this point in the history
 - add fetch default foreign_key to Field::BelongsTo
 - style fixes and specs
  • Loading branch information
iarie committed Apr 21, 2017
1 parent 2e486d3 commit fc45f8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/administrate/field/associative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/administrate/field/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions spec/lib/fields/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit fc45f8f

Please sign in to comment.