Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsbnRecordモデルを追加 #1816

Merged
merged 12 commits into from
Aug 18, 2023
4 changes: 2 additions & 2 deletions app/helpers/manifestations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,11 @@ def rdf_statement(manifestation)
RDF::Literal.new(manifestation.note)
)

manifestation.identifier_contents(:isbn).each do |i|
manifestation.isbn_records.each do |i|
graph << RDF::Statement.new(
subject,
nextl.isbn,
RDF::Literal.new(i)
RDF::Literal.new(i.body)
)
end

Expand Down
9 changes: 2 additions & 7 deletions app/models/concerns/enju_loc/enju_manifestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ def self.import_record_from_loc(doc)
serial: is_serial
)
identifier = {}
if isbn
identifier[:isbn] = Identifier.new(
manifestation: manifestation,
body: isbn,
identifier_type: IdentifierType.find_by(name: 'isbn') || IdentifierType.create!(name: 'isbn')
)
end
if loc_identifier
identifier[:loc_identifier] = Identifier.new(
manifestation: manifestation,
Expand Down Expand Up @@ -145,13 +138,15 @@ def self.import_record_from_loc(doc)
identifier_type: IdentifierType.find_by(name: 'issn_l') || IdentifierType.create!(name: 'issn_l')
)
end

manifestation.carrier_type = carrier_type if carrier_type
manifestation.manifestation_content_type = content_type if content_type
manifestation.frequency = frequency if frequency
manifestation.save!
identifier.each do |k, v|
manifestation.identifiers << v if v.valid?
end
manifestation.isbn_records.create(body: isbn) if isbn.present?
manifestation.publishers << publisher_agents
manifestation.creators << creator_agents
create_loc_subject_related_elements(doc, manifestation)
Expand Down
6 changes: 2 additions & 4 deletions app/models/concerns/enju_ndl/enju_manifestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ def import_record(doc)
)
manifestation.serial = true if is_serial
identifier = {}
if isbn
identifier[:isbn] = Identifier.new(body: isbn)
identifier[:isbn].identifier_type = IdentifierType.find_by(name: 'isbn') || IdentifierType.create!(name: 'isbn')
end
if iss_itemno
identifier[:iss_itemno] = Identifier.new(body: iss_itemno)
identifier[:iss_itemno].identifier_type = IdentifierType.find_by(name: 'iss_itemno') || IdentifierType.create!(name: 'iss_itemno')
Expand All @@ -167,6 +163,8 @@ def import_record(doc)
manifestation.carrier_type = carrier_type if carrier_type
manifestation.manifestation_content_type = content_type if content_type
if manifestation.save
manifestation.isbn_records.find_or_create_by(body: isbn) if isbn.present?

identifier.each do |_k, v|
manifestation.identifiers << v if v.valid?
end
Expand Down
6 changes: 1 addition & 5 deletions app/models/concerns/enju_nii/enju_manifestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ def import_record_from_cinii_books(doc)
identifier_type_ncid = IdentifierType.find_or_create_by!(name: 'ncid')
identifier[:ncid].identifier_type = identifier_type_ncid
end
if isbn
identifier[:isbn] = Identifier.new(body: isbn)
identifier_type_isbn = IdentifierType.find_or_create_by!(name: 'isbn')
identifier[:isbn].identifier_type = identifier_type_isbn
end
identifier.each do |k, v|
manifestation.identifiers << v
end
Expand All @@ -88,6 +83,7 @@ def import_record_from_cinii_books(doc)
if manifestation.valid?
Agent.transaction do
manifestation.save!
manifestation.isbn_records.create(body: isbn) if isbn.present?
create_cinii_series_statements(doc, manifestation)
publisher_patrons = Agent.import_agents(publishers)
creator_patrons = Agent.import_agents(creators)
Expand Down
49 changes: 49 additions & 0 deletions app/models/isbn_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class IsbnRecord < ApplicationRecord
has_many :isbn_record_and_manifestations, dependent: :destroy
has_many :manifestations, through: :isbn_record_and_manifestations
before_save :normalize_isbn
validates :body, presence: true
strip_attributes

