Skip to content

Instantly share code, notes, and snippets.

@topherfangio
Created May 24, 2011 20:00
Show Gist options
  • Save topherfangio/989557 to your computer and use it in GitHub Desktop.
Save topherfangio/989557 to your computer and use it in GitHub Desktop.
class AuditsController < ApiController
def index
@audits = current_member.audit_responses.where('updated_at IS NULL').map{ |ar| ar.audit.attributes }.uniq
respond_with @audits
end
def respond
if params[:audit]
id = params[:audit][:id]
if Audit.exists?(:id => id)
audit = Audit.find(id)
audit.update_attributes(params[:audit])
audit_json = { "created" => [], "updated" => [audit.id], "deleted" => [], "attributes" => { audit.id => audit } }
else
audit = Audit.create(params[:audit])
audit_json = { "created" => [audit.id], "updated" => [], "deleted" => [], "attributes" => { audit.id => audit } }
end
if audit.is_survey
audit.audit_responses.where("user_id = ? AND custom_form_question_id IS NOT NULL", current_member.id).each { |r| r.accepted_at = Time.new; r.save }
end
if audit.save
deleted = AuditResponse.where(:user_id => current_member.id, :audit_id => audit.id, :updated_at => nil).destroy_all
render(:json => {
"changeset" => {
"sc_version" => 2,
"sc_types" => ["Evaluation", "EvaluationResponse"],
"Evaluation" => audit_json,
"EvaluationResponse" => {
"created" => [],
"updated" => [],
"deleted" => deleted.map{ |ar| ar.id },
"attributes" => {},
}
}
}, :status => 200) and return
else
render(:json => audit.errors, :status => 500) and return
end
end
render(:json => { :message => "audit must be provided" }, :status => 500) and return
end
end
...
# Audit requests
get '/audits(.:format)', :to => 'audits#index'
post '/audits(.:format)', :to => 'audits#respond'
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment