Models and controllers looks more like Rails

This commit is contained in:
riggraz
2019-09-09 16:50:33 +02:00
parent 46b8f06955
commit 9fcf9bcef9
4 changed files with 21 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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