app/models/item.rb in enju_biblio-0.4.0.beta.2 vs app/models/item.rb in enju_biblio-0.4.0.rc.1
- old
+ new
@@ -8,19 +8,21 @@
end
}
delegate :display_name, to: :shelf, prefix: true
has_many :owns
has_many :agents, through: :owns
- has_many :donates
+ has_many :donates, dependent: :destroy
has_many :donors, through: :donates, source: :agent
has_one :resource_import_result
belongs_to :manifestation, touch: true
belongs_to :bookstore, optional: true
belongs_to :required_role, class_name: 'Role', foreign_key: 'required_role_id'
belongs_to :budget_type, optional: true
has_one :accept, dependent: :destroy
has_one :withdraw, dependent: :destroy
+ has_many :custom_properties, as: :resource, dependent: :destroy
+ accepts_nested_attributes_for :custom_properties, allow_destroy: true, reject_if: :all_blank
scope :accepted_between, lambda{|from, to| includes(:accept).where('items.created_at BETWEEN ? AND ?', Time.zone.parse(from).beginning_of_day, Time.zone.parse(to).end_of_day)}
belongs_to :shelf, counter_cache: true
validates_associated :bookstore
@@ -94,10 +96,56 @@
true
else
true
end
end
+
+ def self.csv_header(role: 'Guest')
+ Item.new.to_hash(role: role).keys
+ end
+
+ def to_hash(role: 'Guest')
+ record = {
+ item_id: id,
+ item_identifier: item_identifier,
+ call_number: call_number,
+ shelf: shelf.name,
+ item_note: note,
+ accepted_at: accept.try(:created_at),
+ acquired_at: acquired_at,
+ item_created_at: created_at,
+ item_updated_at: updated_at
+ }
+
+ if ['Administrator', 'Librarian'].include?(role)
+ record.merge!({
+ bookstore: bookstore.try(:name),
+ budget_type: budget_type.try(:name),
+ item_price: price,
+ memo: memo
+ })
+
+ # 最もカスタム項目の多い資料について、カスタム項目の個数を取得する
+ ActiveRecord::Base.connection.execute('SELECT max(record_count) FROM (SELECT count(*) AS record_count, resource_id, resource_type FROM custom_properties GROUP BY resource_id, resource_type) AS type_count ;').first.values[0].to_i.times do |i|
+ property = custom_properties[i]
+ if property
+ record[:"item_custom_property_#{i + 1}"] = "#{property.label}: #{property.value}"
+ else
+ record[:"item_custom_property_#{i + 1}"] = nil
+ end
+ end
+
+ if defined?(EnjuCirculation)
+ record.merge!({
+ use_restriction: use_restriction.try(:name),
+ total_checkouts: checkouts.count
+ })
+ end
+ end
+
+ record
+ end
end
# == Schema Information
#
# Table name: items
@@ -105,10 +153,9 @@
# id :integer not null, primary key
# call_number :string
# item_identifier :string
# created_at :datetime
# updated_at :datetime
-# deleted_at :datetime
# shelf_id :integer default(1), not null
# include_supplements :boolean default(FALSE), not null
# note :text
# url :string
# price :integer