From 38750cc9c000ee22a5d460f358125ecb3b3023b6 Mon Sep 17 00:00:00 2001 From: Gabriela Navarro Date: Tue, 3 Feb 2015 17:12:33 -0200 Subject: [PATCH] Add articles on the JSON. --- controllers/colab_integration_plugin_controller.rb | 15 +++++++++------ lib/ext/article.rb | 20 ++++++++++++++++++++ lib/ext/profile.rb | 18 ++++++++++-------- 3 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 lib/ext/article.rb diff --git a/controllers/colab_integration_plugin_controller.rb b/controllers/colab_integration_plugin_controller.rb index 27a4de1..976bfb4 100644 --- a/controllers/colab_integration_plugin_controller.rb +++ b/controllers/colab_integration_plugin_controller.rb @@ -1,11 +1,14 @@ class ColabIntegrationPluginController < ApplicationController def index - render json: { + data = { "total" => environment.profiles.count, - "profiles" => - environment.profiles.each do |profile| - profile.attr_to_json - end - }.to_json + "profiles" => [] + } + + environment.profiles.each do |profile| + data["profiles"] << profile.attr_to_json + end + + render json: data.to_json end end diff --git a/lib/ext/article.rb b/lib/ext/article.rb new file mode 100644 index 0000000..a2c2665 --- /dev/null +++ b/lib/ext/article.rb @@ -0,0 +1,20 @@ +require_dependency "article" + +class Article + def attr_to_json + { + "type" => self.type, + "data" => { + "id" => self.id, + "parent_id" => self.parent_id, + "title" => self.title, + "body" => self.body, + "path" => self.path, + "slug" => self.slug, + "published" => self.published, + "show_to_followers" => self.show_to_followers, + "author_id" => self.author_id + } + } + end +end diff --git a/lib/ext/profile.rb b/lib/ext/profile.rb index aa66a26..6120644 100644 --- a/lib/ext/profile.rb +++ b/lib/ext/profile.rb @@ -2,19 +2,21 @@ require_dependency 'profile' class Profile def attr_to_json - { + attrs = { "type" => "#{self.type}", "data" => { "id" => "#{self.id}", "identifier" => "#{self.identifier}", "name" => "#{self.name}", "description" => "#{self.description}" - }, - # "articles" => [ - # self.articles.each do |article| - # { article.attr_to_json } - # end - # ] - }.to_json + } + } + + attrs['articles'] = [] + self.articles.each do |article| + attrs['articles'] << article.attr_to_json + end + + attrs end end -- libgit2 0.21.2