scinote-web/app/models/checklist.rb

55 lines
1.7 KiB
Ruby
Raw Normal View History

2017-06-23 21:19:08 +08:00
class Checklist < ApplicationRecord
2016-07-21 19:11:15 +08:00
include SearchableModel
auto_strip_attributes :name, nullify: false
validates :name,
presence: true,
length: { maximum: Constants::TEXT_MAX_LENGTH }
2016-02-12 23:52:43 +08:00
validates :step, presence: true
belongs_to :step, inverse_of: :checklists, touch: true
2017-06-28 21:21:32 +08:00
belongs_to :created_by,
foreign_key: 'created_by_id',
class_name: 'User',
optional: true
belongs_to :last_modified_by,
foreign_key: 'last_modified_by_id',
class_name: 'User',
optional: true
2016-02-12 23:52:43 +08:00
has_many :checklist_items,
-> { order(:position) },
2016-02-12 23:52:43 +08:00
inverse_of: :checklist,
dependent: :destroy
has_many :report_elements,
inverse_of: :checklist,
dependent: :destroy
accepts_nested_attributes_for :checklist_items,
reject_if: :all_blank,
allow_destroy: true
scope :asc, -> { order('checklists.created_at ASC') }
2017-05-05 22:41:23 +08:00
def self.search(user,
include_archived,
query = nil,
page = 1,
_current_team = nil,
2017-05-08 16:22:54 +08:00
options = {})
step_ids = Step.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
.pluck(:id)
2016-07-21 19:11:15 +08:00
new_query = Checklist.distinct
.where(checklists: { step_id: step_ids })
.joins('LEFT JOIN checklist_items ON checklists.id = checklist_items.checklist_id')
.where_attributes_like(['checklists.name', 'checklist_items.text'], query, options)
2016-07-21 19:11:15 +08:00
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
2016-07-21 19:11:15 +08:00
new_query
else
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
2016-07-21 19:11:15 +08:00
end
end
2016-02-12 23:52:43 +08:00
end