scinote-web/app/models/project.rb

170 lines
4.9 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class Project < ActiveRecord::Base
include ArchivableModel, SearchableModel
enum visibility: { hidden: 0, visible: 1 }
auto_strip_attributes :name, nullify: false
2016-02-12 23:52:43 +08:00
validates :name,
2016-09-16 21:41:31 +08:00
length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH },
uniqueness: { scope: :organization, case_sensitive: false }
2016-02-12 23:52:43 +08:00
validates :visibility, presence: true
validates :organization, presence: true
belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User'
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User'
belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User'
belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User'
has_many :user_projects, inverse_of: :project
has_many :users, through: :user_projects
has_many :experiments, inverse_of: :project
2016-02-12 23:52:43 +08:00
has_many :project_comments, inverse_of: :project
has_many :comments, through: :project_comments
has_many :activities, inverse_of: :project
has_many :tags, inverse_of: :project
has_many :reports, inverse_of: :project, dependent: :destroy
has_many :report_elements, inverse_of: :project, dependent: :destroy
belongs_to :organization, inverse_of: :projects
def self.search(user, include_archived, query = nil, page = 1)
2016-07-21 19:11:15 +08:00
if query
a_query = query.strip
.gsub("_","\\_")
.gsub("%","\\%")
.split(/\s+/)
.map {|t| "%" + t + "%" }
else
a_query = query
end
2016-02-12 23:52:43 +08:00
org_ids =
Organization
.joins(:user_organizations)
.where("user_organizations.user_id = ?", user.id)
.select("id")
.distinct
if include_archived
new_query = Project
.distinct
.joins(:user_projects)
.where("projects.organization_id IN (?)", org_ids)
.where("projects.visibility = 1 OR user_projects.user_id = ?", user.id)
2016-07-21 19:11:15 +08:00
.where_attributes_like(:name, a_query)
2016-02-12 23:52:43 +08:00
else
new_query = Project
.distinct
.joins(:user_projects)
.where("projects.organization_id IN (?)", org_ids)
.where("projects.visibility = 1 OR user_projects.user_id = ?", user.id)
2016-07-21 19:11:15 +08:00
.where_attributes_like(:name, a_query)
2016-02-12 23:52:43 +08:00
.where("projects.archived = ?", false)
end
# Show all results if needed
if page == SHOW_ALL_RESULTS
new_query
else
new_query
.limit(SEARCH_LIMIT)
.offset((page - 1) * SEARCH_LIMIT)
end
end
def last_activities(count = 20)
2016-07-21 19:11:15 +08:00
activities.order(created_at: :desc).first(count)
2016-02-12 23:52:43 +08:00
end
# Get project comments order by created_at time. Results are paginated
# using last comment id and per_page parameters.
def last_comments(last_id = 1, per_page = 20)
last_id = 9999999999999 if last_id <= 1
2016-09-27 15:00:02 +08:00
comments = Comment.joins(:project_comment)
.where(project_comments: { project_id: id })
.where('comments.id < ?', last_id)
.order(created_at: :desc)
.limit(per_page)
comments.reverse
2016-02-12 23:52:43 +08:00
end
def unassigned_users
2016-07-21 19:11:15 +08:00
User
2016-08-18 19:27:46 +08:00
.joins('INNER JOIN user_organizations ON users.id = user_organizations.user_id')
2016-08-18 19:24:44 +08:00
.where('user_organizations.organization_id = ?', organization)
.where.not(confirmed_at: nil)
.where('users.id NOT IN (?)',
UserProject.where(project: self).select(:user_id).distinct)
2016-02-12 23:52:43 +08:00
end
def user_role(user)
unless self.users.include? user
return nil
end
return (self.user_projects.select { |up| up.user == user }).first.role
end
2016-08-19 21:36:12 +08:00
def active_experiments(ordered = 'created_at DESC')
experiments.is_archived(false).order(ordered)
2016-07-29 21:47:41 +08:00
end
2016-08-08 20:19:35 +08:00
def archived_experiments
experiments.is_archived(true)
end
def project_my_modules
2016-07-26 14:44:09 +08:00
MyModule.where('"experiment_id" IN (?)', experiments.select(:id))
2016-02-12 23:52:43 +08:00
end
def space_taken
st = 0
2016-07-22 20:31:09 +08:00
project_my_modules.find_each do |my_module|
2016-02-12 23:52:43 +08:00
st += my_module.space_taken
end
st
end
# Writes to user log.
def log(message)
final = "[%s] %s" % [name, message]
organization.log(final)
end
def assigned_samples
Sample.joins(:my_modules).where(my_modules: {
id: my_modules_ids.split(',')
})
end
def my_modules_ids
ids = active_experiments.map do |exp|
exp.my_modules.pluck(:id) if exp.my_modules
end
ids.delete_if { |i| i.flatten.empty? }
ids.join(', ')
end
2016-08-17 19:17:40 +08:00
def assigned_modules(user)
role = user_role(user)
if role.blank?
MyModule.none
elsif role == 'owner'
2016-08-18 16:55:29 +08:00
project_my_modules
.joins(:experiment)
.where('experiments.archived=false')
2016-08-18 17:53:57 +08:00
.where('my_modules.archived=false')
2016-08-17 19:17:40 +08:00
else
project_my_modules
.joins(:user_my_modules)
2016-08-18 16:55:29 +08:00
.joins(:experiment)
.where('experiments.archived=false AND user_my_modules.user_id IN (?)',
user.id)
2016-08-18 17:53:57 +08:00
.where('my_modules.archived=false')
2016-08-17 19:17:40 +08:00
.distinct
end
end
2016-02-12 23:52:43 +08:00
end