diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-08-05 23:56:56 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-08-05 23:56:56 +0200 |
| commit | af2c6fd85e13139be3e3b2329a51c3a35acb3c40 (patch) | |
| tree | 02114279d6517ffc12b9920cc1d843937bff5a10 /app | |
| parent | a0a495d804319c3f7ad179baba7b87b41f1dc1f3 (diff) | |
Search assets and events, and order events by when they happen
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/assets_controller.rb | 8 | ||||
| -rw-r--r-- | app/controllers/events_controller.rb | 7 | ||||
| -rw-r--r-- | app/models/asset.rb | 14 | ||||
| -rw-r--r-- | app/models/event.rb | 18 | ||||
| -rw-r--r-- | app/views/assets/index.html.erb | 2 | ||||
| -rw-r--r-- | app/views/events/index.html.erb | 2 | ||||
| -rw-r--r-- | app/views/nodes/_node_list.html.erb | 17 | ||||
| -rw-r--r-- | app/views/shared/_search_form.html.erb | 15 |
8 files changed, 62 insertions, 21 deletions
diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 801e0968..f8a7d1e4 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb | |||
| @@ -6,12 +6,10 @@ class AssetsController < ApplicationController | |||
| 6 | before_action :login_required | 6 | before_action :login_required |
| 7 | 7 | ||
| 8 | layout 'admin' | 8 | layout 'admin' |
| 9 | 9 | ||
| 10 | def index | 10 | def index |
| 11 | @assets = Asset.order('id DESC').paginate( | 11 | scope = params[:q].present? ? Asset.editor_search(params[:q]) : Asset.all |
| 12 | :page => params[:page], | 12 | @assets = scope.order("id DESC").paginate(:page => params[:page], :per_page => 20) |
| 13 | :per_page => 20 | ||
| 14 | ) | ||
| 15 | end | 13 | end |
| 16 | 14 | ||
| 17 | # GET /assets/1 | 15 | # GET /assets/1 |
diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index d581de16..7ace5c1d 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb | |||
| @@ -10,11 +10,12 @@ class EventsController < ApplicationController | |||
| 10 | # GET /events | 10 | # GET /events |
| 11 | # GET /events.xml | 11 | # GET /events.xml |
| 12 | def index | 12 | def index |
| 13 | @events = Event.order(:id) | 13 | scope = Event.order(Arel.sql("start_time DESC NULLS LAST")) |
| 14 | scope = scope.merge(Event.editor_search(params[:q])) if params[:q].present? | ||
| 14 | 15 | ||
| 15 | respond_to do |format| | 16 | respond_to do |format| |
| 16 | format.html { @events = @events.paginate(page: params[:page], per_page: 25) } | 17 | format.html { @events = scope.paginate(:page => params[:page], :per_page => 25) } |
| 17 | format.xml { render :xml => @events } | 18 | format.xml { render :xml => scope } |
| 18 | end | 19 | end |
| 19 | end | 20 | end |
| 20 | 21 | ||
diff --git a/app/models/asset.rb b/app/models/asset.rb index b256b929..4d43b18f 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb | |||
| @@ -82,4 +82,18 @@ class Asset < ApplicationRecord | |||
| 82 | destroy! | 82 | destroy! |
| 83 | end | 83 | end |
| 84 | end | 84 | end |
| 85 | |||
| 86 | def self.editor_search(term) | ||
| 87 | words = term.to_s.split(/\s+/).reject(&:blank?) | ||
| 88 | return none if words.empty? | ||
| 89 | |||
| 90 | words.inject(all) do |scope, word| | ||
| 91 | like = "%#{sanitize_sql_like(word)}%" | ||
| 92 | scope.where( | ||
| 93 | "assets.name ILIKE :t OR assets.creator ILIKE :t OR " \ | ||
| 94 | "assets.upload_file_name ILIKE :t OR assets.source_url ILIKE :t OR " \ | ||
| 95 | "assets.upload_content_type ILIKE :t", :t => like | ||
| 96 | ) | ||
| 97 | end | ||
| 98 | end | ||
| 85 | end | 99 | end |
diff --git a/app/models/event.rb b/app/models/event.rb index 792ab448..e7823617 100644 --- a/app/models/event.rb +++ b/app/models/event.rb | |||
| @@ -54,6 +54,24 @@ class Event < ApplicationRecord | |||
| 54 | destroyed | 54 | destroyed |
| 55 | end | 55 | end |
| 56 | 56 | ||
| 57 | def self.editor_search(term) | ||
| 58 | words = term.to_s.split(/\s+/).reject(&:blank?) | ||
| 59 | return none if words.empty? | ||
| 60 | |||
| 61 | words.inject(all) do |scope, word| | ||
| 62 | like = "%#{sanitize_sql_like(word)}%" | ||
| 63 | tagged = ActsAsTaggableOn::Tagging | ||
| 64 | .where(:taggable_type => base_class.name) | ||
| 65 | .joins(:tag).where("tags.name ILIKE ?", like) | ||
| 66 | .select(:taggable_id) | ||
| 67 | scope.where( | ||
| 68 | "events.title ILIKE :t OR events.description ILIKE :t OR " \ | ||
| 69 | "events.location ILIKE :t OR events.url ILIKE :t OR " \ | ||
| 70 | "events.id IN (:tagged)", :t => like, :tagged => tagged | ||
| 71 | ) | ||
| 72 | end | ||
| 73 | end | ||
| 74 | |||
| 57 | private | 75 | private |
| 58 | def generate_occurrences | 76 | def generate_occurrences |
| 59 | Occurrence.generate self | 77 | Occurrence.generate self |
diff --git a/app/views/assets/index.html.erb b/app/views/assets/index.html.erb index af11f54f..d2c5d6cc 100644 --- a/app/views/assets/index.html.erb +++ b/app/views/assets/index.html.erb | |||
| @@ -4,6 +4,8 @@ | |||
| 4 | :label => t(".create_asset"), | 4 | :label => t(".create_asset"), |
| 5 | :options => [[t(".create_asset"), new_asset_path]] %> | 5 | :options => [[t(".create_asset"), new_asset_path]] %> |
| 6 | 6 | ||
| 7 | <%= render "shared/search_form", :placeholder => t(".search_placeholder") %> | ||
| 8 | |||
| 7 | <%= will_paginate @assets %> | 9 | <%= will_paginate @assets %> |
| 8 | 10 | ||
| 9 | <table class="assets_table"> | 11 | <table class="assets_table"> |
diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index c4f0a7f9..14790526 100644 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb | |||
| @@ -4,6 +4,8 @@ | |||
| 4 | :label => t(".create_event"), | 4 | :label => t(".create_event"), |
| 5 | :options => [[t(".create_event"), new_event_path]] %> | 5 | :options => [[t(".create_event"), new_event_path]] %> |
| 6 | 6 | ||
| 7 | <%= render "shared/search_form", :placeholder => t(".search_placeholder") %> | ||
| 8 | |||
| 7 | <%= will_paginate @events %> | 9 | <%= will_paginate @events %> |
| 8 | 10 | ||
| 9 | <table class="events_table"> | 11 | <table class="events_table"> |
diff --git a/app/views/nodes/_node_list.html.erb b/app/views/nodes/_node_list.html.erb index f3101058..55f396b7 100644 --- a/app/views/nodes/_node_list.html.erb +++ b/app/views/nodes/_node_list.html.erb | |||
| @@ -1,16 +1,7 @@ | |||
| 1 | <%= form_tag url_for(controller: params[:controller], action: params[:action]), method: :get, class: "node_search_form" do %> | 1 | <%= render "shared/search_form", |
| 2 | <% Array(params[:kinds]).each do |kind| %> | 2 | :placeholder => t(".search_placeholder"), |
| 3 | <%= hidden_field_tag "kinds[]", kind %> | 3 | :hidden => Array(params[:kinds]).map { |k| ["kinds[]", k] } + |
| 4 | <% end %> | 4 | (params[:tags].present? ? [["tags", params[:tags]]] : []) %> |
| 5 | <%= hidden_field_tag :tags, params[:tags] if params[:tags].present? %> | ||
| 6 | <%= text_field_tag :q, params[:q], placeholder: t(".search_placeholder") %> | ||
| 7 | <%= button_tag type: "submit", class: "action_button" do %> | ||
| 8 | <%= icon("search", library: "tabler", "aria-hidden": true) %> <%= t("admin.common.search") %> | ||
| 9 | <% end %> | ||
| 10 | <% if params[:q].present? || params[:kinds].present? || params[:tags].present? %> | ||
| 11 | <%= link_to t("admin.common.reset"), url_for(controller: params[:controller], action: params[:action]) %> | ||
| 12 | <% end %> | ||
| 13 | <% end %> | ||
| 14 | 5 | ||
| 15 | <%= will_paginate @nodes %> | 6 | <%= will_paginate @nodes %> |
| 16 | <table class="node_table"> | 7 | <table class="node_table"> |
diff --git a/app/views/shared/_search_form.html.erb b/app/views/shared/_search_form.html.erb new file mode 100644 index 00000000..bff6dfaa --- /dev/null +++ b/app/views/shared/_search_form.html.erb | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | <%= form_tag url_for(:controller => params[:controller], :action => params[:action]), | ||
| 2 | :method => :get, :class => "search_form" do %> | ||
| 3 | <% Array(local_assigns[:hidden]).each do |field_name, value| %> | ||
| 4 | <%= hidden_field_tag field_name, value %> | ||
| 5 | <% end %> | ||
| 6 | <%= text_field_tag :q, params[:q], :placeholder => placeholder %> | ||
| 7 | <%= button_tag :type => "submit", :class => "action_button" do %> | ||
| 8 | <%= icon("search", library: "tabler", "aria-hidden": true) %> | ||
| 9 | <%= t("admin.common.search") %> | ||
| 10 | <% end %> | ||
| 11 | <% if params[:q].present? || Array(local_assigns[:hidden]).any? %> | ||
| 12 | <%= link_to t("admin.common.reset"), | ||
| 13 | url_for(:controller => params[:controller], :action => params[:action]) %> | ||
| 14 | <% end %> | ||
| 15 | <% end %> | ||
