Add resource "country_states"
This commit is contained in:
parent
26f2c3d503
commit
37b46cfa60
8 changed files with 137 additions and 0 deletions
22
app/controllers/country_states_controller.rb
Normal file
22
app/controllers/country_states_controller.rb
Normal file
|
@ -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
|
17
app/policies/country_state_policy.rb
Normal file
17
app/policies/country_state_policy.rb
Normal file
|
@ -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
|
28
app/views/country_states/index.html.erb
Normal file
28
app/views/country_states/index.html.erb
Normal file
|
@ -0,0 +1,28 @@
|
|||
<div class="container">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">
|
||||
<%= CountryState.human_attribute_name :name %>
|
||||
</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @country_states.each do |country_state| %>
|
||||
<tr>
|
||||
<td><%= country_state.name %></td>
|
||||
<td>
|
||||
<% if policy(country_state).show? %>
|
||||
<%= link_to country_state,
|
||||
role: :button, class: 'btn btn-light btn-sm' do %>
|
||||
<i class="far fa-eye"></i>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
3
app/views/country_states/show.html.erb
Normal file
3
app/views/country_states/show.html.erb
Normal file
|
@ -0,0 +1,3 @@
|
|||
<div class="container">
|
||||
<h1><%= @country_state.name %></h1>
|
||||
</div>
|
|
@ -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 #
|
||||
###############
|
||||
|
|
28
spec/policies/country_state_policy_spec.rb
Normal file
28
spec/policies/country_state_policy_spec.rb
Normal file
|
@ -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
|
19
spec/requests/country_states/index_spec.rb
Normal file
19
spec/requests/country_states/index_spec.rb
Normal file
|
@ -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
|
18
spec/requests/country_states/show_spec.rb
Normal file
18
spec/requests/country_states/show_spec.rb
Normal file
|
@ -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
|
Reference in a new issue