Class: Manifestation
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Manifestation
- Defined in:
- app/models/manifestation.rb
Instance Attribute Summary collapse
-
#during_import ⇒ Object
Returns the value of attribute during_import.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
Class Method Summary collapse
- .cached_numdocs ⇒ Object
- .csv_header(role, options = {col_sep: "\t", role: :Guest}) ⇒ Object
- .export(options = {format: :txt, role: :Guest}) ⇒ Object
- .find_by_isbn(isbn) ⇒ Object
-
.pickup(keyword = nil, current_user = nil) ⇒ Object
TODO: よりよい推薦方法.
Instance Method Summary collapse
- #acquired_at ⇒ Object
- #clear_cached_numdocs ⇒ Object
- #contributor ⇒ Object
- #created(agent) ⇒ Object
- #creator ⇒ Object
- #extract_text ⇒ Object
- #extract_text! ⇒ Object
- #first_issue ⇒ Object
- #identifier_contents(name) ⇒ Object
- #index_series_statement ⇒ Object
- #isbn_characters ⇒ Object
- #latest_issue ⇒ Object
- #number_of_pages ⇒ Object
- #parent_of_series ⇒ Object
- #produced(agent) ⇒ Object
- #pub_dates ⇒ Object
- #publisher ⇒ Object
- #realized(agent) ⇒ Object
- #root_series_statement ⇒ Object
- #series_master? ⇒ Boolean
- #set_agent_role_type(agent_lists, options = {scope: :creator}) ⇒ Object
- #set_date_of_publication ⇒ Object
- #set_number ⇒ Object
- #sort_title ⇒ Object
- #title ⇒ Object
- #titles ⇒ Object
- #to_csv(options = {format: :txt, role: :Guest}) ⇒ Object
- #url ⇒ Object
- #web_item ⇒ Object
Instance Attribute Details
#during_import ⇒ Object
Returns the value of attribute during_import.
255 256 257 |
# File 'app/models/manifestation.rb', line 255 def during_import @during_import end |
#parent_id ⇒ Object
Returns the value of attribute parent_id.
255 256 257 |
# File 'app/models/manifestation.rb', line 255 def parent_id @parent_id end |
Class Method Details
.cached_numdocs ⇒ Object
300 301 302 |
# File 'app/models/manifestation.rb', line 300 def self.cached_numdocs Rails.cache.fetch("manifestation_search_total"){Manifestation.search.total} end |
.csv_header(role, options = {col_sep: "\t", role: :Guest}) ⇒ Object
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'app/models/manifestation.rb', line 531 def self.csv_header(role, = {col_sep: "\t", role: :Guest}) header = %w( manifestation_id original_title title_transcription creator contributor publisher pub_date statement_of_responsibility manifestation_price manifestation_created_at manifestation_updated_at manifestation_identifier access_address description note extent dimensions carrier_type edition edition_string volume_number volume_number_string issue_number issue_number_string serial_number ) header += IdentifierType.order(:position).pluck(:name) if defined?(EnjuSubject) header += SubjectHeadingType.order(:position).pluck(:name).map{|type| "subject:#{type}"} header += ClassificationType.order(:position).pluck(:name).map{|type| "classification:#{type}"} end header += %w( item_id item_identifier call_number item_note ) case role.to_sym when :Administrator, :Librarian header << "item_price" end header += %w( acquired_at accepted_at ) case role.to_sym when :Administrator, :Librarian header += %w( bookstore budget_type total_checkouts ) end header += %w( circulation_status shelf library item_created_at item_updated_at ) case role.to_sym when :Administrator, :Librarian header << "use_restriction" end header.to_csv() end |
.export(options = {format: :txt, role: :Guest}) ⇒ Object
785 786 787 788 789 790 791 792 |
# File 'app/models/manifestation.rb', line 785 def self.export( = {format: :txt, role: :Guest}) file = '' file += Manifestation.csv_header([:role], col_sep: "\t") if [:format].to_sym == :txt Manifestation.find_each do |manifestation| file += manifestation.to_csv() end file end |
.find_by_isbn(isbn) ⇒ Object
427 428 429 430 431 |
# File 'app/models/manifestation.rb', line 427 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') end |
.pickup(keyword = nil, current_user = nil) ⇒ Object
TODO: よりよい推薦方法
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'app/models/manifestation.rb', line 356 def self.pickup(keyword = nil, current_user = nil) return nil if self.cached_numdocs < 5 if current_user.try(:role) current_role_id = current_user.role.id else current_role_id = 1 end # TODO: ヒット件数が0件のキーワードがあるときに指摘する response = Manifestation.search do fulltext keyword if keyword with(:required_role_id).less_than_or_equal_to current_role_id order_by(:random) paginate page: 1, per_page: 1 end response.results.first end |
Instance Method Details
#acquired_at ⇒ Object
437 438 439 |
# File 'app/models/manifestation.rb', line 437 def acquired_at items.order(:acquired_at).first.try(:acquired_at) end |
#clear_cached_numdocs ⇒ Object
304 305 306 |
# File 'app/models/manifestation.rb', line 304 def clear_cached_numdocs Rails.cache.delete("manifestation_search_total") end |
#contributor ⇒ Object
343 344 345 |
# File 'app/models/manifestation.rb', line 343 def contributor contributors.collect(&:name).flatten end |
#created(agent) ⇒ Object
399 400 401 |
# File 'app/models/manifestation.rb', line 399 def created(agent) creates.find_by(agent_id: agent.id) end |
#creator ⇒ Object
339 340 341 |
# File 'app/models/manifestation.rb', line 339 def creator creators.collect(&:name).flatten end |
#extract_text ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'app/models/manifestation.rb', line 374 def extract_text return nil if .path.nil? return nil unless ENV['ENJU_EXTRACT_TEXT'] == 'true' if ENV['ENJU_STORAGE'] == 's3' body = Faraday.get(.expiring_url(10)).body.force_encoding('UTF-8') else body = File.open(.path).read end client = Faraday.new(url: ENV['SOLR_URL'] || Sunspot.config.solr.url) do |conn| conn.request :multipart conn.adapter :net_http end response = client.post('update/extract?extractOnly=true&wt=json&extractFormat=text') do |req| req.headers['Content-type'] = 'text/html' req.body = body end update_column(:fulltext, JSON.parse(response.body)[""]) end |
#extract_text! ⇒ Object
393 394 395 396 397 |
# File 'app/models/manifestation.rb', line 393 def extract_text! extract_text index Sunspot.commit end |
#first_issue ⇒ Object
512 513 514 515 516 |
# File 'app/models/manifestation.rb', line 512 def first_issue if series_master? derived_manifestations.where('date_of_publication IS NOT NULL').order('date_of_publication DESC').first end end |
#identifier_contents(name) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'app/models/manifestation.rb', line 518 def identifier_contents(name) if Rails::VERSION::MAJOR > 3 identifiers.id_type(name).order(:position).pluck(:body) else identifier_type = IdentifierType.find_by(name: name) if identifier_type identifiers.where(identifier_type_id: identifier_type.id).order(:position).pluck(:body) else [] end end end |
#index_series_statement ⇒ Object
433 434 435 |
# File 'app/models/manifestation.rb', line 433 def index_series_statement series_statements.map{|s| s.index; s.root_manifestation.try(:index)} end |
#isbn_characters ⇒ Object
798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'app/models/manifestation.rb', line 798 def isbn_characters identifier_contents(:isbn).map{|i| isbn10 = isbn13 = isbn10_dash = isbn13_dash = nil isbn10 = Lisbn.new(i).isbn10 isbn13 = Lisbn.new(i).isbn13 isbn10_dash = Lisbn.new(isbn10).isbn_with_dash if isbn10 isbn13_dash = Lisbn.new(isbn13).isbn_with_dash if isbn13 [ isbn10, isbn13, isbn10_dash, isbn13_dash ] }.flatten end |
#latest_issue ⇒ Object
506 507 508 509 510 |
# File 'app/models/manifestation.rb', line 506 def latest_issue if series_master? derived_manifestations.where('date_of_publication IS NOT NULL').order('date_of_publication DESC').first end end |
#number_of_pages ⇒ Object
312 313 314 315 316 |
# File 'app/models/manifestation.rb', line 312 def number_of_pages if start_page && end_page end_page.to_i - start_page.to_i + 1 end end |
#parent_of_series ⇒ Object
308 309 310 |
# File 'app/models/manifestation.rb', line 308 def parent_of_series original_manifestations end |
#produced(agent) ⇒ Object
407 408 409 |
# File 'app/models/manifestation.rb', line 407 def produced(agent) produces.find_by(agent_id: agent.id) end |
#pub_dates ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'app/models/manifestation.rb', line 488 def pub_dates return [] unless pub_date pub_date_array = pub_date.split(';') pub_date_array.map{|pub_date_string| date = nil while date.nil? do pub_date_string += '-01' break if pub_date_string =~ /-01-01-01$/ begin date = Time.zone.parse(pub_date_string) rescue ArgumentError rescue TZInfo::AmbiguousTime end end date }.compact end |
#publisher ⇒ Object
347 348 349 |
# File 'app/models/manifestation.rb', line 347 def publisher publishers.collect(&:name).flatten end |
#realized(agent) ⇒ Object
403 404 405 |
# File 'app/models/manifestation.rb', line 403 def realized(agent) realizes.find_by(agent_id: agent.id) end |
#root_series_statement ⇒ Object
794 795 796 |
# File 'app/models/manifestation.rb', line 794 def root_series_statement series_statements.find_by(root_manifestation_id: id) end |
#series_master? ⇒ Boolean
441 442 443 444 |
# File 'app/models/manifestation.rb', line 441 def series_master? return true if root_series_statement false end |
#set_agent_role_type(agent_lists, options = {scope: :creator}) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'app/models/manifestation.rb', line 450 def set_agent_role_type(agent_lists, = {scope: :creator}) agent_lists.each do |agent_list| name_and_role = agent_list[:full_name].split('||') if agent_list[:agent_identifier].present? agent = Agent.find_by(agent_identifier: agent_list[:agent_identifier]) end agent = Agent.find_by(full_name: name_and_role[0]) unless agent next unless agent type = name_and_role[1].to_s.strip case [:scope] when :creator type = 'author' if type.blank? role_type = CreateType.find_by(name: type) create = Create.find_by(work_id: id, agent_id: agent.id) if create create.create_type = role_type create.save(validate: false) end when :publisher type = 'publisher' if role_type.blank? produce = Produce.find_by(manifestation_id: id, agent_id: agent.id) if produce produce.produce_type = ProduceType.find_by(name: type) produce.save(validate: false) end else raise "#{[:scope]} is not supported!" end end end |
#set_date_of_publication ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'app/models/manifestation.rb', line 257 def set_date_of_publication return if pub_date.blank? year = Time.utc(pub_date.rjust(4, "0")).year rescue nil begin date = Time.zone.parse(pub_date.rjust(4, "0")) if date.year != year raise ArgumentError end rescue ArgumentError, TZInfo::AmbiguousTime date = nil end pub_date_string = pub_date.rjust(4, "0").split(';').first.gsub(/[\[\]]/, '') if pub_date_string.length == 4 date = Time.zone.parse(Time.utc(pub_date_string).to_s).beginning_of_day else while date.nil? do pub_date_string += '-01' break if pub_date_string =~ /-01-01-01$/ begin date = Time.zone.parse(pub_date_string) if date.year != year raise ArgumentError end rescue ArgumentError date = nil rescue TZInfo::AmbiguousTime date = nil self.year_of_publication = pub_date_string.to_i if pub_date_string =~ /^\d+$/ break end end end if date self.year_of_publication = date.year self.month_of_publication = date.month if date.year > 0 self.date_of_publication = date end end end |
#set_number ⇒ Object
482 483 484 485 486 |
# File 'app/models/manifestation.rb', line 482 def set_number self.volume_number = volume_number_string.scan(/\d*/).map{|s| s.to_i if s =~ /\d/}.compact.first if volume_number_string && !volume_number? self.issue_number = issue_number_string.scan(/\d*/).map{|s| s.to_i if s =~ /\d/}.compact.first if issue_number_string && !issue_number? self.edition = edition_string.scan(/\d*/).map{|s| s.to_i if s =~ /\d/}.compact.first if edition_string && !edition? end |
#sort_title ⇒ Object
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'app/models/manifestation.rb', line 411 def sort_title if series_master? if root_series_statement.title_transcription? NKF.nkf('-w --katakana', root_series_statement.title_transcription) else root_series_statement.original_title end else if title_transcription? NKF.nkf('-w --katakana', title_transcription) else original_title end end end |
#title ⇒ Object
351 352 353 |
# File 'app/models/manifestation.rb', line 351 def title titles end |
#titles ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'app/models/manifestation.rb', line 318 def titles title = [] title << original_title.to_s.strip title << title_transcription.to_s.strip title << title_alternative.to_s.strip title << volume_number_string title << issue_number_string title << serial_number.to_s title << edition_string title << series_statements.map{|s| s.titles} #title << original_title.wakati #title << title_transcription.wakati rescue nil #title << title_alternative.wakati rescue nil title.flatten end |
#to_csv(options = {format: :txt, role: :Guest}) ⇒ Object
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 |
# File 'app/models/manifestation.rb', line 603 def to_csv( = {format: :txt, role: :Guest}) lines = [] if items.exists? items.includes(shelf: :library).each do |i| item_lines = [] item_lines << id item_lines << original_title item_lines << title_transcription if creators.exists? item_lines << creators.pluck(:full_name).join("//") else item_lines << nil end if contributors.exists? item_lines << contributors.pluck(:full_name).join("//") else item_lines << nil end if publishers.exists? item_lines << publishers.pluck(:full_name).join("//") else item_lines << nil end item_lines << pub_date item_lines << statement_of_responsibility item_lines << price item_lines << created_at item_lines << updated_at item_lines << manifestation_identifier item_lines << access_address item_lines << description.try(:gsub, /\r?\n/, '\n') item_lines << note.try(:gsub, /\r?\n/, '\n') item_lines << extent item_lines << dimensions item_lines << carrier_type.name item_lines << edition item_lines << edition_string item_lines << volume_number item_lines << volume_number_string item_lines << issue_number item_lines << issue_number_string item_lines << serial_number IdentifierType.order(:position).pluck(:name).each do |identifier_type| identifier_list = identifier_contents(identifier_type.to_sym) if identifier_list item_lines << identifier_list.join("//") else item_lines << nil end end if defined?(EnjuSubject) SubjectHeadingType.order(:position).each do |subject_heading_type| if subjects.exists? item_lines << subjects.where(subject_heading_type: subject_heading_type).pluck(:term).join('//') else item_lines << nil end end ClassificationType.order(:position).each do |classification_type| if classifications.exists? item_lines << classifications.where(classification_type: classification_type).pluck(:category).join('//') else item_lines << nil end end end item_lines << i.id item_lines << i.item_identifier item_lines << i.call_number item_lines << i.note.try(:gsub, /\r?\n/, '\n') case [:role].to_sym when :Administrator, :Librarian item_lines << i.price end item_lines << i.acquired_at item_lines << i.accept.try(:created_at) case [:role].to_sym when :Administrator, :Librarian item_lines << i.bookstore.try(:name) item_lines << i.budget_type.try(:name) if defined?(EnjuCirculation) item_lines << Checkout.where(item_id: i.id).count else item_lines << '' end end if defined?(EnjuCirculation) item_lines << i.circulation_status.try(:name) else item_lines << '' end item_lines << i.shelf.name item_lines << i.shelf.library.name item_lines << i.created_at item_lines << i.updated_at case [:role].to_sym when :Administrator, :Librarian if defined?(EnjuCirculation) item_lines << i.use_restriction.try(:name) else item_lines << '' end end lines << item_lines end else line = [] line << id line << original_title line << title_transcription if creators.exists? line << creators.pluck(:full_name).join("//") else line << nil end if contributors.exists? line << contributors.pluck(:full_name).join("//") else line << nil end if publishers.exists? line << publishers.pluck(:full_name).join("//") else line << nil end line << pub_date line << statement_of_responsibility line << price line << created_at line << updated_at line << manifestation_identifier line << access_address line << description.try(:gsub, /\r?\n/, '\n') line << note.try(:gsub, /\r?\n/, '\n') line << extent line << dimensions line << carrier_type.name line << edition line << edition_string line << volume_number line << volume_number_string line << issue_number line << issue_number_string line << serial_number IdentifierType.order(:position).pluck(:name).each do |identifier_type| identifier_list = identifier_contents(identifier_type.to_sym) if identifier_list line << identifier_list.join("//") else line << nil end end if defined?(EnjuSubject) SubjectHeadingType.order(:position).each do |subject_heading_type| if subjects.exists? line << subjects.where(subject_heading_type: subject_heading_type).pluck(:term).join('//') else line << nil end end ClassificationType.order(:position).each do |classification_type| if classifications.exists? line << classifications.where(classification_type: classification_type).pluck(:category).join('//') else line << nil end end end lines << line end if [:format] == :txt lines.map{|i| i.to_csv(col_sep: "\t")}.join else lines end end |
#url ⇒ Object
334 335 336 337 |
# File 'app/models/manifestation.rb', line 334 def url #access_address "#{LibraryGroup.site_config.url}#{self.class.to_s.tableize}/#{self.id}" end |
#web_item ⇒ Object
446 447 448 |
# File 'app/models/manifestation.rb', line 446 def web_item items.find_by(shelf_id: Shelf.web.id) end |