diff options
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 5 | ||||
-rw-r--r-- | app/controllers/quotes_controller.rb | 14 | ||||
-rw-r--r-- | app/models/quote.rb | 9 | ||||
-rw-r--r-- | app/views/quotes/search_form.html.haml | 6 | ||||
-rw-r--r-- | config/routes.rb | 2 |
6 files changed, 37 insertions, 0 deletions
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 | |||
88 | gem "webmention" | 88 | gem "webmention" |
89 | gem 'microformats', '~> 4.0', '>= 4.2.1' | 89 | gem 'microformats', '~> 4.0', '>= 4.2.1' |
90 | gem 'recaptcha' | 90 | gem 'recaptcha' |
91 | 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 | |||
296 | thor (~> 1.0, >= 1.2.2) | 296 | thor (~> 1.0, >= 1.2.2) |
297 | zeitwerk (~> 2.6) | 297 | zeitwerk (~> 2.6) |
298 | rake (13.1.0) | 298 | rake (13.1.0) |
299 | ransack (4.3.0) | ||
300 | activerecord (>= 6.1.5) | ||
301 | activesupport (>= 6.1.5) | ||
302 | i18n | ||
299 | rb-fsevent (0.11.2) | 303 | rb-fsevent (0.11.2) |
300 | rb-inotify (0.10.1) | 304 | rb-inotify (0.10.1) |
301 | ffi (~> 1.0) | 305 | ffi (~> 1.0) |
@@ -417,6 +421,7 @@ DEPENDENCIES | |||
417 | paperclip | 421 | paperclip |
418 | rails (~> 7.1.0) | 422 | rails (~> 7.1.0) |
419 | rails_autolink | 423 | rails_autolink |
424 | ransack | ||
420 | recaptcha | 425 | recaptcha |
421 | redcarpet | 426 | redcarpet |
422 | rouge | 427 | 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 | |||
30 | end | 30 | end |
31 | end | 31 | end |
32 | 32 | ||
33 | def search_form | ||
34 | @q = Quote.published.ransack | ||
35 | end | ||
36 | |||
37 | def search | ||
38 | @quotes = Quote.published.ransack(params[:q]).result(distinct: true).paginate(page: params[:page], per_page: 10) | ||
39 | |||
40 | respond_to do |format| | ||
41 | format.html { render :list } | ||
42 | format.json { render :json => @quotes } | ||
43 | format.xml { render :xml => @quotes } | ||
44 | end | ||
45 | end | ||
46 | |||
33 | def random | 47 | def random |
34 | picked = Quote.where(state: :published).ids.sample | 48 | picked = Quote.where(state: :published).ids.sample |
35 | redirect_to quote_url(picked) | 49 | 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 | |||
36 | def has_tags? | 36 | def has_tags? |
37 | !tags.empty? | 37 | !tags.empty? |
38 | end | 38 | end |
39 | |||
40 | def self.ransackable_attributes(auth_object = nil) | ||
41 | ["content", "notes"] | ||
42 | end | ||
43 | |||
44 | def self.ransackable_associations(auth_object = nil) | ||
45 | [] | ||
46 | end | ||
47 | |||
39 | end | 48 | 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 @@ | |||
1 | %h3 Search | ||
2 | = search_form_for @q, url: search_quotes_url do |f| | ||
3 | .form-field | ||
4 | = f.label :content_or_notes_cont, "Query:" | ||
5 | = f.search_field :content_or_notes_cont | ||
6 | .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 | |||
74 | get 'tags' | 74 | get 'tags' |
75 | get 'tags/:id', :action => "tag", :as => "tag" | 75 | get 'tags/:id', :action => "tag", :as => "tag" |
76 | get 'stats' | 76 | get 'stats' |
77 | get 'search_form' | ||
78 | get 'search' | ||
77 | end | 79 | end |
78 | 80 | ||
79 | member do | 81 | member do |