-
Notifications
You must be signed in to change notification settings - Fork 6
add search for for topics #60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ class TopicsController < ApplicationController | |
| before_action :check_admin!, only: :destroy | ||
|
|
||
| def index | ||
| @topics = scope.includes(:language, :provider) | ||
| @topics = scope.search_with_params(search_params) | ||
| @providers = scope.map(&:provider).uniq.sort_by(&:name) | ||
| @languages = scope.map(&:language).uniq.sort_by(&:name) | ||
| end | ||
|
|
||
| def new | ||
|
|
@@ -47,6 +49,13 @@ def topic_params | |
| params.require(:topic).permit(:title, :description, :uid, :language_id, :provider_id) | ||
| end | ||
|
|
||
| helper_method :search_params | ||
| def search_params | ||
| return {} unless params[:search].present? | ||
|
|
||
| params.require(:search).permit(:query, :state, :provider_id, :language_id, :year, :month, :order) | ||
| end | ||
|
|
||
| def set_topic | ||
| @topic = Topic.find(params[:id]) | ||
| end | ||
|
|
@@ -55,7 +64,7 @@ def scope | |
| @scope ||= if Current.user.is_admin? | ||
| Topic.all | ||
| else | ||
| Topic.active | ||
| end | ||
| Current.user.topics | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we should filter by state, all topics are available for admins. |
||
| end.includes(:language, :provider) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| module Searcheable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| SORTS = %i[asc desc].freeze | ||
|
|
||
| class_methods do | ||
| def search(query) | ||
| where("title ILIKE :query OR description ILIKE :query", query: "%#{query}%") | ||
| end | ||
|
|
||
| def by_year(year) | ||
| where("extract(year from created_at) = ?", year) | ||
| end | ||
|
|
||
| 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 | ||
|
|
||
| def by_state(state) | ||
| where(state: state) | ||
| end | ||
|
|
||
| def sort_order(order_from_params) | ||
| return :desc unless SORTS.include?(order_from_params) | ||
|
|
||
| order_from_params | ||
| end | ||
|
|
||
| def search_with_params(params) | ||
| 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 } | ||
| .then { |scope| params[:query].present? ? scope.search(params[:query]) : scope } | ||
| .then { |scope| scope.order(created_at: sort_order(params[:order])) } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| class Topic < ApplicationRecord | ||
| include Searcheable | ||
|
|
||
| belongs_to :language | ||
| belongs_to :provider | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <div class="card"> | ||
| <div class="card-header d-flex justify-content-between align-items-center"> | ||
| <h2 class="card-title">Search</h2> | ||
| </div> | ||
| <div class="card-content"> | ||
| <div class="card-body"> | ||
| <p class="card-text"> Use these params settings to search topics</p> | ||
| <%= form_for :search, url: topics_path, method: :get do |f| %> | ||
| <div class="form-body"> | ||
| <div class="row"> | ||
| <div class="col-12"> | ||
| <div class="form-group"> | ||
| <%= f.label :provider %> | ||
| <%= f.select :provider_id, options_from_collection_for_select(providers, :id, :name, params[:provider_id]), { prompt: "Select provider" }, class: "form-select" %> | ||
| </div> | ||
| <div class="form-group"> | ||
| <%= f.label :language %> | ||
| <%= f.select :language_id, options_from_collection_for_select(languages, :id, :name, params[:provider_id]), { prompt: "Select language" }, class: "form-select" %> | ||
| </div> | ||
| <div class="form-group"> | ||
| <%= f.label :query %> | ||
| <%= f.text_field :query, value: params[:query], class: "form-control" %> | ||
| </div> | ||
| <div class="form-group"> | ||
| <%= f.label :year %> | ||
| <%= f.select :year, options_for_select((Date.today.year-10..Date.today.year).to_a, params[:year]), { prompt: "Select year" }, class: "form-select" %> | ||
| </div> | ||
| <div class="form-group"> | ||
| <%= f.label :month %> | ||
| <%= f.select :month, options_for_select((1..12).to_a, params[:month]), { prompt: "Select month" }, class: "form-select" %> | ||
| </div> | ||
| <div class="form-group"> | ||
| <%= f.label :state %> | ||
| <%= f.select :state, options_for_select(Topic::STATES.index_with(&:itself), params[:state]), { prompt: "Select state" }, class: "form-select" %> | ||
| </div> | ||
| <div class="form-group"> | ||
| <%= f.label :order %> | ||
| <%= f.select :order, options_for_select(Topic::SORTS.reverse.index_with(&:itself), params[:order]), {}, class: "form-select" %> | ||
| </div> | ||
| <div class="col-12 d-flex justify-content-end"> | ||
| <%= f.submit "Search", class: "btn btn-primary me-1 mb-1" %> | ||
| <%= link_to "Clear", topics_path, class: "btn btn-light-secondary me-1 mb-1" %> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <% end %> | ||
| </div> | ||
| </div> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,11 @@ | |
| puts "Topics created!" | ||
| puts "Creating users..." | ||
|
|
||
| User.create(email: "[email protected]", password: "test123") | ||
| User.create(email: "[email protected]", password: "test123", is_admin: true) | ||
| me = User.create(email: "[email protected]", password: "test123") | ||
| Provider.each do |provider| | ||
| provider.users << me | ||
| end | ||
|
|
||
| 10.times do | ||
| User.create( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| FactoryBot.define do | ||
| factory :contributor do | ||
| association :provider | ||
| association :user | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to use these params for view