app/models/item.rb in enju_biblio-0.3.6 vs app/models/item.rb in enju_biblio-0.3.7
- old
+ new
@@ -15,12 +15,14 @@
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
- has_one :withdraw
+ 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
@@ -123,6 +171,7 @@
# checkout_type_id :integer default(1), not null
# binding_item_identifier :string
# binding_call_number :string
# binded_at :datetime
# manifestation_id :integer not null
+# memo :text
#