Skip to content

Fix topics filtering by provider #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def current_provider
@current_provider ||= begin
provider_scope.find(cookies.signed[:current_provider_id])
rescue ActiveRecord::RecordNotFound
provider_scope.first
provider_scope.first || NullProvider.new
end
end
helper_method :current_provider
Expand Down
10 changes: 2 additions & 8 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ def set_topic
end

def scope
@scope ||= if Current.user.is_admin?
Topic.includes(:provider).all
elsif current_provider.present?
current_provider.topics
else
Current.user.topics
end.includes(:language)
@scope ||= current_provider.topics.includes(:language)
end

def topic_tags_params
Expand All @@ -105,7 +99,7 @@ def topic_tags_params
def search_params
return {} unless params[:search].present?

params.require(:search).permit(:query, :state, :provider_id, :language_id, :year, :month, :order, tag_list: [])
params.require(:search).permit(:query, :state, :language_id, :year, :month, :order, tag_list: [])
end
helper_method :search_params

Expand Down
5 changes: 0 additions & 5 deletions app/models/concerns/searcheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ def by_month(month)
where("extract(month from created_at) = ?", month)
end

def by_provider(provider_id)
where(provider_id: provider_id)
end

def by_language(language_id)
where(language_id: language_id)
end
Expand All @@ -43,7 +39,6 @@ def sort_order(order_from_params)
scope :search_with_params, ->(params) do
self
.then { |scope| params[:state].present? ? scope.by_state(params[:state]) : scope }
.then { |scope| params[:provider_id].present? ? scope.by_provider(params[:provider_id]) : scope }
.then { |scope| params[:language_id].present? ? scope.by_language(params[:language_id]): scope }
.then { |scope| params[:year].present? ? scope.by_year(params[:year]) : scope }
.then { |scope| params[:month].present? ? scope.by_month(params[:month]) : scope }
Expand Down
5 changes: 5 additions & 0 deletions app/models/null_provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NullProvider
def topics = Topic.none

def present? = false
end
4 changes: 0 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,4 @@ class User < ApplicationRecord
.then { |scope| params[:is_admin].present? ? scope.where(is_admin: params[:is_admin]) : scope }
.then { |scope| scope.order(created_at: params[:order]&.to_sym || :desc) }
end

def topics
Topic.where(provider_id: providers.pluck(:id))
end
end
Loading