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.
99 lines
2.9 KiB
99 lines
2.9 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: :restrict_with_error |
|
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_initialize_by(name: root_name, ancestry: nil) if root_name.present? |
|
sub1 = root.children.find_or_initialize_by(name: sub1_name) if root.present? && sub1_name.present? |
|
sub2 = sub1.children.find_or_initialize_by(name: sub2_name) if sub1.present? && sub2_name.present? |
|
sub3 = sub2.children.find_or_initialize_by(name: sub3_name) if sub2.present? && sub3_name.present? |
|
sub4 = sub3.children.find_or_initialize_by(name: sub4_name) if sub3.present? && sub4_name.present? |
|
sub5 = sub4.children.find_or_initialize_by(name: sub5_name) if sub4.present? && sub5_name.present? |
|
sub6 = sub5.children.find_or_initialize_by(name: sub6_name) if sub5.present? && sub6_name.present? |
|
|
|
root.descendants.each { |a| a.library_ids = [Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)] } if root.present? |
|
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
|
|
|