Make Event's location accessible from Liquid templates.

This commit is contained in:
Akinori MUSHA 2014-09-19 21:57:45 +09:00
parent 1406f37d48
commit dd507e3cbf
4 changed files with 37 additions and 0 deletions

View file

@ -117,4 +117,8 @@ class EventDrop
@object.created_at
}
end
def _location_
@object.location
end
end

View file

@ -1,6 +1,10 @@
require 'liquid'
Location = Struct.new(:lat, :lng, :radius, :speed, :course)
class Location
include LiquidDroppable
protected :[]=
def initialize(data = {})
@ -86,3 +90,13 @@ class Location
end
end
end
class LocationDrop
KEYS = Location.members.map(&:to_s).concat(%w[latitude longitude])
def before_method(key)
if KEYS.include?(key)
@object.__send__(key)
end
end
end

View file

@ -53,4 +53,16 @@ describe Location do
expect(Location.new(lat: 2, radius: 1).present?).to be_falsy
expect(Location.new(lng: 3, radius: 1).present?).to be_falsy
end
it "is droppable" do
{
'{{location.lat}}' => '2.0',
'{{location.latitude}}' => '2.0',
'{{location.lng}}' => '3.0',
'{{location.longitude}}' => '3.0',
}.each { |template, result|
expect(Liquid::Template.parse(template).render('location' => location.to_liquid)).to eq(result),
"expected #{template.inspect} to expand to #{result.inspect}"
}
end
end

View file

@ -174,6 +174,8 @@ describe EventDrop do
'title' => 'some title',
'url' => 'http://some.site.example.org/',
}
@event.lat = 2
@event.lng = 3
@event.save!
end
@ -210,4 +212,9 @@ describe EventDrop do
t = '{{created_at | date:"%FT%T%z" }}'
interpolate(t, @event).should eq(@event.created_at.strftime("%FT%T%z"))
end
it 'should have _location_' do
t = '{{_location_.lat}},{{_location_.lng}}'
interpolate(t, @event).should eq("2.0,3.0")
end
end