Переглянути джерело

Add a scope `with_location` to Event.

Akinori MUSHA 10 роки тому
батько
коміт
aceed9ac83

+ 4 - 0
app/models/event.rb

@@ -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

+ 1 - 1
app/views/agents/agent_views/user_location_agent/_show.html.erb

@@ -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>
 

+ 14 - 0
spec/models/event_spec.rb

@@ -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