Sha256: 0b08e6860795af9b667898c079cb65b23c5bc7c08b2af7693d2bda29f41e5166

Contents?: true

Size: 1.78 KB

Versions: 16

Compression:

Stored size: 1.78 KB

Contents

module Gaku
  class Admin::StatesController < Admin::BaseController

    respond_to :js,   only: %i( new create edit update destroy country_states index )
    respond_to :html, only: %i( edit update )

    before_action :set_state, only: %i( edit show update destroy )
    before_action :set_countries

    def country_states
      if params[:state][:country_iso].empty?
        states_for_default_country
      else
        @country = Country.where(iso: params[:state][:country_iso]).first
        @states = @country.states
        set_count @country
        respond_with @country
      end
    end

    def destroy
      @state.destroy
      respond_with @state
    end

    def new
      @state = State.new
      respond_with @state
    end

    def create
      @state = State.new(state_params)
      @state.save
      respond_with @state
    end

    def edit
    end

    def update
      @state.update(state_params)
      respond_with @state
    end

    def index
      @country = set_default_country
      set_count @country
      respond_with @country
    end

    private

    def states_for_default_country
      @country = set_default_country
      set_count @country
      if @country
        respond_with @country
      else
        render nothing: true
      end
    end

    def state_params
      params.require(:state).permit(attributes)
    end

    def attributes
      %i( name abbr name_ascii code country_iso )
    end

    def set_state
      @state = State.find(params[:id])
    end

    def set_countries
      @countries = Country.all
    end

    def set_count(object)
      @count = object.states.count unless object.nil?
    end

    def country_preset
      Preset.address('country')
    end

    def set_default_country
      Country.where(iso: country_preset).first
    end

  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
gaku_admin-0.3.0 app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.3.0.pre.4 app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.3.0.pre.3 app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.3.0.pre.2 app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.3.0.pre.1 app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.3.0.pre.0 app/controllers/gaku/admin/states_controller.rb
gaku-0.2.4 admin/app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.2.4 app/controllers/gaku/admin/states_controller.rb
gaku-0.2.3 admin/app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.2.3 app/controllers/gaku/admin/states_controller.rb
gaku-0.2.2 admin/app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.2.2 app/controllers/gaku/admin/states_controller.rb
gaku-0.2.1 admin/app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.2.1 app/controllers/gaku/admin/states_controller.rb
gaku-0.2.0 admin/app/controllers/gaku/admin/states_controller.rb
gaku_admin-0.2.0 app/controllers/gaku/admin/states_controller.rb