lib/carte/server/models/card.rb in carte-server-0.0.9 vs lib/carte/server/models/card.rb in carte-server-0.0.10
- old
+ new
@@ -4,14 +4,16 @@
class Card
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
include Mongoid::Document::Taggable
+ include Mongoid::Geospatial
field :title, type: String
field :new_title, type: String
field :content, type: String
+ field :random_point, type: Point, spatial: true
index({title: 1}, {unique: true, name: "title_index"})
validates :title,
presence: true,
@@ -36,12 +38,16 @@
if self.new_title
self.title = self.new_title
self.new_title = nil
end
end
+
+ before_create do
+ self.random_point = [Random.rand, 0]
+ end
- def self.sample(size=1)
- self.in(id: (1..self.count).to_a.sample(size))
+ def self.random
+ self.near(random_point: [Random.rand, 0])
end
def lefts(size=1)
ids = []
count = self.class.all.count