Sha256: a1ef702e478701e360a5e2a0644c163e1d61beeae93026298d1fdb18c7174c28

Contents?: true

Size: 1.5 KB

Versions: 14

Compression:

Stored size: 1.5 KB

Contents

class Shelf < ActiveRecord::Base
  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
    string :library do
      library.name
    end
    text :name do
      [name, library.name, display_name, library.display_name]
    end
    integer :position
  end

  paginates_per 10

  def web_shelf?
    return true if id == 1
    false
  end

  def self.web
    Shelf.find(1)
  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
#
# Table name: shelves
#
#  id           :integer          not null, primary key
#  name         :string           not null
#  display_name :text
#  note         :text
#  library_id   :integer          not null
#  items_count  :integer          default(0), not null
#  position     :integer
#  created_at   :datetime
#  updated_at   :datetime
#  deleted_at   :datetime
#  closed       :boolean          default(FALSE), not null
#

Version data entries

14 entries across 12 versions & 2 rubygems

Version Path
enju_library-0.3.3 app/models/shelf.rb
enju_library-0.3.2 app/models/shelf.rb
enju_library-0.3.1 app/models/shelf.rb
enju_library-0.3.0 app/models/shelf.rb
enju_library-0.3.0.rc.1 app/models/shelf.rb
enju_library-0.3.0.beta.2 app/models/shelf.rb
enju_library-0.2.5 app/models/shelf.rb
enju_library-0.2.4 app/models/shelf.rb
enju_library-0.2.3 app/models/shelf.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/enju_library-0.2.2/app/models/shelf.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/bundler/gems/enju_library-d36f415e177e/app/models/shelf.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/enju_library-0.2.1/app/models/shelf.rb
enju_library-0.2.2 app/models/shelf.rb
enju_library-0.2.1 app/models/shelf.rb