def self.new_records(isbn_records_params)
return [] unless isbn_records_params
isbn_records = []
IsbnRecord.transaction do
isbn_records_params.each do |k, v|
next if v['_destroy'] == '1'
if v['body'].present?
isbn_record = IsbnRecord.where(body: Lisbn.new(v['body'].gsub(/[^0-9x]/i, '')).isbn13).first_or_create!
elsif v['id'].present?
isbn_record = IsbnRecord.find(v['id'])
else
v.delete('_destroy')
isbn_record = IsbnRecord.create(v)
end
isbn_records << isbn_record
end
end
isbn_records
end

def normalize_isbn
if StdNum::ISBN.valid?(body)
self.body = StdNum::ISBN.normalize(body)
else
errors.add(:body)
end
end

def valid_isbn?
StdNum::ISBN.valid?(body)
end
end

# == Schema Information
#
# Table name: isbn_records
#
# id :bigint not null, primary key
# body :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
16 changes: 16 additions & 0 deletions app/models/isbn_record_and_manifestation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class IsbnRecordAndManifestation < ApplicationRecord
belongs_to :isbn_record
belongs_to :manifestation
validates :isbn_record_id, uniqueness: {scope: :manifestation_id}
end

# == Schema Information
#
# Table name: isbn_record_and_manifestations
#
# id :bigint not null, primary key
# isbn_record_id :bigint not null
# manifestation_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
16 changes: 8 additions & 8 deletions app/models/manifestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Manifestation < ApplicationRecord
has_one :periodical_record, class_name: 'Periodical', dependent: :destroy
has_one :periodical_and_manifestation, dependent: :destroy
has_one :periodical, through: :periodical_and_manifestation, dependent: :destroy
has_many :isbn_record_and_manifestations, dependent: :destroy
has_many :isbn_records, through: :isbn_record_and_manifestations
has_one :doi_record, dependent: :destroy

accepts_nested_attributes_for :creators, allow_destroy: true, reject_if: :all_blank
Expand All @@ -40,6 +42,7 @@ class Manifestation < ApplicationRecord
accepts_nested_attributes_for :series_statements, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :identifiers, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :manifestation_custom_values, reject_if: :all_blank
accepts_nested_attributes_for :isbn_records, reject_if: :all_blank

searchable do
text :title, default_boost: 2 do
Expand Down Expand Up @@ -192,7 +195,7 @@ class Manifestation < ApplicationRecord
end
end
text :identifier do
other_identifiers = identifiers.joins(:identifier_type).merge(IdentifierType.where.not(name: [:isbn, :issn]))
other_identifiers = identifiers.joins(:identifier_type).merge(IdentifierType.where.not(name: [:issn]))
other_identifiers.pluck(:body)
end
string :sort_title
Expand Down Expand Up @@ -427,10 +430,7 @@ def sort_title
end

def self.find_by_isbn(isbn)
identifier_type = IdentifierType.find_by(name: 'isbn')
return nil unless identifier_type

Manifestation.includes(identifiers: :identifier_type).where("identifiers.body": isbn, "identifier_types.name": 'isbn')
IsbnRecord.find_by(body: isbn)&.manifestations
end

def index_series_statement
Expand Down Expand Up @@ -574,7 +574,7 @@ def to_hash(role: 'Guest')
content_type: manifestation_content_type.name,
frequency: frequency.name,
language: language.name,
isbn: identifier_contents(:isbn).join('//'),
isbn: isbn_records.pluck(:body).join('//'),
issn: identifier_contents(:issn).join('//'),
volume_number: volume_number,
volume_number_string: volume_number_string,
Expand All @@ -599,7 +599,7 @@ def to_hash(role: 'Guest')
}

IdentifierType.find_each do |type|
next if ['issn', 'isbn', 'jpno', 'ncid', 'lccn', 'doi'].include?(type.name.downcase.strip)
next if ['isbn', 'issn', 'jpno', 'ncid', 'lccn', 'doi'].include?(type.name.downcase.strip)

record[:"identifier:#{type.name.to_sym}"] = identifiers.where(identifier_type: type).pluck(:body).join('//')
end
Expand Down Expand Up @@ -678,7 +678,7 @@ def root_series_statement
end

