From ed114f4c4c830f2161d30255deb9f1a0d85da503 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 15:52:36 +0100 Subject: [PATCH] Fix rubocop issues --- app/controllers/api/states_controller.rb | 26 +++++++++++++------ .../controllers/api/states_controller_spec.rb | 21 ++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/states_controller.rb b/app/controllers/api/states_controller.rb index 708e40d277..0ad0b991d4 100644 --- a/app/controllers/api/states_controller.rb +++ b/app/controllers/api/states_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Api class StatesController < Api::BaseController respond_to :json @@ -5,14 +7,7 @@ module Api skip_authorization_check def index - @states = scope.ransack(params[:q]).result. - includes(:country).order('name ASC') - - if params[:page] || params[:per_page] - @states = @states.page(params[:page]).per(params[:per_page]) - end - - render json: @states, each_serializer: Api::StateSerializer, status: :ok + render json: states, each_serializer: Api::StateSerializer, status: :ok end def show @@ -30,5 +25,20 @@ module Api Spree::State.all end end + + def states + states = scope.ransack(params[:q]).result. + includes(:country).order('name ASC') + + if pagination? + states = states.page(params[:page]).per(params[:per_page]) + end + + states + end + + def pagination? + params[:page] || params[:per_page] + end end end diff --git a/spec/controllers/api/states_controller_spec.rb b/spec/controllers/api/states_controller_spec.rb index 3badf4a518..f8372ea3bf 100644 --- a/spec/controllers/api/states_controller_spec.rb +++ b/spec/controllers/api/states_controller_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require 'spec_helper' module Api describe StatesController do render_views - let!(:state) { create(:state, :name => "Victoria") } + let!(:state) { create(:state, name: "Victoria") } let(:attributes) { [:id, :name, :abbr, :country_id] } let(:current_user) { create(:user) } @@ -33,26 +35,25 @@ module Api it "paginates when page parameter is passed through" do @scope.should_receive(:page).with(1).and_return(@scope) @scope.should_receive(:per).with(nil) - api_get :index, :page => 1 + api_get :index, page: 1 end it "paginates when per_page parameter is passed through" do @scope.should_receive(:page).with(nil).and_return(@scope) @scope.should_receive(:per).with(25) - api_get :index, :per_page => 25 + api_get :index, per_page: 25 end end - context "with two states" do - before { create(:state, :name => "New South Wales") } + before { create(:state, name: "New South Wales") } it "gets all states for a country" do - country = create(:country, :states_required => true) - state.country = country + country = create(:country, states_required: true) + state.country = country state.save - api_get :index, :country_id => country.id + api_get :index, country_id: country.id expect(json_response.first.symbolize_keys.keys).to include(*attributes) expect(json_response.count).to eq 1 end @@ -63,13 +64,13 @@ module Api end it 'can query the results through a paramter' do - api_get :index, :q => { :name_cont => 'Vic' } + api_get :index, q: { name_cont: 'Vic' } expect(json_response.first['name']).to eq("Victoria") end end it "can view a state" do - api_get :show, :id => state.id + api_get :show, id: state.id expect(json_response.symbolize_keys.keys).to include(*attributes) end end