From 6202a6e778bd4ad846d927533dcab3ace5034cfb Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Sat, 14 Oct 2023 13:50:50 -0400 Subject: [PATCH] Add words controller with index and show actions --- app/controllers/words_controller.rb | 10 ++++++ app/helpers/word_helper.rb | 2 ++ app/views/application/_definitions.html.erb | 12 +++++++ app/views/dictionary/index.html.erb | 4 ++- app/views/words/index.html.erb | 3 ++ app/views/words/show.html.erb | 3 ++ config/routes.rb | 1 + spec/controllers/words_spec.rb | 22 ++++++++++++ spec/helpers/word_helper_spec.rb | 15 ++++++++ spec/requests/word_spec.rb | 39 +++++++++++++++++++++ spec/requests/words_spec.rb | 11 ++++++ 11 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 app/controllers/words_controller.rb create mode 100644 app/helpers/word_helper.rb create mode 100644 app/views/application/_definitions.html.erb create mode 100644 app/views/words/index.html.erb create mode 100644 app/views/words/show.html.erb create mode 100644 spec/controllers/words_spec.rb create mode 100644 spec/helpers/word_helper_spec.rb create mode 100644 spec/requests/word_spec.rb create mode 100644 spec/requests/words_spec.rb diff --git a/app/controllers/words_controller.rb b/app/controllers/words_controller.rb new file mode 100644 index 0000000..33e9058 --- /dev/null +++ b/app/controllers/words_controller.rb @@ -0,0 +1,10 @@ +class WordsController < ApplicationController + + def index + @words = Word.all + end + + def show + @word = Word.find_by_id(params[:id]) + end +end diff --git a/app/helpers/word_helper.rb b/app/helpers/word_helper.rb new file mode 100644 index 0000000..000fecf --- /dev/null +++ b/app/helpers/word_helper.rb @@ -0,0 +1,2 @@ +module WordHelper +end diff --git a/app/views/application/_definitions.html.erb b/app/views/application/_definitions.html.erb new file mode 100644 index 0000000..9ce43cc --- /dev/null +++ b/app/views/application/_definitions.html.erb @@ -0,0 +1,12 @@ + + + + + +<% word.definitions.each do |definition| %> + + + + +<% end %> +
Part of SpeechDefinition
<%= definition.pos %><%= definition.definition %>
diff --git a/app/views/dictionary/index.html.erb b/app/views/dictionary/index.html.erb index cb53560..fa6e459 100644 --- a/app/views/dictionary/index.html.erb +++ b/app/views/dictionary/index.html.erb @@ -19,7 +19,8 @@

Words

<% @words.each do |word| %>

<%= word.word %>

- + <%= render "application/definitions", word: word %> + <% end %> diff --git a/app/views/words/index.html.erb b/app/views/words/index.html.erb new file mode 100644 index 0000000..f20756e --- /dev/null +++ b/app/views/words/index.html.erb @@ -0,0 +1,3 @@ +<% @words.each do |word| %> +

<%= link_to word.word, word %>

+<% end %> diff --git a/app/views/words/show.html.erb b/app/views/words/show.html.erb new file mode 100644 index 0000000..f605887 --- /dev/null +++ b/app/views/words/show.html.erb @@ -0,0 +1,3 @@ +

<%= @word.word %>

+ +<%= render "application/definitions", word: @word %> diff --git a/config/routes.rb b/config/routes.rb index 9a9074c..52b101c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ Rails.application.routes.draw do devise_for :users #get 'dictionary/index' resources :dictionary, only: [:index] + resources :words, only: [:index, :show] # 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. diff --git a/spec/controllers/words_spec.rb b/spec/controllers/words_spec.rb new file mode 100644 index 0000000..3881197 --- /dev/null +++ b/spec/controllers/words_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe WordsController, type: :controller do + describe "GET :index" do + it 'renders the index template' do + get :index + expect(response).to render_template(:index) + end + + it 'sets @words instance variable' do + FactoryBot.create(:word) + get :index + expect(assigns(:words)).to be_a(ActiveRecord::Relation) + end + + it 'fetches all words into @words' do + 5.times { FactoryBot.create(:word) } + get :index + expect(assigns(:words).count).to eq(5) + end + end +end diff --git a/spec/helpers/word_helper_spec.rb b/spec/helpers/word_helper_spec.rb new file mode 100644 index 0000000..9102335 --- /dev/null +++ b/spec/helpers/word_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the WordHelper. For example: +# +# describe WordHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe WordHelper, type: :helper do + #pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/word_spec.rb b/spec/requests/word_spec.rb new file mode 100644 index 0000000..77ed5dd --- /dev/null +++ b/spec/requests/word_spec.rb @@ -0,0 +1,39 @@ +require 'rails_helper' + +RSpec.describe "Words", type: :request do + describe "GET /index" do + + it "renders the index template" do + get "/words" + expect(response).to render_template(:index) + end + end + + describe "GET /show/:val" do + + before(:each) do + @word = FactoryBot.create(:word) + end + + it "renders the show template" do + get "/words/#{@word.id}" + expect(response).to render_template(:show) + end + + it "sets @word instance variable" do + get "/words/#{@word.id}" + expect(assigns(:word)).to be_a(Word) + end + + it "displays the word name" do + get "/words/#{@word.id}" + expect(response.body).to include(@word.word) + end + + it "displays the definitions" do + @definition = FactoryBot.create(:definition) + get "/words/#{@definition.word_id}" + expect(response.body).to include(@definition.definition) + end + end +end diff --git a/spec/requests/words_spec.rb b/spec/requests/words_spec.rb new file mode 100644 index 0000000..99b807f --- /dev/null +++ b/spec/requests/words_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Words", type: :request do + describe "GET /index" do + + it "renders the index template" do + get "/words" + expect(response).to render_template(:index) + end + end +end