Skip to content

Commit

Permalink
belongs_to association should respect association's collection_path (#…
Browse files Browse the repository at this point in the history
…514)

* belongs_to association should respect association's collection_path

fixes #513
  • Loading branch information
keegangroth authored and zacharywelch committed Feb 13, 2019
1 parent 8a0d873 commit 983b528
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/her/model/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def has_one(name, opts = {})
# @param [Hash] opts Options
# @option opts [String] :class_name The name of the class to map objects to
# @option opts [Symbol] :data_key The attribute where the data is stored
# @option opts [Path] :path The relative path where to fetch the data (defaults to `/{class_name}.pluralize/{id}`)
# @option opts [Path] :path The relative path where to fetch the data
# @option opts [Symbol] :foreign_key The foreign key used to build the `:id` part of the path (defaults to `{name}_id`)
#
# @example
Expand Down
3 changes: 1 addition & 2 deletions lib/her/model/associations/belongs_to_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def self.attach(klass, name, opts)
:name => name,
:data_key => name,
:default => nil,
:foreign_key => "#{name}_id",
:path => "/#{name.to_s.pluralize}/:id"
:foreign_key => "#{name}_id"
}.merge(opts)
klass.associations[:belongs_to] << opts

Expand Down
97 changes: 89 additions & 8 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,39 @@
data_key: :organization,
default: nil,
class_name: "Organization",
foreign_key: "organization_id",
path: "/organizations/:id"
foreign_key: "organization_id"
}
end
before { Foo::User.belongs_to :organization }

it { is_expected.to eql [organization_association] }
end

context "multiple" do
context "specifying non-default path" do
let(:path) { 'my_special_path' }
let(:organization_association) do
{
name: :organization,
data_key: :organization,
default: nil,
class_name: "Organization",
foreign_key: "organization_id",
path: "/organizations/:id"
path: path
}
end
before { Foo::User.belongs_to :organization, path: path }

it { is_expected.to eql [organization_association] }
end

context "multiple" do
let(:organization_association) do
{
name: :organization,
data_key: :organization,
default: nil,
class_name: "Organization",
foreign_key: "organization_id"
}
end
let(:family_association) do
Expand All @@ -138,8 +153,7 @@
data_key: :family,
default: nil,
class_name: "Family",
foreign_key: "family_id",
path: "/families/:id"
foreign_key: "family_id"
}
end
before do
Expand Down Expand Up @@ -216,8 +230,7 @@
data_key: :org,
default: true,
class_name: "Business",
foreign_key: "org_id",
path: "/organizations/:id"
foreign_key: "org_id"
}
end
before do
Expand Down Expand Up @@ -527,6 +540,74 @@
end
end

context "handling associations with collection_path" do
before do
spawn_model "Foo::Organization" do
has_many :users
parse_root_in_json true
collection_path '/special/organizations'
end
spawn_model "Foo::User" do
belongs_to :organization
end
end

context "without included data" do
before(:context) do
Her::API.setup url: "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/2") { [200, {}, { id: 2, name: "Lindsay Fünke", organization_id: 2 }.to_json] }
stub.get("/special/organizations/2") { [200, {}, { organization: { id: 2, name: "Bluth Company" } }.to_json] }
end
end
end

let(:user) { Foo::User.find(2) }

it "fetches data that was not included through belongs_to" do
expect(user.organization).to be_a(Foo::Organization)
expect(user.organization.id).to eq(2)
expect(user.organization.name).to eq("Bluth Company")
end
end
end

context "handling associations with path_prefix" do
before do
spawn_model "Foo::Organization" do
has_many :users
parse_root_in_json true
end
spawn_model "Foo::User" do
belongs_to :organization
end
end

context "without included data" do
before(:context) do
Her::API.setup url: "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.path_prefix = 'special'
builder.adapter :test do |stub|
stub.get("/special/users/2") { [200, {}, { id: 2, name: "Lindsay Fünke", organization_id: 2 }.to_json] }
stub.get("/special/organizations/2") { [200, {}, { organization: { id: 2, name: "Bluth Company" } }.to_json] }
end
end
end

let(:user) { Foo::User.find(2) }

it "fetches data that was not included through belongs_to" do
expect(user.organization).to be_a(Foo::Organization)
expect(user.organization.id).to eq(2)
expect(user.organization.name).to eq("Bluth Company")
end
end
end

context "handling associations with details in active_model_serializers format" do
before do
spawn_model "Foo::User" do
Expand Down

0 comments on commit 983b528

Please sign in to comment.