diff --git a/app/assets/images/icons/globe-white.svg b/app/assets/images/icons/globe-white.svg new file mode 100644 index 00000000..e6d6a559 --- /dev/null +++ b/app/assets/images/icons/globe-white.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/controllers/community_filter_keywords_controller.rb b/app/controllers/community_filter_keywords_controller.rb index ab92d11b..35385b26 100644 --- a/app/controllers/community_filter_keywords_controller.rb +++ b/app/controllers/community_filter_keywords_controller.rb @@ -1,6 +1,12 @@ class CommunityFilterKeywordsController < ApplicationController before_action :set_community_filter_keyword, only: [:edit, :update, :destroy] + PER_PAGE = 10 + def index + @search = fetch_keywords + @keywords = @search.result.page(params[:page]).per(PER_PAGE) + end + def create @community_filter_keyword = CommunityFilterKeyword.new(community_filter_keyword_params) patchwork_community_id = params.dig(:community_filter_keyword, :patchwork_community_id) @@ -46,4 +52,24 @@ def community_filter_keyword_params def determine_redirect_path(patchwork_community_id, filter_type) filter_type == 'filter_in' ? step3_community_path(id: patchwork_community_id) : step4_community_path(id: patchwork_community_id) end + + def fetch_keywords + filter_type = params[:filter_type].to_s.strip + filter_type = 'filter_out' unless %w[filter_out filter_in].include?(filter_type) + + begin + community_filter_keywords = CommunityFilterKeyword + .select(:id, :keyword, :filter_type, :is_filter_hashtag, :patchwork_community_id) + .where( + patchwork_community_id: params[:community_id].presence, + filter_type: filter_type + ) + .order(:keyword).ransack(params[:q]) + community_filter_keywords + rescue ActiveRecord::ActiveRecordError => e + Rails.logger.error("Error fetching community filter keywords: #{e.message}") + community_filter_keywords = [] + flash[:error] = 'An error occurred while fetching filter keywords. Please try again later.' + end + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 82b00833..87b66119 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -24,6 +24,7 @@ def sidebar_menu_items { path: communities_path(channel_type: 'newsmast'), id: 'communities-link', header: 'Newsmast channels', icon: 'newsmast.svg', text: 'Newsmast channels', active_if: newsmast_active }, { path: collections_path, id: 'collections-link', header: 'Collections', icon: 'collection.svg', text: 'Collections', active_if: 'collections' }, { path: master_admins_path, id: 'master_admins-link', header: 'Master admin', icon: 'administrator.svg', text: 'Master admins', active_if: 'master_admins' }, + { path: community_filter_keywords_path(community_id: nil), id: 'global_keywords-link', header: 'Global keywords', icon: 'globe-white.svg', text: 'Global keywords', active_if: 'global_keywords' }, # { path: accounts_path, id: 'accounts-link', header: 'Users', icon: 'users.svg', text: 'Users', active_if: 'accounts' }, { path: resources_path, id: 'resources-link', header: 'Resources', icon: 'folder.svg', text: 'Resources', active_if: 'resources' }, { path: api_keys_path, id: 'resources-link', header: 'API Key', icon: 'key.svg', text: 'API Key', active_if: 'api_keys' }, diff --git a/app/models/community_filter_keyword.rb b/app/models/community_filter_keyword.rb index 10a60a77..32be9abb 100644 --- a/app/models/community_filter_keyword.rb +++ b/app/models/community_filter_keyword.rb @@ -28,4 +28,12 @@ class CommunityFilterKeyword < ApplicationRecord FILTER_TYPES = %w[filter_in filter_out].freeze validates :filter_type, presence: true, inclusion: { in: FILTER_TYPES } validates_uniqueness_of :keyword, scope: [:is_filter_hashtag, :patchwork_community_id], message: "already exists." + + def self.ransackable_attributes(auth_object = nil) + ["keyword"] + end + + def self.ransackable_associations(auth_object = nil) + [] + end end diff --git a/app/views/community_filter_keywords/index.html.haml b/app/views/community_filter_keywords/index.html.haml new file mode 100644 index 00000000..da11d3e2 --- /dev/null +++ b/app/views/community_filter_keywords/index.html.haml @@ -0,0 +1,39 @@ +- title = "Global keywords codes" + +- content_for :title do + = #{title} + +.row + .col-12 + .card + .card-header + %h3.card-title #{title} + .card-tools.d-flex.justify-content-end.align-items-center + = search_form_for @search, url: community_filter_keywords_path, method: :get, html: { class: 'form-inline' } do |f| + .input-group.input-group-sm + = f.search_field :keyword_cont, class: "form-control", placeholder: "Search" + .input-group-append + %button.btn.btn-default{type: "submit"} + %i.fas.fa-search + .card-body + %p.text-muted Here you can see a list of all global keywords. + %table.table.no-side-borders.top-bottom-border{data: {url: community_filter_keywords_url, type: 'community_filter_keyword'}} + %thead + %tr + %th{ style: 'width: 45%;' } Name + %th{ style: 'width: 45%;' } Hashtag? + %tbody + - if @keywords.size == 0 + %tr + %td.text-center.info-box-content{colspan: "100%"} + %p.text-muted.small You haven’t added any keywords + - @keywords.each do |keyword| + %tr + %td{ style: 'width: 45%;' }= keyword.keyword + %td{ style: 'width: 45%;' } + %i{ class: "fa-solid #{keyword.is_filter_hashtag ? 'fa-check' : 'fa-xmark'}", style: "color: #{keyword.is_filter_hashtag ? 'green' : 'red'}" } + - if master_admin? + .row.mt-2 + .col + .pagination.pagination-sm.m-0.float-right + = paginate @keywords, theme: 'bootstrap4' diff --git a/config/routes.rb b/config/routes.rb index 1a5fb7d8..91f854b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,7 +48,7 @@ resources :community_admins - resources :community_filter_keywords, only: [:create, :update, :destroy] + resources :community_filter_keywords, only: [:index, :create, :update, :destroy] resources :community do resources :hashtag