7 changed files with 147 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||||
|
class Cdao::Base < ActiveRecord::Base |
||||||
|
self.abstract_class = true |
||||||
|
|
||||||
|
establish_connection Cdao::DatabaseConnector.database_config |
||||||
|
|
||||||
|
def self.table_name_prefix |
||||||
|
"cdao_" |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,30 @@ |
|||||||
|
module Cdao |
||||||
|
module DatabaseConnector |
||||||
|
class << self |
||||||
|
attr_writer :config, :config_path |
||||||
|
|
||||||
|
def database_config |
||||||
|
config["database"] |
||||||
|
end |
||||||
|
|
||||||
|
def config |
||||||
|
@config ||= |
||||||
|
if File.exist?(config_path) |
||||||
|
File.open(config_path) do |file| |
||||||
|
YAML.safe_load(ERB.new(file.read).result)[::Rails.env] |
||||||
|
end |
||||||
|
else |
||||||
|
{} |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def config_path |
||||||
|
@config_path ||= File.join(::Rails.root, "config", "database.cdao.yml") |
||||||
|
end |
||||||
|
|
||||||
|
def reset |
||||||
|
@config = nil |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,3 @@ |
|||||||
|
class Cdao::Jurisprudence < Cdao::Base |
||||||
|
self.table_name = "jurisprudences" |
||||||
|
end |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
class Cdao::Library < Cdao::Base |
||||||
|
default_scope { where(id: Cdao::Library.find_by(key: "JurisprudenceEncyclopedia").try(:id)) } |
||||||
|
|
||||||
|
self.table_name = "producttitles" |
||||||
|
end |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
class Cdao::LibrarySubject < Cdao::Base |
||||||
|
acts_as_list column: :rank, scope: %i[library_id subject_parent_id] |
||||||
|
|
||||||
|
belongs_to :library, optional: false |
||||||
|
delegate :name, :rank, to: :library, prefix: true |
||||||
|
|
||||||
|
belongs_to :subject, optional: false |
||||||
|
|
||||||
|
validates :library_id, uniqueness: { scope: %i[subject_id] } |
||||||
|
|
||||||
|
after_save do |
||||||
|
update_column :subject_parent_id, subject.parent_id # rubocop:disable Rails/SkipsModelValidations |
||||||
|
end |
||||||
|
|
||||||
|
def self.import |
||||||
|
Subject.find_each do |subject| |
||||||
|
subject.library_subjects.where(library_id: subject.library_id).first_or_create |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def self.import_rank_from_subjects |
||||||
|
Subject.find_each do |subject| |
||||||
|
subject.library_subjects.each do |subject_library| |
||||||
|
subject_library.update(rank: subject.rank) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
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_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 |
||||||
|
|
||||||
|
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 |
||||||
|
end |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
development: |
||||||
|
database: |
||||||
|
adapter: <%= ENV["DB_ADAPTER"] || "postgresql" %> |
||||||
|
url: <%= ENV["DATABASE_URL_CDAO"] %> |
||||||
|
pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %> |
||||||
|
prepared_statements: false |
||||||
|
advisory_locks: false |
||||||
|
variables: |
||||||
|
statement_timeout: 10s |
||||||
|
|
||||||
|
test: |
||||||
|
database: |
||||||
|
adapter: <%= ENV["DB_ADAPTER"] || "postgresql" %> |
||||||
|
url: <%= ENV["DATABASE_URL_CDAO"] %> |
||||||
|
pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %> |
||||||
|
|
||||||
|
production: |
||||||
|
database: |
||||||
|
adapter: <%= ENV["DB_ADAPTER"] || "postgresql" %> |
||||||
|
url: <%= ENV["DATABASE_URL_CDAO"] %> |
||||||
|
pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %> |
||||||
|
prepared_statements: false |
||||||
|
advisory_locks: false |
||||||
|
variables: |
||||||
|
statement_timeout: 10s |
||||||
Loading…
Reference in new issue