Namespace languages controller to admin

This commit is contained in:
maddiebaka 2023-10-25 16:25:29 -04:00
parent 206c4da31f
commit c2891a66f8
17 changed files with 43 additions and 41 deletions

View File

@ -1,4 +1,4 @@
class LanguagesController < ApplicationController class Admin::LanguagesController < ApplicationController
def index def index
@languages = Language.all @languages = Language.all
@ -12,7 +12,7 @@ class LanguagesController < ApplicationController
@language = Language.new(language_params) @language = Language.new(language_params)
if @language.save if @language.save
flash[:notice] = "Language successfully created." flash[:notice] = "Language successfully created."
redirect_to languages_path redirect_to admin_languages_path
else else
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
end end
@ -26,7 +26,7 @@ class LanguagesController < ApplicationController
@language = Language.find_by_id(params[:id]) @language = Language.find_by_id(params[:id])
if @language.update(language_params) if @language.update(language_params)
flash[:notice] = "Language successfully updated." flash[:notice] = "Language successfully updated."
redirect_to languages_path redirect_to admin_languages_path
else else
render :edit, status: :unprocessable_entity render :edit, status: :unprocessable_entity
end end
@ -36,7 +36,7 @@ class LanguagesController < ApplicationController
@language = Language.find_by_id(params[:id]) @language = Language.find_by_id(params[:id])
if @language.destroy if @language.destroy
flash[:notice] = "Language successfully deleted." flash[:notice] = "Language successfully deleted."
redirect_to languages_path redirect_to admin_languages_path
else else
render :index, status: :not_modified render :index, status: :not_modified
end end

View File

@ -1,4 +1,4 @@
<%= form_for language do |f| %> <%= form_for [:admin, language] do |f| %>
<%= f.label :name %> <%= f.label :name %>
<%= f.text_field :name %><br/> <%= f.text_field :name %><br/>
<br/> <br/>

View File

@ -0,0 +1,5 @@
<%= render "admin/languages/errors", language: @language %>
<h1>Edit Language</h1>
<%= render "admin/languages/form", language: @language %>

View File

@ -7,9 +7,9 @@
<% @languages.each do |language| %> <% @languages.each do |language| %>
<tr> <tr>
<td><%= language.name %></td> <td><%= language.name %></td>
<td><%= link_to "Edit", edit_language_path(language) %></td> <td><%= link_to "Edit", edit_admin_language_path(language) %></td>
<td> | </td> <td> | </td>
<td><%= link_to "Delete", language_path(language), data: { turbo_method: :delete, <td><%= link_to "Delete", admin_language_path(language), data: { turbo_method: :delete,
turbo_confirm: "Are you sure?" } %></td> turbo_confirm: "Are you sure?" } %></td>
</tr> </tr>
<% end %> <% end %>
@ -17,4 +17,4 @@
<br/> <br/>
<br/> <br/>
<%= link_to "New Language", new_language_path %> <%= link_to "New Language", new_admin_language_path %>

View File

@ -0,0 +1,5 @@
<%= render "admin/languages/errors", language: @language %>
<h1>New Language</h1>
<%= render "admin/languages/form", language: @language %>

View File

@ -1,4 +1,5 @@
<h1>Parts of Speech</h1> <h1>Parts of Speech</h1>
<table> <table>
<tr> <tr>
<td><b>Part of Speech</b></td> <td><b>Part of Speech</b></td>
@ -11,4 +12,3 @@
</tr> </tr>
<% end %> <% end %>
</table> </table>

View File

@ -1,5 +0,0 @@
<%= render "languages/errors", language: @language %>
<h1>Edit Language</h1>
<%= render "languages/form", language: @language %>

View File

@ -1,5 +0,0 @@
<%= render "languages/errors", language: @language %>
<h1>New Language</h1>
<%= render "languages/form", language: @language %>

View File

@ -17,7 +17,7 @@
<%= link_to "Dictionary", dictionary_index_path %> <%= link_to "Dictionary", dictionary_index_path %>
<% if user_signed_in? %> <% if user_signed_in? %>
<pre style="display: inline;">|</pre> <pre style="display: inline;">|</pre>
<%= link_to "Languages", languages_path %> <%= link_to "Languages", admin_languages_path %>
<% end %> <% end %>
</div> </div>
<div class="ms-auto p-2"> <div class="ms-auto p-2">

View File

@ -1,9 +1,11 @@
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :users devise_for :users
#get 'dictionary/index' #get 'dictionary/index'
resources :dictionary, only: [:index, :show] resources :dictionary, only: [:index, :show, :create]
resources :words, only: [:index, :show] resources :words, only: [:index, :show]
resources :languages namespace :admin do
resources :languages
end
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.

