Skip to content

Commit

Permalink
Allow primary_key option on relationships instead of hard coding id (#…
Browse files Browse the repository at this point in the history
…729)

Currently BelongsTo and HasMany default to using #id of the
related records. This will allow you to pass the option of primary_key
and use that fields' value in the form.
  • Loading branch information
npezza93 authored and BenMorganIO committed Jan 12, 2017
1 parent 405cb2f commit 5ebdb1a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/administrate/field/associative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def associated_class
def associated_class_name
options.fetch(:class_name, attribute.to_s.singularize.camelcase)
end

def primary_key
options.fetch(:primary_key, :id)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/administrate/field/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def permitted_attribute

def associated_resource_options
[nil] + candidate_resources.map do |resource|
[display_candidate_resource(resource), resource.id]
[display_candidate_resource(resource), resource.send(primary_key)]
end
end

def selected_option
data && data.id
data && data.send(primary_key)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/administrate/field/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def attribute_key

def associated_resource_options
candidate_resources.map do |resource|
[display_candidate_resource(resource), resource.id]
[display_candidate_resource(resource), resource.send(primary_key)]
end
end

def selected_options
data && data.map(&:id)
data && data.map { |object| object.send(primary_key) }
end

def limit
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/fields/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,33 @@
end
end
end

describe "primary_key option" do
it "determines what primary key is used on the relationship for the form" do
begin
Foo = Class.new
FooDashboard = Class.new
uuid = SecureRandom.uuid
allow(Foo).to receive(:all).and_return([Foo])
allow(Foo).to receive(:uuid).and_return(uuid)
allow(Foo).to receive(:id).and_return(1)
allow_any_instance_of(FooDashboard).to(
receive(:display_resource).and_return(uuid)
)

association =
Administrate::Field::BelongsTo.with_options(
primary_key: "uuid", class_name: "Foo"
)
field = association.new(:customers, [], :show)
field.associated_resource_options

expect(Foo).to have_received(:all)
expect(Foo).to have_received(:uuid)
expect(Foo).not_to have_received(:id)
ensure
remove_constants :Foo, :FooDashboard
end
end
end
end
29 changes: 29 additions & 0 deletions spec/lib/fields/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@
end
end

describe "primary_key option" do
it "determines what primary key is used on the relationship for the form" do
begin
Foo = Class.new
FooDashboard = Class.new
uuid = SecureRandom.uuid
allow(Foo).to receive(:all).and_return([Foo])
allow(Foo).to receive(:uuid).and_return(uuid)
allow(Foo).to receive(:id).and_return(1)
allow_any_instance_of(FooDashboard).to(
receive(:display_resource).and_return(uuid)
)

association =
Administrate::Field::HasMany.with_options(
primary_key: "uuid", class_name: "Foo"
)
field = association.new(:customers, [], :show)
field.associated_resource_options

expect(Foo).to have_received(:all)
expect(Foo).to have_received(:uuid)
expect(Foo).not_to have_received(:id)
ensure
remove_constants :Foo, :FooDashboard
end
end
end

describe "#more_than_limit?" do
it "returns true if record count > limit" do
limit = Administrate::Field::HasMany::DEFAULT_LIMIT
Expand Down

0 comments on commit 5ebdb1a

Please sign in to comment.