Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions rails/app/graphql/types/work_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def topics
object.topics
end

field :primary_location, Types::SourceType
def primary_location
object.works_primary_location
end

field :is_oa, Boolean
def is_oa
object.works_open_access.is_oa
Expand Down
8 changes: 8 additions & 0 deletions rails/app/models/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ class Source < ApplicationRecord
has_many :sources_counts_by_year,
primary_key: :source_openalex_id,
foreign_key: :source_openalex_id

has_many :works_primary_locations,
primary_key: :source_openalex_id,
foreign_key: :source_openalex_id

has_many :works,
through: :works_primary_locations

end
8 changes: 7 additions & 1 deletion rails/app/models/works_primary_location.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# frozen_string_literal: true

class WorksPrimaryLocation < ApplicationRecord
belongs_to :work
belongs_to :work,
primary_key: :work_openalex_id,
foreign_key: :work_openalex_id

belongs_to :source,
primary_key: :source_openalex_id,
foreign_key: :source_openalex_id
end
23 changes: 23 additions & 0 deletions rails/spec/graphql/queries/work_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
)
end

@source = FactoryBot.create(:source)

WorksPrimaryLocation.create(
work_openalex_id: @work.work_openalex_id,
source_openalex_id: @source.source_openalex_id
)

@referenced_works = []
2.times { @referenced_works << FactoryBot.create(:article) }
2.times { @referenced_works << FactoryBot.create(:dataset) }
Expand Down Expand Up @@ -96,6 +103,9 @@
topics {
id
}
primaryLocation {
id
}
}
}
GRAPHQL
Expand Down Expand Up @@ -265,6 +275,19 @@
expect(result['data']['workByDoi']['topics'].count).to eq(5)
end

it 'retrieves a work with primary location association' do
result =
BookWormSchema.execute(
@work_by_doi_query_with_associations,
variables: {
doi: @work.doi
}
)

expect(result['data']['workByDoi']['primaryLocation']).to be_a(Hash)
expect(result['data']['workByDoi']['primaryLocation'].keys).to eq(["id"])
end

it 'retrieves a work with ID and biblio information' do
result =
BookWormSchema.execute(
Expand Down
10 changes: 10 additions & 0 deletions rails/spec/models/source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Source, type: :model do
describe 'relationships' do
it { should have_many(:works_primary_locations) }
it { should have_many(:works).through(:works_primary_locations) }
end
end
10 changes: 10 additions & 0 deletions rails/spec/models/works_primary_location_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe WorksPrimaryLocation, type: :model do
describe 'relationships' do
it { should belong_to :work }
it { should belong_to :source }
end
end