app/models/shelf.rb in enju_library-0.1.0.pre39 vs app/models/shelf.rb in enju_library-0.1.0.pre40
- old
+ new
@@ -1,17 +1,17 @@
class Shelf < ActiveRecord::Base
- attr_accessible :name, :display_name, :note, :library_id, :closed
include MasterModel
scope :real, -> { where('library_id != 1') }
belongs_to :library, validate: true
has_many :items
has_many :picture_files, as: :picture_attachable, dependent: :destroy
validates_associated :library
validates :library, presence: true
validates_uniqueness_of :display_name, scope: :library_id
validates :name, format: { with: /\A[a-z][0-9a-z\-_]{1,253}[0-9a-z]\Z/ }
+ before_update :reset_position
acts_as_list scope: :library
searchable do
string :name
@@ -33,17 +33,18 @@
def self.web
Shelf.find(1)
end
- def first?
- # 必ずposition順に並んでいる
- return true if library.shelves.first.position == position
- false
- end
-
def localized_display_name
display_name.localize
+ end
+
+ # http://stackoverflow.com/a/12437606
+ def reset_position
+ if library_id_changed?
+ self.position = library.shelves.count > 0 ? library.shelves.last.position + 1 : 1
+ end
end
end
# == Schema Information
#