Sha256: 9d5116def0ee87a545b2c08a0ae666e52b4ab7b2cb006fe5d3b640dea21dc604

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Carte
  class Server < Sinatra::Base
    module Models
      class Card
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::Attributes::Dynamic
      
        field :title, type: String
        field :new_title, type: String
        field :content, type: String

        index({title: 1}, {unique: true, name: "title_index"})
      
        validates :title,
          presence: true,
	  on: :create
        validates :title,
          uniqueness: true,
          length: {maximum: 70}
        validates :content,
          presence: true,
          length: {maximum: 560}
      
        has_many :histories
      
        def version
          self.histories.size + 1
        end
      
        before_validation(on: :update) do
          self.title = self.new_title
          self.new_title = nil
        end
      
        def self.sample(size=1)
          self.in(id: (1...self.count).to_a.sample(size))
        end
      
        def lefts(size=1)
          ids = []
          count = self.class.all.count
          1.upto(size) do |i|
            ids << (self.id - i > 0 ? self.id - i : count + (self.id - i))
          end
          self.class.in(id: ids)
        end
      
        def rights(size=1)
          ids = []
          count = self.class.all.count
          1.upto(size) do |i|
            ids << (self.id + i <= count ? self.id + i : self.id + i - count)
          end
          self.class.in(id: ids)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
carte-server-0.0.3 lib/carte/server/models/card.rb
carte-server-0.0.1 lib/carte/server/models/card.rb