View File

@ -4,59 +4,59 @@ RSpec.describe "Languages", type: :request do
describe "GET :index" do describe "GET :index" do
it "returns http success" do it "returns http success" do
get "/languages/" get "/admin/languages/"
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end
it "lists all languages" do it "lists all languages" do
language = FactoryBot.create(:language) language = FactoryBot.create(:language)
get "/languages/" get "/admin/languages/"
expect(response.body).to include(language.name) expect(response.body).to include(language.name)
end end
it "has a new-language link" do it "has a new-language link" do
get "/languages/" get "/admin/languages/"
expect(response.body).to have_selector(%(a[href="#{new_language_path}"])) expect(response.body).to have_selector(%(a[href="#{new_admin_language_path}"]))
end end
it "has an edit button" do it "has an edit button" do
language = FactoryBot.create(:language) language = FactoryBot.create(:language)
get "/languages/" get "/admin/languages/"
expect(response.body).to have_link("Edit") expect(response.body).to have_link("Edit")
end end
it "has a delete button" do it "has a delete button" do
language = FactoryBot.create(:language) language = FactoryBot.create(:language)
get "/languages/" get "/admin/languages/"
expect(response.body).to have_link("Delete") expect(response.body).to have_link("Delete")
end end
end end
describe "GET :new" do describe "GET :new" do
it "returns http success" do it "returns http success" do
get "/languages/new" get "/admin/languages/new"
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end
it "has a form for a new language" do it "has a form for a new language" do
get "/languages/new" get "/admin/languages/new"
expect(response.body).to have_selector(%(input[name="language[name]"])) expect(response.body).to have_selector(%(input[name="language[name]"]))
end end
end end
describe "POST :create" do describe "POST :create" do
it "redirect to languages_path on success" do it "redirect to admin_languages_path on success" do
post "/languages", params: { language: { name: "Test Name" } } post "/admin/languages", params: { language: { name: "Test Name" } }
expect(response).to redirect_to(languages_path) expect(response).to redirect_to(admin_languages_path)
end end
it "creates a new language" do it "creates a new language" do
post "/languages", params: { language: { name: "Test Name" } } post "/admin/languages", params: { language: { name: "Test Name" } }
expect(Language.count).to eq(1) expect(Language.count).to eq(1)
end end
it "requires a name for a new language" do it "requires a name for a new language" do
post "/languages", params: { language: { name: nil } } post "/admin/languages", params: { language: { name: nil } }
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
end end
end end
@ -64,7 +64,7 @@ RSpec.describe "Languages", type: :request do
describe "GET :edit" do describe "GET :edit" do
before :each do before :each do
@language = FactoryBot.create(:language) @language = FactoryBot.create(:language)
get "/languages/#{@language.id}/edit" get "/admin/languages/#{@language.id}/edit"
end end
it "returns http success" do it "returns http success" do
@ -83,13 +83,13 @@ RSpec.describe "Languages", type: :request do
end end
it "updates a language" do it "updates a language" do
patch "/languages/#{@language.id}", params: @params patch "/admin/languages/#{@language.id}", params: @params
expect(response).to redirect_to(languages_path) expect(response).to redirect_to(admin_languages_path)
end end
it "requires params to update a language" do it "requires params to update a language" do
@params = { language: { name: nil, invalid: "oh no" } } @params = { language: { name: nil, invalid: "oh no" } }
patch "/languages/#{@language.id}", params: @params patch "/admin/languages/#{@language.id}", params: @params
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
end end
@ -99,7 +99,7 @@ RSpec.describe "Languages", type: :request do
it "deletes a language" do it "deletes a language" do
language = FactoryBot.create(:language) language = FactoryBot.create(:language)
language2 = FactoryBot.create(:language) language2 = FactoryBot.create(:language)
delete "/languages/#{language.id}" delete "/admin/languages/#{language.id}"
expect(Language.all).to eq([language2]) expect(Language.all).to eq([language2])
end end
end end

View File

@ -30,7 +30,7 @@ RSpec.describe "Root path", type: :request do
it "should have the 'Languages' link" do it "should have the 'Languages' link" do
sign_in FactoryBot.create(:user) sign_in FactoryBot.create(:user)
get root_path get root_path
expect(response.body).to have_selector(%(a[href="#{languages_path}"])) expect(response.body).to have_selector(%(a[href="#{admin_languages_path}"]))
end end
it "should welcome user by username" do it "should welcome user by username" do