Let's go into config/routes and add a get for subjects and
route that over to the thing_images controller #subjects.
And then if we use rake routes, in order to check our results,
we'll see that we now have a subjects helper method.
We have a URI under /api/subjects.
And that's being routed over to the controller that we expect.
So obviously, if we're going to define the fact that our thing_images controller
has a action method for subjects, we have to define that method.
And before we go too far with this method I know I
probably ought to go into the policy and say, yeah, cverybody can call it.
Because that is going to be our policy.
But I'm going to go ahead and take a shortcut and go ahead and
exclude it from the policy within the controller.
And I'll do that in the name of just showing you an example of
how to do that, okay?
So our subjects action method does not have to call verify_authorized.
But one of the things it does have to go on is our subjects action
requires an origin.
And so let's make it easy on our implementation and
have a before_action that'll resolve that origin for the subject.
So at the bottom of the controller, let's go ahead and
add a helper method called origin.
And what it's going to do is inspect the parameters and
pluck out what it believes is an origin.
And so if it sees longitude and latitude it's going to say that looks good.
I'm going to go ahead and now create a new Point based upon the float values
of the longitude and latitude being passed in.
And there might have been another couple of cases of how we could pull out origin,
like if you were saying relative to an image, or relative to a thing.
But let's stop right now and say well if that wasn't enough,
let's go ahead and raise the fact that we have a missing parameter.
And tell them that lat and long, or
an address, is going to be required in order to call this method.
Remember, we're going to to be able to geocode by address as well as
lat and long.
So we've already taken care of origin.
And the other parameter that we're passing in is miles.
So let's go ahead and let our action method take care of extracting that.
And then what we want to do is do a range query, where we'll look for
all thing_images within miles of the origin.
And when we return it, we want to include both the thing name,
the caption for the image, as well as the image position.
Now we have no subjects view,
which would be the default if we just returned right here.
So what we want to do is signal that we want
the thing_images/index marshaler to be used when returning a result.
When we look back at our test results, we're looking pretty good, except for
something that we should be very familiar with.
We need to add position to the ThingImage marshaling.
So over in the view for thing_images, in its index,
here's our current implementation of how we're marshaling a thing image.
So the representation of ThingImage at this point is based upon a query scope
which only put a flat longitude and latitude in with the object.
We don't have a position represented as a point that we can just call to hash on.
Let's just play it easy and
manually marshal it out here since it's a very simple object.
Note that not all of the thing_images that marshal will have a longitude and
a latitude.
Note, we are using the generic index view, so
we need to test whether this thing_image responds to longitude.
We could have also test for latitude, but I think one's enough.
And then it does a double check to see, well does it even have a longitude value?
Just trying to cut down on marshaling nils, okay?