Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for nearby and featured projects to the JSON API.

  • Loading branch information...
commit ebdfbd10b2edb9a6adffbe8e23366f49174fab64 1 parent 15e2a3a
@kueda kueda authored
Showing with 39 additions and 13 deletions.
  1. +21 −13 app/controllers/projects_controller.rb
  2. +18 −0 app/models/project.rb
View
34 app/controllers/projects_controller.rb
@@ -20,15 +20,26 @@ class ProjectsController < ApplicationController
}
def index
- project_observations = ProjectObservation.all(
- :select => "MAX(id) AS id, project_id",
- :order => "id desc", :limit => 9, :group => "project_id")
- @projects = Project.all(:conditions => ["id IN (?)", project_observations.map(&:project_id)])
- @created = Project.all(:order => "id desc", :limit => 9)
- @featured = Project.featured.all
- if logged_in?
- @started = current_user.projects.all(:order => "id desc", :limit => 9)
- @joined = current_user.project_users.all(:include => :project, :order => "id desc", :limit => 9).map(&:project)
+ respond_to do |format|
+ format.html do
+ project_observations = ProjectObservation.all(
+ :select => "MAX(id) AS id, project_id",
+ :order => "id desc", :limit => 9, :group => "project_id")
+ @projects = Project.all(:conditions => ["id IN (?)", project_observations.map(&:project_id)])
+ @created = Project.all(:order => "id desc", :limit => 9)
+ @featured = Project.featured.all
+ if logged_in?
+ @started = current_user.projects.all(:order => "id desc", :limit => 9)
+ @joined = current_user.project_users.all(:include => :project, :order => "id desc", :limit => 9).map(&:project)
+ end
+ end
+ format.json do
+ scope = Project.scoped({})
+ scope = scope.featured if params[:featured]
+ scope = scope.near_point(params[:latitude], params[:longitude]) if params[:latitude] && params[:longitude]
+ @projects = scope.paginate(:page => params[:page], :per_page => 100)
+ render :json => @projects.to_json(Project.default_json_options.update(:include => :project_list))
+ end
end
end
@@ -132,10 +143,7 @@ def by_login
format.json do
render :json => @project_users.to_json(:include => {
:user => {:only => :login},
- :project => {
- :methods => [:icon_url, :project_observation_rule_terms, :featured_at_utc],
- :include => :project_list
- }
+ :project => Project.default_json_options.update(:include => :project_list)
})
end
end
View
18 app/models/project.rb
@@ -31,6 +31,18 @@ class Project < ActiveRecord::Base
validates_presence_of :user_id
named_scope :featured, {:conditions => "featured_at IS NOT NULL"}
+ named_scope :near_point, lambda {|latitude, longitude|
+ latitude = latitude.to_f
+ longitude = longitude.to_f
+ {
+ :joins => [
+ "INNER JOIN rules ON rules.ruler_type = 'Project' AND rules.operand_type = 'Place' AND rules.ruler_id = projects.id",
+ "INNER JOIN places ON places.id = rules.operand_id"
+ ],
+ :conditions => "ST_Distance(ST_Point(places.longitude, places.latitude), ST_Point(#{longitude}, #{latitude})) < 5",
+ :order => "ST_Distance(ST_Point(places.longitude, places.latitude), ST_Point(#{longitude}, #{latitude}))"
+ }
+ }
has_attached_file :icon,
:styles => { :thumb => "48x48#", :mini => "16x16#", :span1 => "30x30#", :span2 => "70x70#" },
@@ -88,6 +100,12 @@ def featured_at_utc
featured_at.try(:utc)
end
+ def self.default_json_options
+ {
+ :methods => [:icon_url, :project_observation_rule_terms, :featured_at_utc, :rule_place]
+ }
+ end
+
def self.update_curator_idents_on_make_curator(project_id, project_user_id)
unless proj = Project.find_by_id(project_id)
return
Please sign in to comment.
Something went wrong with that request. Please try again.