diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 838a5a45..e09a8184 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,7 +6,7 @@ class PostsController < ApplicationController .left_outer_joins(:post_status) .select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color') .where(filter_params) - .search(params[:search]) + .search_by_name_or_description(params[:search]) .page(params[:page]) render json: posts @@ -30,9 +30,8 @@ class PostsController < ApplicationController defaults = { board_id: Board.first.id } params - .permit(:board_id, :post_status_id, :page, :search) + .permit(:board_id, :post_status_id) .with_defaults(defaults) - .except(:page, :search) # permit, but do not return page and search params end def post_params diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index f01654a0..4f02ca0c 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -1,12 +1,11 @@ class StaticPagesController < ApplicationController def roadmap @post_statuses = PostStatus + .find_roadmap .select(:id, :name, :color) - .where(show_in_roadmap: true) - .order(order: :asc) @posts = Post + .find_with_post_status_in(@post_statuses) .select(:id, :title, :board_id, :post_status_id, :user_id, :created_at) - .where(post_status_id: @post_statuses.pluck(:id)) end end \ No newline at end of file diff --git a/app/models/post.rb b/app/models/post.rb index d41e022f..d1568c34 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -7,9 +7,15 @@ class Post < ApplicationRecord paginates_per 15 - def self.search(s = '') - s = s || '' - s = sanitize_sql_like(s) - where("posts.title ILIKE ? OR posts.description ILIKE ?", "%#{s}%", "%#{s}%") + class << self + def find_with_post_status_in(post_statuses) + where(post_status_id: post_statuses.pluck(:id)) + end + + def search_by_name_or_description(s) + s = s || '' + s = sanitize_sql_like(s) + where("posts.title ILIKE ? OR posts.description ILIKE ?", "%#{s}%", "%#{s}%") + end end end diff --git a/app/models/post_status.rb b/app/models/post_status.rb index d73df254..658330e7 100644 --- a/app/models/post_status.rb +++ b/app/models/post_status.rb @@ -2,4 +2,11 @@ class PostStatus < ApplicationRecord validates :name, presence: true, uniqueness: true validates :color, format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/ } validates :order, numericality: { only_integer: true, greater_than: 0 } + + class << self + def find_roadmap + where(show_in_roadmap: true) + .order(order: :asc) + end + end end