Add a scope with_location to Event.

This commit is contained in:
Akinori MUSHA 2014-09-19 10:16:02 +09:00
parent 94cec5e4cd
commit aceed9ac83
3 changed files with 19 additions and 1 deletions

View file

@ -28,6 +28,10 @@ class Event < ActiveRecord::Base
where("expires_at IS NOT NULL AND expires_at < ?", Time.now)
}
scope :with_location, -> {
where.not(lat: nil).where.not(lng: nil)
}
# Emit this event again, as a new Event.
def reemit!
agent.create_event :payload => payload, :lat => lat, :lng => lng

View file

@ -2,7 +2,7 @@
<h3>Recent Event Map</h3>
<% events = @agent.events.where("lat IS NOT null AND lng IS NOT null").order("id desc").limit(500) %>
<% events = @agent.events.with_location.order("id desc").limit(500) %>
<% if events.length > 0 %>
<div id="map_canvas" style="width:800px; height:800px"></div>

View file

@ -1,6 +1,20 @@
require 'spec_helper'
describe Event do
describe ".with_location" do
it "selects events with location" do
event = events(:bob_website_agent_event)
event.lat = 2
event.lng = 3
event.save!
Event.with_location.pluck(:id).should == [event.id]
event.lat = nil
event.save!
Event.with_location.should be_empty
end
end
describe "#reemit" do
it "creates a new event identical to itself" do
events(:bob_website_agent_event).lat = 2