Dictionary show action for letters, alphabetical links helper

This commit is contained in:
maddiebaka 2023-10-20 14:29:34 -04:00
parent 6202a6e778
commit c5cbffb4e5
9 changed files with 93 additions and 1 deletions

View File

@ -51,6 +51,7 @@ group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri windows ] gem "debug", platforms: %i[ mri windows ]
gem "rspec-rails" gem "rspec-rails"
gem "capybara"
gem "rails-controller-testing" gem "rails-controller-testing"
gem "factory_bot_rails" gem "factory_bot_rails"
end end

View File

@ -83,6 +83,15 @@ GEM
bootsnap (1.16.0) bootsnap (1.16.0)
msgpack (~> 1.2) msgpack (~> 1.2)
builder (3.2.4) builder (3.2.4)
capybara (3.39.2)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
concurrent-ruby (1.2.2) concurrent-ruby (1.2.2)
connection_pool (2.4.1) connection_pool (2.4.1)
crass (1.0.6) crass (1.0.6)
@ -145,6 +154,7 @@ GEM
net-pop net-pop
net-smtp net-smtp
marcel (1.0.2) marcel (1.0.2)
matrix (0.4.2)
mini_mime (1.1.5) mini_mime (1.1.5)
minitest (5.20.0) minitest (5.20.0)
msgpack (1.7.2) msgpack (1.7.2)
@ -216,6 +226,7 @@ GEM
rake (13.0.6) rake (13.0.6)
rdoc (6.5.0) rdoc (6.5.0)
psych (>= 4.0.0) psych (>= 4.0.0)
regexp_parser (2.8.2)
reline (0.3.9) reline (0.3.9)
io-console (~> 0.5) io-console (~> 0.5)
responders (3.1.1) responders (3.1.1)
@ -274,6 +285,8 @@ GEM
websocket-driver (0.7.6) websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0) websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5) websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.12) zeitwerk (2.6.12)
PLATFORMS PLATFORMS
@ -283,6 +296,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
bootsnap bootsnap
capybara
debug debug
devise devise
factory_bot_rails factory_bot_rails

View File

@ -3,4 +3,9 @@ class DictionaryController < ApplicationController
@parts_of_speech = PartOfSpeech.all @parts_of_speech = PartOfSpeech.all
@words = Word.all @words = Word.all
end end
def show
@letter = params[:id]
@words = Word.where('substr(word, 1, 1) = ?', @letter)
end
end end

View File

@ -1,2 +1,10 @@
module DictionaryHelper module DictionaryHelper
LETTERS = ['i', 'u', 'e', 'o', 'a', 'm', 'n', 'p', 't', 'k', 's', 'w', 'l', 'j'].sort
def alphabetical_links
LETTERS.map do |l|
link_to l.to_s, dictionary_path(l)
end.join("\n")
end
end end

View File

@ -1,6 +1,8 @@
<h1>Dictionary#index</h1> <h1>Dictionary#index</h1>
<p>Find me in app/views/dictionary/index.html.erb</p> <p>Find me in app/views/dictionary/index.html.erb</p>
<%= sanitize alphabetical_links, tags: ["a"] %>
<p><%= @parts_of_speech.count %> parts of speech entries in database</p> <p><%= @parts_of_speech.count %> parts of speech entries in database</p>
<h1>Parts of Speech</h1> <h1>Parts of Speech</h1>
<table> <table>

View File

@ -0,0 +1,9 @@
<div>
<%= @letter.to_s %>
</div>
<% @words.each do |word| %>
<div>
<h1><%= word.word %></h1>
</div>
<% end %>

View File

@ -1,7 +1,7 @@
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] resources :dictionary, only: [:index, :show]
resources :words, only: [:index, :show] resources :words, only: [:index, :show]
# 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

View File

@ -0,0 +1,36 @@
require 'rails_helper'
include Capybara::RSpecMatchers
# Specs in this file have access to a helper object that includes
# the DictionaryHelper. For example:
#
# describe DictionaryHelper 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 DictionaryHelper, type: :helper do
describe "#alphabetical_links" do
it "generates fourteen letter-links" do
expect(helper.alphabetical_links).to have_selector(%(a), count: 14)
end
it "generates links for all fourteen letters" do
output = helper.alphabetical_links
letters = ['i', 'u', 'e', 'o', 'a', 'm', 'n', 'p', 't', 'k', 's', 'w', 'l', 'j']
letters.each do |l|
expect(output).to have_link(l, href: dictionary_path(l))
end
end
it "sorts the toki pona alphabet" do
expect(DictionaryHelper::LETTERS).to eql(DictionaryHelper::LETTERS.sort)
end
end
end

View File

@ -7,4 +7,21 @@ RSpec.describe "Dictionary", type: :request do
expect(response).to render_template(:index) expect(response).to render_template(:index)
end end
end end
describe "GET /show" do
it 'renders the show template' do
get "/dictionary/a"
expect(response).to render_template(:show)
end
it "sets @letter instance variable" do
get "/dictionary/a"
expect(assigns(:letter)).to be_a(String)
end
it "sets @words instance variable" do
get "/dictionary/a"
expect(assigns(:words)).to be_a(ActiveRecord::Relation)
end
end
end end