mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 19:27:52 +01:00
Models and controllers looks more like Rails
This commit is contained in:
@@ -6,7 +6,7 @@ class PostsController < ApplicationController
|
|||||||
.left_outer_joins(:post_status)
|
.left_outer_joins(:post_status)
|
||||||
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
||||||
.where(filter_params)
|
.where(filter_params)
|
||||||
.search(params[:search])
|
.search_by_name_or_description(params[:search])
|
||||||
.page(params[:page])
|
.page(params[:page])
|
||||||
|
|
||||||
render json: posts
|
render json: posts
|
||||||
@@ -30,9 +30,8 @@ class PostsController < ApplicationController
|
|||||||
defaults = { board_id: Board.first.id }
|
defaults = { board_id: Board.first.id }
|
||||||
|
|
||||||
params
|
params
|
||||||
.permit(:board_id, :post_status_id, :page, :search)
|
.permit(:board_id, :post_status_id)
|
||||||
.with_defaults(defaults)
|
.with_defaults(defaults)
|
||||||
.except(:page, :search) # permit, but do not return page and search params
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_params
|
def post_params
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
class StaticPagesController < ApplicationController
|
class StaticPagesController < ApplicationController
|
||||||
def roadmap
|
def roadmap
|
||||||
@post_statuses = PostStatus
|
@post_statuses = PostStatus
|
||||||
|
.find_roadmap
|
||||||
.select(:id, :name, :color)
|
.select(:id, :name, :color)
|
||||||
.where(show_in_roadmap: true)
|
|
||||||
.order(order: :asc)
|
|
||||||
|
|
||||||
@posts = Post
|
@posts = Post
|
||||||
|
.find_with_post_status_in(@post_statuses)
|
||||||
.select(:id, :title, :board_id, :post_status_id, :user_id, :created_at)
|
.select(:id, :title, :board_id, :post_status_id, :user_id, :created_at)
|
||||||
.where(post_status_id: @post_statuses.pluck(:id))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -7,9 +7,15 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
paginates_per 15
|
paginates_per 15
|
||||||
|
|
||||||
def self.search(s = '')
|
class << self
|
||||||
s = s || ''
|
def find_with_post_status_in(post_statuses)
|
||||||
s = sanitize_sql_like(s)
|
where(post_status_id: post_statuses.pluck(:id))
|
||||||
where("posts.title ILIKE ? OR posts.description ILIKE ?", "%#{s}%", "%#{s}%")
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,4 +2,11 @@ class PostStatus < ApplicationRecord
|
|||||||
validates :name, presence: true, uniqueness: true
|
validates :name, presence: true, uniqueness: true
|
||||||
validates :color, format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/ }
|
validates :color, format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/ }
|
||||||
validates :order, numericality: { only_integer: true, greater_than: 0 }
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user