def isbn_characters
identifier_contents(:isbn).map{|i|
isbn_records.pluck(:body).map{|i|
isbn10 = isbn13 = isbn10_dash = isbn13_dash = nil
isbn10 = Lisbn.new(i).isbn10
isbn13 = Lisbn.new(i).isbn13
Expand Down
3 changes: 1 addition & 2 deletions app/models/resource_import_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def import
if row['isbn'].present?
if StdNum::ISBN.valid?(row['isbn'])
isbn = StdNum::ISBN.normalize(row['isbn'])
identifier_type_isbn = IdentifierType.find_or_create_by!(name: 'isbn')
m = Identifier.find_by(body: isbn, identifier_type_id: identifier_type_isbn.id).try(:manifestation)
m = IsbnRecord.find_by(body: isbn)&.manifestations&.first
if m
if m.series_statements.exists?
manifestation = m
Expand Down
2 changes: 1 addition & 1 deletion app/views/items/index.text.ruby
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ csv = %w[title isbn item_identifier call_number created_at].to_csv(col_sep: "\t"
@items.map{|item|
[
item.manifestation.original_title,
item.manifestation.identifier_contents(:isbn).join("; "),
item.manifestation.isbn_records.pluck(:body).join("; "),
item.item_identifier,
item.call_number,
item.created_at
Expand Down
10 changes: 10 additions & 0 deletions app/views/manifestations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@
<%= f.select(:language_id, @languages.collect{|language| [language.display_name.localize, language.id]}) -%>
</div>

<div class="field">
<%= f.label :isbn_records -%><br />
<%= f.fields_for :isbn_records do |isbn_record_form| %>
<%= render 'isbn_record_fields', f: isbn_record_form %>
<% end %>
<div class="links">
<p><%= link_to_add_association t('page.add'), f, :isbn_records %></p>
</div>
</div>

<div class="field">
<%= f.label :identifier -%><br />
<%= f.fields_for :identifiers do |identifier_form| %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/manifestations/_google_book_thumbnail.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% unless manifestation.identifier_contents(:isbn).empty? %>
<% unless manifestation.isbn_records.empty? %>
<div id="gbsthumbnail<%= manifestation.id %>" class="book_jacket"></div>
<script type="text/javascript">
gbs_addTheCover( '<%= manifestation.identifier_contents(:isbn).first %>', 'gbsthumbnail<%= manifestation.id %>' );
gbs_addTheCover( '<%= manifestation.isbn_records.first.body %>', 'gbsthumbnail<%= manifestation.id %>' );
</script>
<% else %>
<% if @library_group.settings[:book_jacket_unknown_resource] %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/manifestations/_hanmotocom_thumbnail.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% unless manifestation.identifier_contents(:isbn).empty? %>
<% unless manifestation.isbn_records.empty? %>
<div id="gbsthumbnail<%= manifestation.id %>" class="book_jacket"></div>
<%= image_tag("https://www.hanmoto.com/bd/img/#{ Lisbn.new(manifestation.identifier_contents(:isbn).first).isbn13 }.jpg") %>
<%= image_tag("https://www.hanmoto.com/bd/img/#{ Lisbn.new(manifestation.isbn_records.first.body).isbn13 }.jpg") %>
<% else %>
<% if @library_group.settings[:book_jacket_unknown_resource] %>
<%= @library_group.settings[:book_jacket_unknown_resource] %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/manifestations/_isbn_record_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="nested-fields">
<%= f.text_field :body %>
<%= link_to_remove_association t('page.remove'), f, data: {confirm: t('page.are_you_sure')} %><br />
</div>
4 changes: 2 additions & 2 deletions app/views/manifestations/_openbd_thumbnail.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% unless manifestation.identifier_contents(:isbn).empty? %>
<% unless manifestation.isbn_records.empty? %>
<div id="openbd_thumbnail<%= manifestation.id %>" class="book_jacket"></div>
<script type="text/javascript">
openbd_addTheCover( '<%= manifestation.identifier_contents(:isbn).first %>', 'openbd_thumbnail<%= manifestation.id %>' );
openbd_addTheCover( '<%= manifestation.isbn_records.first.body %>', 'openbd_thumbnail<%= manifestation.id %>' );
</script>
<% else %>
<% if @library_group.settings[:book_jacket_unknown_resource] %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/manifestations/_show.mods.builder
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ if defined?(EnjuSubject)
end
xml.abstract manifestation.description
xml.note manifestation.note
manifestation.identifier_contents(:isbn).each do |i|
xml.identifier i, type: 'isbn'
manifestation.isbn_records.each do |i|
xml.identifier i.body, type: 'isbn'
end
manifestation.identifier_contents(:lccn).each do |l|
xml.identifier l, type: 'lccn'
Expand Down
4 changes: 2 additions & 2 deletions app/views/manifestations/index.rss.builder
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ xml.rss('version' => "2.0",
xml.pubDate manifestation.date_of_publication.try(:utc).try(:rfc822)
xml.link manifestation_url(manifestation)
xml.guid manifestation_url(manifestation), isPermaLink: "true"
manifestation.identifier_contents(:isbn).each do |i|
xml.tag! "dc:identifier", i
manifestation.isbn_records.each do |i|
xml.tag! "dc:identifier", i.body
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/manifestations/show.html+phone.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<table class="table table-striped">
<tr>
<th>ISBN</th>
<td><%= @manifestation.identifier_contents(:isbn).join(' ') %></td>
<td><%= @manifestation.isbn_records.pluck(:body).join(' ') %></td>
</tr>
<tr>
<th><%= t('activerecord.attributes.manifestation.price') %></th>
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifier/manifestation_info.en.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Title: <%= @manifestation.original_title %>
Creator(s): <%= @manifestation.creators.readable_by(@user).collect(&:full_name).join("/") %>
Contributor(s): <%= @manifestation.contributors.readable_by(@user).collect(&:full_name).join("/") %>
Publisher(s): <%= @manifestation.publishers.readable_by(@user).collect(&:full_name).join("/") %>
ISBN: <%= @manifestation.identifier_contents(:isbn).join(" ") %>
ISBN: <%= @manifestation.isbn_records.pluck(:body).join(" ") %>
URL: <%= @manifestation.access_address %>
Holding:
<% @items.on_shelf.each do |item| -%>
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifier/manifestation_info.ja.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
著者: <%= @manifestation.creators.readable_by(@user).collect(&:full_name).join("/") %>
編者: <%= @manifestation.contributors.readable_by(@user).collect(&:full_name).join("/") %>
出版者: <%= @manifestation.publishers.readable_by(@user).collect(&:full_name).join("/") %>
ISBN: <%= @manifestation.identifier_contents(:isbn).join(" ") %>
ISBN: <%= @manifestation.isbn_records.pluck(:body).join(" ") %>
URL: <%= @manifestation.access_address %>
所蔵場所:
<% @items.on_shelf.each do |item| -%>
Expand Down
2 changes: 1 addition & 1 deletion app/views/reserves/index.text.ruby
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CSV.generate(col_sep: "\t", row_sep: "\r\n") do |csv|
reserve.manifestation.creators.pluck(:full_name).join('//'),
reserve.manifestation.publishers.pluck(:full_name).join('//'),
reserve.manifestation.pub_date,
reserve.manifestation.identifier_contents(:isbn).join('//'),
reserve.manifestation.isbn_records.pluck(:body).join('//'),
reserve.item.try(:item_identifier),
reserve.item.try(:call_number),
reserve.user.profile.try(:user_number),
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20160506144040_create_isbn_records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateIsbnRecords < ActiveRecord::Migration[6.1]
def change
create_table :isbn_records, comment: 'ISBN' do |t|
t.string :body, null: false, comment: 'ISBN'

t.timestamps
end

add_index :isbn_records, :body
end
end
12 changes: 12 additions & 0 deletions db/migrate/20170116134120_create_isbn_record_and_manifestations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateIsbnRecordAndManifestations < ActiveRecord::Migration[6.1]
def change
create_table :isbn_record_and_manifestations, comment: '書誌とISBNの関係' do |t|
t.references :isbn_record, foreign_key: true, null: false
t.references :manifestation, foreign_key: true, null: false, index: false

t.timestamps
end

add_index :isbn_record_and_manifestations, [:manifestation_id, :isbn_record_id], unique: true, name: 'index_isbn_record_and_manifestations_on_manifestation_id'
end
end
Loading