Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/images/icons/globe-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions app/controllers/community_filter_keywords_controller.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
8 changes: 8 additions & 0 deletions app/models/community_filter_keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 39 additions & 0 deletions app/views/community_filter_keywords/index.html.haml
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down