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
fixes remi#513
  • Loading branch information
keegangroth committed Oct 20, 2018
1 parent 417fbc1 commit 223abdc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
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
63 changes: 55 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,40 @@
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 details in active_model_serializers format" do
before do
spawn_model "Foo::User" do
Expand Down

0 comments on commit 223abdc

Please sign in to comment.