Skip to content

Commit

Permalink
Added support for nearby and featured projects to the JSON API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kueda committed Apr 18, 2012
1 parent 15e2a3a commit ebdfbd1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
34 changes: 21 additions & 13 deletions app/controllers/projects_controller.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions app/models/project.rb
Expand Up @@ -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#" },
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ebdfbd1

Please sign in to comment.