scinote-web/app/models/sample.rb

94 lines
3.5 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class Sample < ActiveRecord::Base
include SearchableModel
auto_strip_attributes :name, nullify: false
validates :name,
presence: true,
length: { maximum: Constants::NAME_MAX_LENGTH }
2017-01-24 23:34:21 +08:00
validates :user, :team, presence: true
2016-02-12 23:52:43 +08:00
belongs_to :user, inverse_of: :samples
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User'
2017-01-24 23:34:21 +08:00
belongs_to :team, inverse_of: :samples
2016-02-12 23:52:43 +08:00
belongs_to :sample_group, inverse_of: :samples
belongs_to :sample_type, inverse_of: :samples
has_many :sample_my_modules, inverse_of: :sample, dependent: :destroy
has_many :my_modules, through: :sample_my_modules
has_many :sample_custom_fields, inverse_of: :sample, dependent: :destroy
has_many :custom_fields, through: :sample_custom_fields
def self.search(
user,
2017-05-05 22:41:23 +08:00
_include_archived,
2016-02-12 23:52:43 +08:00
query = nil,
2017-01-09 18:33:28 +08:00
page = 1,
2017-05-05 22:41:23 +08:00
current_team = nil,
options = {}
2016-02-12 23:52:43 +08:00
)
2017-01-31 20:33:55 +08:00
team_ids = Team.joins(:user_teams)
.where('user_teams.user_id = ?', user.id)
.distinct
2017-04-11 20:55:44 +08:00
.pluck(:id)
2016-02-12 23:52:43 +08:00
2017-01-24 23:34:21 +08:00
if current_team
2017-01-09 18:33:28 +08:00
new_query = Sample
.distinct
2017-01-24 23:34:21 +08:00
.where('samples.team_id = ?', current_team.id)
2017-05-05 22:41:23 +08:00
.where_attributes_like(['samples.name'], query, options)
2016-02-12 23:52:43 +08:00
2017-01-12 16:52:29 +08:00
return new_query
2017-01-09 18:33:28 +08:00
else
2017-04-11 20:55:44 +08:00
user_ids = User
.joins(:user_teams)
.where('user_teams.team_id IN (?)', team_ids)
2017-05-05 22:41:23 +08:00
.where_attributes_like(['users.full_name'], query, options)
2017-04-11 20:55:44 +08:00
.pluck(:id)
sample_ids = Sample
.joins(:user)
.where('team_id IN (?)', team_ids)
2017-05-05 22:41:23 +08:00
.where_attributes_like(['name'], query, options)
2017-04-11 20:55:44 +08:00
.pluck(:id)
sample_type_ids = SampleType
.where('team_id IN (?)', team_ids)
2017-05-05 22:41:23 +08:00
.where_attributes_like(['name'], query, options)
2017-04-11 20:55:44 +08:00
.pluck(:id)
sample_group_ids = SampleGroup
.where('team_id IN (?)', team_ids)
2017-05-05 22:41:23 +08:00
.where_attributes_like(['name'], query, options)
2017-04-11 20:55:44 +08:00
.pluck(:id)
sample_custom_fields = SampleCustomField
.joins(:sample)
.where('samples.team_id IN (?)', team_ids)
2017-05-05 22:41:23 +08:00
.where_attributes_like(['value'], query, options)
2017-04-11 20:55:44 +08:00
.pluck(:id)
2017-01-09 18:33:28 +08:00
new_query = Sample
.distinct
.joins(:user)
.joins('LEFT OUTER JOIN sample_types ON ' \
'samples.sample_type_id = sample_types.id')
.joins('LEFT OUTER JOIN sample_groups ON ' \
'samples.sample_group_id = sample_groups.id')
.joins('LEFT OUTER JOIN sample_custom_fields ON ' \
'samples.id = sample_custom_fields.sample_id')
2017-04-11 20:55:44 +08:00
.where('samples.user_id IN (?) OR samples.id IN (?) ' \
'OR sample_types.id IN (?) ' \
'OR sample_groups.id IN (?)' \
'OR sample_custom_fields.id IN (?)',
user_ids, sample_ids, sample_type_ids,
sample_group_ids, sample_custom_fields)
2017-01-09 18:33:28 +08:00
end
2016-10-07 00:36:55 +08:00
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
new_query
else
new_query
.limit(Constants::SEARCH_LIMIT)
.offset((page - 1) * Constants::SEARCH_LIMIT)
end
2016-02-12 23:52:43 +08:00
end
end