diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 0bc3d0e..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/Gemfile b/Gemfile index 5bec39b..a1266fd 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ gem 'figaro' gem 'ancestry' gem 'will_paginate' # gem 'kaminari' -gem "active_model_serializers", github: "rails-api/active_model_serializers" +gem 'jbuilder' gem "jwt" gem 'unicorn-rails' gem 'rack-cors' diff --git a/Gemfile.lock b/Gemfile.lock index e653c0b..a4639b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,3 @@ -GIT - remote: git://github.com/rails-api/active_model_serializers.git - revision: b45f7b4ffea36b44fdf7be075295ad9db6bea1b0 - specs: - active_model_serializers (0.10.0.rc4) - actionpack (>= 4.0) - activemodel (>= 4.0) - railties (>= 4.0) - GEM remote: https://rubygems.org/ specs: @@ -63,6 +54,9 @@ GEM activesupport (>= 4.1.0) hashie (3.4.3) i18n (0.7.0) + jbuilder (2.4.1) + activesupport (>= 3.0.0, < 5.1) + multi_json (~> 1.2) json (1.8.3) jwt (1.5.1) kgio (2.10.0) @@ -184,10 +178,10 @@ PLATFORMS ruby DEPENDENCIES - active_model_serializers! ancestry faker figaro + jbuilder jwt omniauth-google-oauth2 omniauth-oauth2 (~> 1.3.1) diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index 96476a6..7bc7f42 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -5,12 +5,9 @@ class AnswersController < ApplicationController def index @answers = @question.answers.with_votes - - render json: @answers end def show - render json: @answer end def create @@ -18,7 +15,7 @@ def create @answer.user = current_user if @answer.save - render json: @answer, status: :created, location: question_answer_path(@question, @answer) + render :show else invalid_request(error_msg) end @@ -26,7 +23,7 @@ def create def update if @answer.update(answer_params) - render json: @answer, status: 200 + render :show else invalid_request(error_msg) end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 064ce93..bd1e6bd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,5 @@ class ApplicationController < ActionController::API include ActionController::HttpAuthentication::Token::ControllerMethods - include ActionController::Serialization attr_reader :current_user before_action :authenticate_user diff --git a/app/serializers/answer_serializer.rb b/app/serializers/answer_serializer.rb deleted file mode 100644 index 227cefc..0000000 --- a/app/serializers/answer_serializer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class AnswerSerializer < MainSerializer - attributes :question_id, :comments_count, :comments - has_many :comments -end diff --git a/app/serializers/comment_serializer.rb b/app/serializers/comment_serializer.rb deleted file mode 100644 index 9df92e7..0000000 --- a/app/serializers/comment_serializer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class CommentSerializer < MainSerializer - attributes :user - belongs_to :user -end diff --git a/app/serializers/main_serializer.rb b/app/serializers/main_serializer.rb deleted file mode 100644 index 71964ca..0000000 --- a/app/serializers/main_serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -class MainSerializer < ActiveModel::Serializer - attributes :id, :content, :votes_count, :voted, :created_at, :updated_at - belongs_to :user - - def voted - object.votes.voted?(object.class.to_s, object.id, object.user) - end -end diff --git a/app/serializers/question_serializer.rb b/app/serializers/question_serializer.rb deleted file mode 100644 index 908ab4b..0000000 --- a/app/serializers/question_serializer.rb +++ /dev/null @@ -1,12 +0,0 @@ -class QuestionSerializer < MainSerializer - attributes :title, :tags, :answers_count, :comments_count, :views - - def attributes(*args) - class_eval { has_many :comments; has_many :answers } if instance_options[:include_answers] - super - end - - def answers - object.answers.with_votes - end -end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb deleted file mode 100644 index aea5eae..0000000 --- a/app/serializers/user_serializer.rb +++ /dev/null @@ -1,13 +0,0 @@ -class UserSerializer < ActiveModel::Serializer - attributes :id, :name, :email, :image, :points - - def attributes(*args) - data = super - data['api_key'] = instance_options[:api_key] if instance_options[:api_key] - data - end - - def image - object.social_providers.first.try(:profile_picture) - end -end diff --git a/app/views/answers/_answer.json.jbuilder b/app/views/answers/_answer.json.jbuilder new file mode 100644 index 0000000..9c6f52b --- /dev/null +++ b/app/views/answers/_answer.json.jbuilder @@ -0,0 +1,7 @@ +json.answers(answers) do |answer| + json.partial! 'comments/default', data: answer + json.extract! answer, :question_id + json.extract! answer, :comments_count + json.partial! 'users/user', user: answer.user + json.partial! 'comments/comment', comments: answer.comments +end diff --git a/app/views/answers/index.json.jbuilder b/app/views/answers/index.json.jbuilder new file mode 100644 index 0000000..d2c5ad4 --- /dev/null +++ b/app/views/answers/index.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'answer', answers: @answers diff --git a/app/views/answers/show.json.jbuilder b/app/views/answers/show.json.jbuilder new file mode 100644 index 0000000..a1629d0 --- /dev/null +++ b/app/views/answers/show.json.jbuilder @@ -0,0 +1,5 @@ +json.partial! 'comments/default', data: @answer +json.extract! @answer, :question_id +json.extract! @answer, :comments_count +json.partial! 'users/user', user: @answer.user +json.partial! 'comments/comment', comments: @answer.comments diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder new file mode 100644 index 0000000..3ef6a20 --- /dev/null +++ b/app/views/comments/_comment.json.jbuilder @@ -0,0 +1 @@ + json.comments comments, partial: 'comments/default', as: :data diff --git a/app/views/comments/_default.json.jbuilder b/app/views/comments/_default.json.jbuilder new file mode 100644 index 0000000..8b1499d --- /dev/null +++ b/app/views/comments/_default.json.jbuilder @@ -0,0 +1,2 @@ +json.(data, :id, :content, :votes_count, :created_at, :updated_at) +json.partial! 'users/user', user: data.user diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder new file mode 100644 index 0000000..66d72b2 --- /dev/null +++ b/app/views/comments/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@comments) do |comment| + json.extract! comment, :id + json.url comment_url(comment, format: :json) +end diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder new file mode 100644 index 0000000..ca935ec --- /dev/null +++ b/app/views/comments/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @comment, :id, :created_at, :updated_at diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder new file mode 100644 index 0000000..5e2b759 --- /dev/null +++ b/app/views/users/_user.json.jbuilder @@ -0,0 +1 @@ +json.user user diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder new file mode 100644 index 0000000..6684a9c --- /dev/null +++ b/app/views/users/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@users) do |user| + json.extract! user, :id + json.url user_url(user, format: :json) +end diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder new file mode 100644 index 0000000..80ed63e --- /dev/null +++ b/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @user, :id, :created_at, :updated_at diff --git a/docs/.DS_Store b/docs/.DS_Store deleted file mode 100644 index 143da29..0000000 Binary files a/docs/.DS_Store and /dev/null differ