MiraitSystems/enju_trunk

View on GitHub
app/models/bookbinding.rb

Summary

Maintainability
A
1 hr
Test Coverage
class Bookbinding < ActiveRecord::Base
   has_many :binding_items
   has_many :items, :through => :binding_items
   
   def book_binding
    Item.transaction do
      if self.binding_items.blank?
         errors[:base] = I18n.t('activerecord.errors.messages.bookbinding.bookbinding_nil')
         return nil
      else
         binder = Item.new()
         binder.bookbinder = true
         new_manifestation = Manifestation.new()
         new_manifestation.original_title = binding_items[0].item.manifestation.original_title
         new_manifestation.bookbinder = true
         new_manifestation.manifestation_type = binding_items[0].item.manifestation.manifestation_type
         new_manifestation.save!
         binder.manifestation = new_manifestation
         binder.circulation_status = CirculationStatus.where(:name => 'In Factory').first
         library = binding_items[0].item.shelf.library
         binder.shelf = Shelf.where(:name => library.name + '_binding_shelf', :library_id => library.id).try(:first) || Shelf.where(:name => 'binding_shelf').try(:first)
         binder.save!
         self.binding_items.each do |binding_item|
          binding_item.item.bookbinder_id = binder.id
          binding_item.item.circulation_status = CirculationStatus.where(:name => 'Binded').first
          binding_item.item.shelf_id = binder.shelf_id
          binding_item.item.save!
          Manifestation.find(binding_item.item.manifestation.id).index
        end
        BindingItem.destroy_all(:bookbinding_id => self.id)
        return binder
      end
    end
  end

end