diff --git a/app/controllers/api/taxons_controller.rb b/app/controllers/api/taxons_controller.rb index d56c7a2b63..c20097e0f6 100644 --- a/app/controllers/api/taxons_controller.rb +++ b/app/controllers/api/taxons_controller.rb @@ -1,38 +1,36 @@ -module Spree - module Api - class TaxonsController < ::Api::BaseController - respond_to :json +module Api + class TaxonsController < Api::BaseController + respond_to :json - skip_authorization_check only: :index + skip_authorization_check only: :index - def index - if taxonomy - @taxons = taxonomy.root.children + def index + if taxonomy + @taxons = taxonomy.root.children + else + if params[:ids] + @taxons = Spree::Taxon.where(id: params[:ids].split(",")) else - if params[:ids] - @taxons = Taxon.where(id: params[:ids].split(",")) - else - @taxons = Taxon.ransack(params[:q]).result - end + @taxons = Spree::Taxon.ransack(params[:q]).result end - render json: @taxons, each_serializer: ::Api::TaxonSerializer end + render json: @taxons, each_serializer: Api::TaxonSerializer + end - def show - @taxon = taxon - respond_with(@taxon) - end + def show + @taxon = taxon + respond_with(@taxon) + end - def jstree - show - end + def jstree + show + end - private + private - def taxonomy - return if params[:taxonomy_id].blank? - @taxonomy ||= Taxonomy.find(params[:taxonomy_id]) - end + def taxonomy + return if params[:taxonomy_id].blank? + @taxonomy ||= Spree::Taxonomy.find(params[:taxonomy_id]) end end end diff --git a/spec/controllers/api/taxons_controller_spec.rb b/spec/controllers/api/taxons_controller_spec.rb index fa7068ccd3..884fc17bec 100644 --- a/spec/controllers/api/taxons_controller_spec.rb +++ b/spec/controllers/api/taxons_controller_spec.rb @@ -1,73 +1,69 @@ require 'spec_helper' -module Spree - describe Api::TaxonsController do - render_views +describe Api::TaxonsController do + render_views - let(:taxonomy) { create(:taxonomy) } - let(:taxon) { create(:taxon, name: "Ruby", taxonomy: taxonomy) } - let(:taxon2) { create(:taxon, name: "Rails", taxonomy: taxonomy) } - let(:attributes) { - ["id", "name", "pretty_name", "permalink", "position", "parent_id", "taxonomy_id"] - } + let(:taxonomy) { create(:taxonomy) } + let(:taxon) { create(:taxon, name: "Ruby", taxonomy: taxonomy) } + let(:taxon2) { create(:taxon, name: "Rails", taxonomy: taxonomy) } + let(:attributes) { + ["id", "name", "pretty_name", "permalink", "position", "parent_id", "taxonomy_id"] + } - before do - allow(controller).to receive(:spree_current_user) { current_api_user } + before do + allow(controller).to receive(:spree_current_user) { current_api_user } - taxon2.children << create(:taxon, name: "3.2.2", taxonomy: taxonomy) - taxon.children << taxon2 - taxonomy.root.children << taxon + taxon2.children << create(:taxon, name: "3.2.2", taxonomy: taxonomy) + taxon.children << taxon2 + taxonomy.root.children << taxon + end + + context "as a normal user" do + let(:current_api_user) { build(:user) } + + it "gets all taxons for a taxonomy" do + api_get :index, taxonomy_id: taxonomy.id + + expect(json_response.first['name']).to eq taxon.name + + children = json_response.first['taxons'] + expect(children.count).to eq 1 + expect(children.first['name']).to eq taxon2.name + expect(children.first['taxons'].count).to eq 1 end - context "as a normal user" do - let(:current_api_user) { build(:user) } + it "gets all taxons" do + api_get :index - it "gets all taxons for a taxonomy" do - api_get :index, taxonomy_id: taxonomy.id + expect(json_response.first['name']).to eq taxonomy.root.name - expect(json_response.first['name']).to eq taxon.name + children = json_response.first['taxons'] + expect(children.count).to eq 1 + expect(children.first['name']).to eq taxon.name + expect(children.first['taxons'].count).to eq 1 + end - # WIP maybe not needed - #children = json_response.first['taxons'] - #expect(children.count).to eq 1 - #expect(children.first['name']).to eq taxon2.name - #expect(children.first['taxons'].count).to eq 1 - end + it "can search for a single taxon" do + api_get :index, q: { name_cont: "Ruby" } - it "gets all taxons" do - api_get :index + expect(json_response.count).to eq(1) + expect(json_response.first['name']).to eq "Ruby" + end - expect(json_response.first['name']).to eq taxonomy.root.name + it "gets a single taxon" do + api_get :show, id: taxon.id, taxonomy_id: taxonomy.id - # WIP maybe not needed - #children = json_response.first['taxons'] - #expect(children.count).to eq 1 - #expect(children.first['name']).to eq taxon.name - #expect(children.first['taxons'].count).to eq 1 - end + expect(json_response['name']).to eq taxon.name + expect(json_response['taxons'].count).to eq 1 + end - it "can search for a single taxon" do - api_get :index, q: { name_cont: "Ruby" } + it "gets all taxons in JSTree form" do + api_get :jstree, taxonomy_id: taxonomy.id, id: taxon.id - expect(json_response.count).to eq(1) - expect(json_response.first['name']).to eq "Ruby" - end - - it "gets a single taxon" do - api_get :show, id: taxon.id, taxonomy_id: taxonomy.id - - expect(json_response['name']).to eq taxon.name - expect(json_response['taxons'].count).to eq 1 - end - - it "gets all taxons in JSTree form" do - api_get :jstree, taxonomy_id: taxonomy.id, id: taxon.id - - response = json_response.first - response["data"].should eq(taxon2.name) - response["attr"].should eq("name" => taxon2.name, "id" => taxon2.id) - response["state"].should eq("closed") - end + response = json_response.first + response["data"].should eq(taxon2.name) + response["attr"].should eq("name" => taxon2.name, "id" => taxon2.id) + response["state"].should eq("closed") end end end