Sha256: 18d23cfaf6a4e0cfa93ac056893206629b3b32f8a91a58af4c07be99a38d55e4

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

module Carte
  class Server < Sinatra::Base
    module Models
      class Card
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::Attributes::Dynamic
        include Mongoid::Document::Taggable
      
        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}
        validates :tags,
          length: {maximum: 3, message: 'are too many (maximum is 3 tags)'},
          array: {length: {maximum: 10}}
      
        has_many :histories
      
        def version
          self.histories.size + 1
        end
      
        before_validation(on: :update) do
          if self.new_title
            self.title = self.new_title
            self.new_title = nil
          end
        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.9 lib/carte/server/models/card.rb
carte-server-0.0.8 lib/carte/server/models/card.rb