From f3e3735c7efe9ba8fcc0e58cda4ee3996e12d38e Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 4 Mar 2025 22:47:31 -0500 Subject: Add quotes search --- Gemfile | 1 + Gemfile.lock | 5 +++++ app/controllers/quotes_controller.rb | 14 ++++++++++++++ app/models/quote.rb | 9 +++++++++ app/views/quotes/search_form.html.haml | 6 ++++++ config/routes.rb | 2 ++ 6 files changed, 37 insertions(+) create mode 100644 app/views/quotes/search_form.html.haml diff --git a/Gemfile b/Gemfile index 74a0c62..5fa14f7 100644 --- a/Gemfile +++ b/Gemfile @@ -88,3 +88,4 @@ gem 'whenever', "~> 1.0.0", require: false gem "webmention" gem 'microformats', '~> 4.0', '>= 4.2.1' gem 'recaptcha' +gem 'ransack' diff --git a/Gemfile.lock b/Gemfile.lock index 0afbc47..6b672bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -296,6 +296,10 @@ GEM thor (~> 1.0, >= 1.2.2) zeitwerk (~> 2.6) rake (13.1.0) + ransack (4.3.0) + activerecord (>= 6.1.5) + activesupport (>= 6.1.5) + i18n rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) @@ -417,6 +421,7 @@ DEPENDENCIES paperclip rails (~> 7.1.0) rails_autolink + ransack recaptcha redcarpet rouge diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb index d4800cb..468f6ea 100644 --- a/app/controllers/quotes_controller.rb +++ b/app/controllers/quotes_controller.rb @@ -30,6 +30,20 @@ class QuotesController < ApplicationController end end + def search_form + @q = Quote.published.ransack + end + + def search + @quotes = Quote.published.ransack(params[:q]).result(distinct: true).paginate(page: params[:page], per_page: 10) + + respond_to do |format| + format.html { render :list } + format.json { render :json => @quotes } + format.xml { render :xml => @quotes } + end + end + def random picked = Quote.where(state: :published).ids.sample redirect_to quote_url(picked) diff --git a/app/models/quote.rb b/app/models/quote.rb index 3301667..b1d25b4 100644 --- a/app/models/quote.rb +++ b/app/models/quote.rb @@ -36,4 +36,13 @@ class Quote < ApplicationRecord def has_tags? !tags.empty? end + + def self.ransackable_attributes(auth_object = nil) + ["content", "notes"] + end + + def self.ransackable_associations(auth_object = nil) + [] + end + end diff --git a/app/views/quotes/search_form.html.haml b/app/views/quotes/search_form.html.haml new file mode 100644 index 0000000..74cdd20 --- /dev/null +++ b/app/views/quotes/search_form.html.haml @@ -0,0 +1,6 @@ +%h3 Search += search_form_for @q, url: search_quotes_url do |f| + .form-field + = f.label :content_or_notes_cont, "Query:" + = f.search_field :content_or_notes_cont + .form-field= f.submit diff --git a/config/routes.rb b/config/routes.rb index 76378c4..9e62ebb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -74,6 +74,8 @@ Rails.application.routes.draw do get 'tags' get 'tags/:id', :action => "tag", :as => "tag" get 'stats' + get 'search_form' + get 'search' end member do -- cgit 1.4.1