1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

filter & search projets from postgres

This commit is contained in:
Sylvain 2020-06-23 11:19:20 +02:00
parent 1a6cd356bf
commit 7140571d63
2 changed files with 23 additions and 5 deletions

View File

@ -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

View File

@ -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',