Created
April 27, 2015 17:59
-
-
Save yozlet/a9560c1d1d617845686e to your computer and use it in GitHub Desktop.
JSON admin refactor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
class AdminController < ApplicationController | |
layout 'dashboard' | |
before_filter :require_admin! | |
def index | |
@applications = Doorkeeper::Application.includes(:authorizations) | |
respond_to do |format| | |
format.html do | |
@applications = @applications. | |
filter(params[:filter]). | |
search(params[:search]). | |
paginate(page: params[:page], per_page: 8) | |
end | |
format.json do | |
json_string = {applications: @applications.all}.as_json(methods: :number_of_authorizations) | |
render(json: json_string, status: 200) | |
end | |
format.csv do | |
render(text: generate_csv(@applications), status: 200) | |
end | |
end | |
end | |
private | |
def generate_csv(applications) | |
CSV.generate do |csv| | |
csv << Doorkeeper::Application.attribute_names | |
applications.all.each do |application| | |
csv << application.attributes.values | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment