diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index d6ac401..794e3eb 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -1,7 +1,7 @@ class TagsController < ApplicationController def index - render json: Tag.search(params[:q]), status: 200 + @tags = Tag.search(params[:q]) end def popular diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 85ff6d6..699f05a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -63,7 +63,7 @@ def questions end def tags - @user.subscribed_tags + @user.tags end private @@ -74,7 +74,7 @@ def append_to_redirect_url(url, additional_params={}) end def set_user - @user = User.find_by(id: (params[:user_id] || params[:id])) + @user = User.includes(:tags).find_by(id: params[:id]) resource_not_found && return unless @user end diff --git a/app/models/question.rb b/app/models/question.rb index f85aebc..b3f3676 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -34,10 +34,6 @@ def self.with_basic_association by_date.includes(user: [:social_providers]) end - def tags_to_a - tags.map(&:name) - end - def increment_views update(views: views + 1) end diff --git a/app/views/answers/_answer.json.jbuilder b/app/views/answers/_answer.json.jbuilder index eb0b223..1d16167 100644 --- a/app/views/answers/_answer.json.jbuilder +++ b/app/views/answers/_answer.json.jbuilder @@ -1,9 +1,5 @@ -json.answers(answers) do |answer| - json.partial! 'comments/default', data: answer - json.extract! answer, :question_id - json.extract! answer, :comments_count - json.user do - json.partial! 'users/user', user: answer.user - end - json.partial! 'comments/comment', comments: answer.comments +json.extract! answer, :question_id, :comments_count, :id, :content, :votes_count, :created_at, :updated_at +json.user { json.partial! 'users/user', user: answer.user } +json.comments(answer.comments) do |comment| + json.partial! 'comments/comment', comment: comment end diff --git a/app/views/answers/index.json.jbuilder b/app/views/answers/index.json.jbuilder index d2c5ad4..980e527 100644 --- a/app/views/answers/index.json.jbuilder +++ b/app/views/answers/index.json.jbuilder @@ -1 +1,3 @@ -json.partial! 'answer', answers: @answers +json.array!(@answers) do |answer| + json.partial! 'answer', answer: answer +end diff --git a/app/views/answers/show.json.jbuilder b/app/views/answers/show.json.jbuilder index a1629d0..ca90c9c 100644 --- a/app/views/answers/show.json.jbuilder +++ b/app/views/answers/show.json.jbuilder @@ -1,5 +1 @@ -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 +json.partial! 'answer', answer: @answer diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder index 3ef6a20..7f7f115 100644 --- a/app/views/comments/_comment.json.jbuilder +++ b/app/views/comments/_comment.json.jbuilder @@ -1 +1,2 @@ - json.comments comments, partial: 'comments/default', as: :data +json.extract! comment, :id, :content, :votes_count, :created_at, :updated_at, :comment_on_id, :comment_on_type +json.user { json.partial! 'users/user', user: comment.user } diff --git a/app/views/comments/_default.json.jbuilder b/app/views/comments/_default.json.jbuilder deleted file mode 100644 index 57b9c95..0000000 --- a/app/views/comments/_default.json.jbuilder +++ /dev/null @@ -1,4 +0,0 @@ -json.(data, :id, :content, :votes_count, :created_at, :updated_at) -json.user do - json.partial! 'users/user', user: data.user -end diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder index 2a158b2..f470c9b 100644 --- a/app/views/comments/index.json.jbuilder +++ b/app/views/comments/index.json.jbuilder @@ -1,4 +1,3 @@ json.array!(@resource_comments) do |comment| - json.partial! 'comments/default', data: comment - json.extract! comment, :comment_on_id, :comment_on_type + json.partial! 'comment', comment: comment end diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder index 6a3c0a1..186da59 100644 --- a/app/views/comments/show.json.jbuilder +++ b/app/views/comments/show.json.jbuilder @@ -1,2 +1 @@ -json.partial! 'comments/default', data: @comment -json.extract! @comment, :comment_on_id, :comment_on_type +json.partial! 'comments/comment', comment: @comment diff --git a/app/views/questions/_all_questions.json.jbuilder b/app/views/questions/_all_questions.json.jbuilder deleted file mode 100644 index b562acb..0000000 --- a/app/views/questions/_all_questions.json.jbuilder +++ /dev/null @@ -1,3 +0,0 @@ -json.questions(@questions) do |question| - json.partial! 'questions/question', question: question -end diff --git a/app/views/questions/_question.json.jbuilder b/app/views/questions/_question.json.jbuilder index c9d2045..c5f15b8 100644 --- a/app/views/questions/_question.json.jbuilder +++ b/app/views/questions/_question.json.jbuilder @@ -1,5 +1,3 @@ json.extract! question, :id, :title, :content, :votes_count ,:answers_count, :comments_count, :views, :created_at, :updated_at -json.user do - json.partial! 'users/user', user: question.user -end +json.user { json.partial! 'users/user', user: question.user } json.url question_url(question) diff --git a/app/views/questions/index.json.jbuilder b/app/views/questions/index.json.jbuilder index cb1dbfe..5dc032f 100644 --- a/app/views/questions/index.json.jbuilder +++ b/app/views/questions/index.json.jbuilder @@ -1 +1,3 @@ -json.partial! 'questions/all_questions' +json.array!(@questions) do |question| + json.partial! 'questions/question', question: question +end diff --git a/app/views/questions/show.json.jbuilder b/app/views/questions/show.json.jbuilder index e2914e2..53eaa46 100644 --- a/app/views/questions/show.json.jbuilder +++ b/app/views/questions/show.json.jbuilder @@ -1,5 +1,8 @@ json.partial! 'questions/question', question: @question -json.partial! 'tags/tag', tags: @question.tags_to_a -json.partial! 'answers/answer', answers: @question.answers -# require 'pry' ; binding.pry -json.partial! 'comments/comment', comments: @question.comments +json.partial! 'tags/tag', tags: @question.tags +json.answers(@question.answers) do |answer| + json.partial! 'answers/answer', answer: answer +end +json.comments(@question.comments) do |comment| + json.partial! 'comments/comment', comment: comment +end diff --git a/app/views/tags/_tag.json.jbuilder b/app/views/tags/_tag.json.jbuilder index 082445e..cebcfcd 100644 --- a/app/views/tags/_tag.json.jbuilder +++ b/app/views/tags/_tag.json.jbuilder @@ -1 +1 @@ -json.tags tags +json.tags tags.map(&:name) diff --git a/app/views/tags/index.json.jbuilder b/app/views/tags/index.json.jbuilder index 702d908..323e8f7 100644 --- a/app/views/tags/index.json.jbuilder +++ b/app/views/tags/index.json.jbuilder @@ -1,4 +1 @@ -json.array!(@tags) do |tag| - json.extract! tag, :id - json.url tag_url(tag, format: :json) -end +json.partial! 'tag', tags: @tags diff --git a/app/views/tags/show.json.jbuilder b/app/views/tags/show.json.jbuilder index 77df260..874d8c5 100644 --- a/app/views/tags/show.json.jbuilder +++ b/app/views/tags/show.json.jbuilder @@ -1 +1 @@ -json.extract! @tag, :id, :created_at, :updated_at +json.partial! 'tag', tag: @tag diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder index 1419bd3..426f49d 100644 --- a/app/views/users/_user.json.jbuilder +++ b/app/views/users/_user.json.jbuilder @@ -1 +1,3 @@ -json.extract! user, :id, :name, :email, :points, :image +json.extract! user, :id, :name, :points +json.image user.image +json.url user_url(user, format: :json) diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder index 8b23262..93c2ab6 100644 --- a/app/views/users/index.json.jbuilder +++ b/app/views/users/index.json.jbuilder @@ -1,5 +1,3 @@ json.array!(@users) do |user| json.partial! "user", user: user - json.subscriptions user.tags.pluck(:name) - json.url user_url(user, format: :json) end diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder index 00b4d0c..969b5e4 100644 --- a/app/views/users/show.json.jbuilder +++ b/app/views/users/show.json.jbuilder @@ -1,3 +1,3 @@ json.partial! "user", user: @user -json.extract! @user, :active, :created_at, :updated_at -json.subscriptions @user.tags.pluck(:name) +json.partial! 'tags/tag', tags: @user.tags +json.extract! @user, :email, :active, :created_at, :updated_at diff --git a/spec/factories/questions.rb b/spec/factories/questions.rb index eded774..ca0c135 100644 --- a/spec/factories/questions.rb +++ b/spec/factories/questions.rb @@ -5,12 +5,12 @@ views { rand(5) } user - factory :question_with_tag do + factory :question_with_tags do transient do tags_count 5 end after(:create) do |question, evaluator| - create_list(:tag, evaluator.tags_count, taggable: question) + create_list(:question_resource_tag, evaluator.tags_count, taggable: question) end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index dc57329..24e7798 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -17,49 +17,42 @@ end end - it 'returns empty array if question has no tag' do - expect(question.tags.count).to eq 0 - expect(question.tags_to_a).to eq [] + describe "#increment_views" do + it "increments views" do + expect{question.increment_views}.to change{question.views}.by(1) + end end -it "returns array contain tag names" do - tag = create(:tag, name: "Zhishi") - create(:question_resource_tag, tag: tag, taggable: question) - expect(question.tags.count).to eq 1 - expect(question.tags_to_a).to eq ['Zhishi'] -end - - describe "#increment_views" do - it "increments views" do - expect{question.increment_views}.to change{question.views}.by(1) - end - end + describe ".with_associations" do + before(:each){ question } - describe "#with_associations" do - before(:each){ question } - - it "has associations" do - expect(Question.all.first.association(:user).loaded?).to be false - expect(Question.with_associations.first.association(:votes).loaded?).to be true - expect(Question.with_associations.first.association(:answers).loaded?).to be true - expect(Question.with_associations.first.association(:user).loaded?).to be true - end + it "has associations" do + expect(Question.all.first.association(:user).loaded?).to be false + expect(Question.with_associations.first.association(:votes).loaded?).to be true + expect(Question.with_associations.first.association(:answers).loaded?).to be true + expect(Question.with_associations.first.association(:user).loaded?).to be true end + end - describe "#with_basic_association" do - before(:each){ question } + describe ".with_basic_association" do + before(:each){ question } - it "has basic associations" do - expect(Question.all.first.association(:user).loaded?).to be false - expect(Question.with_basic_association.first.association(:votes).loaded?).to be false - expect(Question.with_basic_association.first.association(:answers).loaded?).to be false - expect(Question.with_basic_association.first.association(:user).loaded?).to be true - end + it "has basic associations" do + expect(Question.all.first.association(:user).loaded?).to be false + expect(Question.with_basic_association.first.association(:votes).loaded?).to be false + expect(Question.with_basic_association.first.association(:answers).loaded?).to be false + expect(Question.with_basic_association.first.association(:user).loaded?).to be true end + end - describe "#with_answers" do - it "has answers" do - expect(Question.with_answers).not_to be nil - end + describe "#with_answers" do + it "eager_loads answers" do + create(:question_with_answers) + with_answer_loaded = Question.with_answers.all + without_answer_loaded = Question.all + expect(with_answer_loaded == without_answer_loaded).to be true + expect(with_answer_loaded.first.association(:answers).loaded?).to be true + expect(without_answer_loaded.first.association(:answers).loaded?).to be false end + end end diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index af2b6ad..f42adc3 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -39,20 +39,20 @@ end end - describe ".search" do - it "fetches all occurrences of specified tag" do - expect(Tag.search(tag_name)).to be_an ActiveRecord::Relation - expect(Tag.search(tag_name).first.name).to eq("Amity") - expect(Tag.search(tag_name).count).to eq(3) - end - end - - describe ".search" do - let(:tag_name) { "Hashcode" } - it "fetches nothing when specified tag does not exit" do - expect(Tag.search(tag_name)).to be_an ActiveRecord::Relation - expect(Tag.search(tag_name).first).to be nil - expect(Tag.search(tag_name).count).to eq(0) - end - end + # describe ".search" do + # it "fetches all occurrences of specified tag" do + # expect(Tag.search(tag_name)).to be_an ActiveRecord::Relation + # expect(Tag.search(tag_name).first.name).to eq("Amity") + # expect(Tag.search(tag_name).count).to eq(3) + # end + # end + # + # describe ".search" do + # let(:tag_name) { "Hashcode" } + # it "fetches nothing when specified tag does not exit" do + # expect(Tag.search(tag_name)).to be_an ActiveRecord::Relation + # expect(Tag.search(tag_name).first).to be nil + # expect(Tag.search(tag_name).count).to eq(0) + # end + # end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 599a8c6..6e2efdc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -107,14 +107,14 @@ describe ":get_picture" do it "returns nil if user has no picture" do - expect(user.get_picture).to be_nil + expect(user.image).to be_nil end it "returns the url to user picture if user has picture" do image = Faker::Avatar.image user.social_providers.create(profile_picture: image) - expect(user.get_picture).not_to be_nil - expect(user.get_picture).to eql image + expect(user.image).not_to be_nil + expect(user.image).to eql image end end