Skip to content

Commit 9ae5275

Browse files
committed
add description
1 parent c77b2cf commit 9ae5275

File tree

10 files changed

+64
-5
lines changed

10 files changed

+64
-5
lines changed

app/controllers/base_libraries_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ def set_library
5959

6060
# Only allow a list of trusted parameters through.
6161
def library_params
62-
params.require(:library).permit(:name, :source_url, :user_id)
62+
params.require(:library).permit(:name, :description, :source_url, :user_id)
6363
end
6464
end

app/models/library.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Library < ApplicationRecord
1111
has_many :users, through: :library_users
1212

1313
pg_search_scope :search_by_name,
14-
against: %i[name],
14+
against: %i[name description],
1515
using: {
1616
tsearch: { prefix: true, dictionary: 'english',
1717
tsvector_column: 'search_vector' } # This option allows partial matches

app/views/libraries/_form.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
1717
</div>
1818

19+
<div class="my-5">
20+
<%= form.label :description %>
21+
<%= form.text_area :description, rows: 3, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
22+
</div>
23+
1924
<div class="my-5">
2025
<%= form.label 'Help URL (A link to more details about the library documents)' %>
2126
<%= form.text_field :source_url, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>

app/views/libraries/_library.html.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<div id="<%= dom_id library %>">
22
<div class="flex justify-between my-3">
3-
<h2 class="w-1/4"><%= link_to library.name, library, class: "hover:underline text-sky-500 font-medium" %></h2>
3+
<div class="w-1/2">
4+
<h2><%= link_to library.name, library, class: "hover:underline text-sky-500 font-medium" %></h2>
5+
<% if library.description.present? %>
6+
<p class="text-sm text-gray-600 mt-1"><%= truncate(library.description, length: 100) %></p>
7+
<% end %>
8+
</div>
49
<div>Created <%= time_ago_in_words(library.created_at) %> ago</div>
510
<div class="w-1/4">
611
<%= link_to library.documents.count.to_s + " documents", library_documents_path(library.id), class:"text-sky-500 hover:underline" %>

app/views/libraries/show.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
<%= render partial: 'shared/button_group', locals: { buttons: buttons } %>
1616
</div>
1717
</div>
18+
<% if @library.description.present? %>
19+
<div class="mt-4 p-4 bg-stone-200 rounded-lg text-sm">
20+
<p class="text-stone-600"><%= simple_format(@library.description) %></p>
21+
</div>
22+
<% end %>
1823
<div class="py-2 text-stone-500 text-xs ">
1924
Owner <span class="text-stone-500 font-bold "><%= (@library.user.email) %> </span> |
2025
Created <span class="text-stone-500 font-bold "><%= time_ago_in_words(@library.created_at) %> ago</span> |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddDescriptionToLibraries < ActiveRecord::Migration[7.2]
2+
def change
3+
add_column :libraries, :description, :text
4+
end
5+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class UpdateLibrariesSearchVector < ActiveRecord::Migration[7.2]
2+
def up
3+
# Drop the existing search_vector column
4+
remove_column :libraries, :search_vector
5+
6+
# Add the new search_vector column that includes both name and description
7+
add_column :libraries, :search_vector, :virtual, type: :tsvector,
8+
as: "to_tsvector('english'::regconfig, COALESCE(name, '') || ' ' || COALESCE(description, ''))",
9+
stored: true
10+
11+
# Re-add the index
12+
add_index :libraries, :search_vector, using: :gin
13+
end
14+
15+
def down
16+
# Drop the current search_vector column
17+
remove_column :libraries, :search_vector
18+
19+
# Add back the original search_vector column (name only)
20+
add_column :libraries, :search_vector, :virtual, type: :tsvector,
21+
as: "to_tsvector('english'::regconfig, (name)::text)",
22+
stored: true
23+
24+
# Re-add the index
25+
add_index :libraries, :search_vector, using: :gin
26+
end
27+
end

db/schema.rb

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/factories/library.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Define attributes for Library model
66
# For example:
77
name { 'Main Library' }
8+
description { 'A comprehensive collection of documents and resources' }
89
association :user
910
end
1011
end

spec/models/library_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
library = build(:library, name: nil, user:)
1717
expect(library).not_to be_valid
1818
end
19+
20+
it 'is valid with a description' do
21+
library = build(:library, description: 'A test library description', user:)
22+
expect(library).to be_valid
23+
end
24+
25+
it 'is valid without a description' do
26+
library = build(:library, description: nil, user:)
27+
expect(library).to be_valid
28+
end
1929
end
2030

2131
describe 'associations' do

0 commit comments

Comments
 (0)