You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

104 lines
3.6 KiB

require "csv"
class Cdao::Subject < Cdao::Base
default_scope { joins(:library_subjects)
.where(cdao_library_subjects: { library_id: [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] }) }
has_paper_trail
has_ancestry
has_many :library_subjects, dependent: :destroy
accepts_nested_attributes_for :library_subjects, allow_destroy: true
has_many :libraries, through: :library_subjects
alias_attribute :producttitles, :libraries
has_many :doctrine_subjects, class_name: "::DoctrineSubject", dependent: :destroy
has_many :doctrines, through: :doctrine_subjects
validates :name, presence: true
validates :name, uniqueness: { scope: %i[ancestry] }
scope :byproduct, lambda { |id|
joins(:library_subjects).where(library_subjects: { library_id: id })
}
scope :for_lib, lambda { |*ids|
product_key = ids.shift
where(arel_table[:id].in(ids).or(arel_table[:library_id].eq(product_key)))
}
scope :with_parent, -> { where(arel_table[:ancestry].not_eq nil) }
state_machine initial: :published do
event :publish do
transition from: :activated, to: :published
end
event :unpublish do
transition from: :published, to: :activated
end
end
def lineage_name
path.map(&:name).join(" > ")
end
def name_with_product_names
product_names = libraries.map(&:name).reject(&:blank?).join(", ")
return name if product_names.blank?
[name, "(#{product_names})"].join(" ")
end
def self.parse_csv_file(path)
::CSV.foreach(path).each_with_index do |data, index|
next if data[0].blank?
names = data[0].split("; ").map(&:strip)
root_name = names[0]
sub1_name = names[1]
sub2_name = names[2]
sub3_name = names[3]
sub4_name = names[4]
sub5_name = names[5]
sub6_name = names[6]
root = Cdao::Subject.find_or_create_by(name: root_name, ancestry: nil) if root_name.present?
root.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if root.persisted?
sub1 = root.children.find_or_create_by(name: sub1_name) if root.present? && sub1_name.present?
sub1.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if sub1.present? && sub1.persisted?
sub2 = sub1.children.find_or_create_by(name: sub2_name) if sub1.present? && sub2_name.present?
sub2.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if sub2.present? && sub2.persisted?
sub3 = sub2.children.find_or_create_by(name: sub3_name) if sub2.present? && sub3_name.present?
sub3.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if sub3.present? && sub3.persisted?
sub4 = sub3.children.find_or_create_by(name: sub4_name) if sub3.present? && sub4_name.present?
sub4.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if sub4.present? && sub4.persisted?
sub5 = sub4.children.find_or_create_by(name: sub5_name) if sub4.present? && sub5_name.present?
sub5.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if sub5.present? && sub5.persisted?
sub6 = sub5.children.find_or_create_by(name: sub6_name) if sub5.present? && sub6_name.present?
sub6.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] if sub6.present? && sub6.persisted?
end
end
searchable do
integer :id
integer :parent_id
integer :doctrine_ids, multiple: true
text :name
text :lineage_name do
lineage_name
end
string :name
string :state
string :lineage_name do
lineage_name
end
end
end