From 37b46cfa60df3df12280808e3c8f95454af33276 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 1 Feb 2019 03:38:06 +0500 Subject: [PATCH] Add resource "country_states" --- app/controllers/country_states_controller.rb | 22 +++++++++++++++ app/policies/country_state_policy.rb | 17 ++++++++++++ app/views/country_states/index.html.erb | 28 ++++++++++++++++++++ app/views/country_states/show.html.erb | 3 +++ config/routes.rb | 2 ++ spec/policies/country_state_policy_spec.rb | 28 ++++++++++++++++++++ spec/requests/country_states/index_spec.rb | 19 +++++++++++++ spec/requests/country_states/show_spec.rb | 18 +++++++++++++ 8 files changed, 137 insertions(+) create mode 100644 app/controllers/country_states_controller.rb create mode 100644 app/policies/country_state_policy.rb create mode 100644 app/views/country_states/index.html.erb create mode 100644 app/views/country_states/show.html.erb create mode 100644 spec/policies/country_state_policy_spec.rb create mode 100644 spec/requests/country_states/index_spec.rb create mode 100644 spec/requests/country_states/show_spec.rb diff --git a/app/controllers/country_states_controller.rb b/app/controllers/country_states_controller.rb new file mode 100644 index 0000000..a00e205 --- /dev/null +++ b/app/controllers/country_states_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class CountryStatesController < ApplicationController + before_action :set_country_state, except: :index + + # GET /country_states + def index + authorize :country_state + @country_states = policy_scope(CountryState) + end + + # GET /country_states/:id + def show + authorize @country_state + end + +private + + def set_country_state + @country_state = CountryState.find params[:id] + end +end diff --git a/app/policies/country_state_policy.rb b/app/policies/country_state_policy.rb new file mode 100644 index 0000000..798a657 --- /dev/null +++ b/app/policies/country_state_policy.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class CountryStatePolicy < ApplicationPolicy + def index? + true + end + + def show? + true + end + + class Scope < Scope + def resolve + scope.all + end + end +end diff --git a/app/views/country_states/index.html.erb b/app/views/country_states/index.html.erb new file mode 100644 index 0000000..5fcfa75 --- /dev/null +++ b/app/views/country_states/index.html.erb @@ -0,0 +1,28 @@ +
+ + + + + + + + + + <% @country_states.each do |country_state| %> + + + + + <% end %> + +
+ <%= CountryState.human_attribute_name :name %> +
<%= country_state.name %> + <% if policy(country_state).show? %> + <%= link_to country_state, + role: :button, class: 'btn btn-light btn-sm' do %> + + <% end %> + <% end %> +
+
diff --git a/app/views/country_states/show.html.erb b/app/views/country_states/show.html.erb new file mode 100644 index 0000000..0341aac --- /dev/null +++ b/app/views/country_states/show.html.erb @@ -0,0 +1,3 @@ +
+

<%= @country_state.name %>

+
diff --git a/config/routes.rb b/config/routes.rb index ad81414..317d93e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,8 @@ Rails.application.routes.draw do get :join, to: 'membership_apps#new' post :join, to: 'membership_apps#create' + resources :country_states, only: %i[index show] + ############### # User routes # ############### diff --git a/spec/policies/country_state_policy_spec.rb b/spec/policies/country_state_policy_spec.rb new file mode 100644 index 0000000..87b4882 --- /dev/null +++ b/spec/policies/country_state_policy_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe CountryStatePolicy do + subject { described_class.new current_account, record } + + let :resolved_scope do + described_class::Scope.new(current_account, CountryState.all).resolve + end + + let!(:record) { create :country_state } + let!(:other_record) { create :country_state } + + before { create_list :country_state, 3 } + + for_account_types nil, :guest, :usual, :superuser do + it { is_expected.to permit_actions %i[index show] } + it { is_expected.to forbid_new_and_create_actions } + it { is_expected.to forbid_edit_and_update_actions } + it { is_expected.to forbid_action :destroy } + + specify { expect(resolved_scope).to eq CountryState.all } + + specify { expect(resolved_scope).to include record } + specify { expect(resolved_scope).to include other_record } + end +end diff --git a/spec/requests/country_states/index_spec.rb b/spec/requests/country_states/index_spec.rb new file mode 100644 index 0000000..4620c45 --- /dev/null +++ b/spec/requests/country_states/index_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /country_states' do + before do + sign_in current_account.user if current_account&.user + + create_list :country_state, 5 + + get '/country_states' + end + + for_account_types nil, :guest, :usual, :superuser do + specify do + expect(response).to have_http_status :ok + end + end +end diff --git a/spec/requests/country_states/show_spec.rb b/spec/requests/country_states/show_spec.rb new file mode 100644 index 0000000..de8b56f --- /dev/null +++ b/spec/requests/country_states/show_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /country_states/:id' do + let!(:country_state) { create :country_state } + + before do + sign_in current_account.user if current_account&.user + get "/country_states/#{country_state.id}" + end + + for_account_types nil, :guest, :usual, :superuser do + specify do + expect(response).to have_http_status :ok + end + end +end