/api/app/controllers/spree/api/v1/states_controller.rb
Ruby | 37 lines | 32 code | 5 blank | 0 comment | 4 complexity | e2e64c460bcdd31b518af983eb0bf881 MD5 | raw file
- module Spree
- module Api
- module V1
- class StatesController < Spree::Api::BaseController
- skip_before_action :authenticate_user
- def index
- @states = scope.ransack(params[:q]).result.includes(:country)
- if params[:page] || params[:per_page]
- @states = @states.page(params[:page]).per(params[:per_page])
- end
- state = @states.last
- if stale?(state)
- respond_with(@states)
- end
- end
- def show
- @state = scope.find(params[:id])
- respond_with(@state)
- end
- private
- def scope
- if params[:country_id]
- @country = Country.accessible_by(current_ability, :read).find(params[:country_id])
- return @country.states.accessible_by(current_ability, :read).order('name ASC')
- else
- return State.accessible_by(current_ability, :read).order('name ASC')
- end
- end
- end
- end
- end
- end