From 7140571d63fd7bf8ffcfa357695a8c5235edc19d Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 23 Jun 2020 11:19:20 +0200 Subject: [PATCH] filter & search projets from postgres --- app/controllers/api/projects_controller.rb | 18 ++++++++++++++---- app/models/project.rb | 10 +++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/projects_controller.rb b/app/controllers/api/projects_controller.rb index 83658f415..6368beca6 100644 --- a/app/controllers/api/projects_controller.rb +++ b/app/controllers/api/projects_controller.rb @@ -7,8 +7,7 @@ class API::ProjectsController < API::ApiController respond_to :json def index - # DEPEND per(12) -> projects.coffee - @projects = policy_scope(Project).page(params[:page]).per(12) + @projects = policy_scope(Project).page(params[:page]) end def last_published @@ -53,9 +52,20 @@ class API::ProjectsController < API::ApiController end def search - records = Project.published.drafts(current_user.statistic_profile.id).search(params[:q]) + query_params = JSON.parse(params[:search]) + + records = Project.published_or_drafts(current_user&.statistic_profile&.id) + records = Project.user_projects(current_user&.statistic_profile&.id) if query_params['from'] == 'mine' + records = Project.collaborations(current_user&.id) if query_params['from'] == 'collaboration' + + records = records.with_machine(query_params['machine_id']) if query_params['machine_id'].present? + records = records.with_component(query_params['component_id']) if query_params['component_id'].present? + records = records.with_theme(query_params['theme_id']) if query_params['theme_id'].present? + records = records.with_space(query_params['space_id']) if query_params['space_id'].present? + records = records.search(query_params['q']) if query_params['q'].present? + @total = records.count - @projects = records.includes(:users, :project_image).page(params[:page]).per(20) + @projects = records.includes(:users, :project_image).page(params[:page]) render :index end diff --git a/app/models/project.rb b/app/models/project.rb index 482f02dc5..bd83f1ecb 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -51,7 +51,15 @@ class Project < ApplicationRecord # scopes scope :published, -> { where("state = 'published'") } - scope :drafts, ->(author_profile) { where("state = 'draft' AND author_statistic_profile_id = ?", author_profile) } + scope :published_or_drafts, lambda { |author_profile| + where("state = 'published' OR (state = 'draft' AND author_statistic_profile_id = ?)", author_profile) + } + scope :user_projects, ->(author_profile) { where('author_statistic_profile_id = ?', author_profile) } + scope :collaborations, ->(collaborators_ids) { joins(:projects_users).where(projects_users: { user_id: collaborators_ids }) } + scope :with_machine, ->(machines_ids) { joins(:projects_machines).where(projects_machines: { machine_id: machines_ids }) } + scope :with_theme, ->(themes_ids) { joins(:projects_themes).where(projects_themes: { theme_id: themes_ids }) } + scope :with_component, ->(component_ids) { joins(:projects_components).where(projects_components: { component_id: component_ids }) } + scope :with_space, ->(spaces_ids) { joins(:projects_spaces).where(projects_spaces: { space_id: spaces_ids }) } pg_search_scope :search, against: { name: 'A',