mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-03 19:24:48 +08:00
Merge pull request #511 from okriuchykhin/ok_SCI_1090
Refactoring of the comments using STI [SCI-1090]
This commit is contained in:
commit
2a33391f4b
26 changed files with 229 additions and 201 deletions
|
@ -43,12 +43,14 @@ class MyModuleCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
@comment = Comment.new(
|
||||
@comment = TaskComment.new(
|
||||
message: comment_params[:message],
|
||||
user: current_user)
|
||||
user: current_user,
|
||||
my_module: @my_module
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
if (@comment.valid? && @my_module.comments << @comment)
|
||||
if @comment.save
|
||||
# Generate activity
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_module,
|
||||
|
@ -176,12 +178,12 @@ class MyModuleCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def check_edit_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = TaskComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? && can_edit_module_comment(@comment)
|
||||
end
|
||||
|
||||
def check_destroy_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = TaskComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? && can_delete_module_comment(@comment)
|
||||
end
|
||||
|
||||
|
|
|
@ -42,13 +42,14 @@ class ProjectCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
@comment = Comment.new(
|
||||
@comment = ProjectComment.new(
|
||||
message: comment_params[:message],
|
||||
user: current_user)
|
||||
user: current_user,
|
||||
project: @project
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
|
||||
if (@comment.valid? && @project.comments << @comment)
|
||||
if @comment.save
|
||||
# Generate activity
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_project,
|
||||
|
@ -172,12 +173,12 @@ class ProjectCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def check_edit_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = ProjectComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? && can_edit_project_comment(@comment)
|
||||
end
|
||||
|
||||
def check_destroy_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = ProjectComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? && can_delete_project_comment(@comment)
|
||||
end
|
||||
|
||||
|
|
|
@ -40,12 +40,14 @@ class ResultCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
@comment = Comment.new(
|
||||
@comment = ResultComment.new(
|
||||
message: comment_params[:message],
|
||||
user: current_user)
|
||||
user: current_user,
|
||||
result: @result
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
if (@comment.valid? && @result.comments << @comment)
|
||||
if @comment.save
|
||||
|
||||
# Generate activity
|
||||
Activity.create(
|
||||
|
@ -54,7 +56,7 @@ class ResultCommentsController < ApplicationController
|
|||
project: @result.my_module.experiment.project,
|
||||
my_module: @result.my_module,
|
||||
message: t(
|
||||
"activities.add_comment_to_result",
|
||||
'activities.add_comment_to_result',
|
||||
user: current_user.full_name,
|
||||
result: @result.name
|
||||
)
|
||||
|
@ -63,7 +65,7 @@ class ResultCommentsController < ApplicationController
|
|||
format.json {
|
||||
render json: {
|
||||
html: render_to_string(
|
||||
partial: "comment.html.erb",
|
||||
partial: 'comment.html.erb',
|
||||
locals: {
|
||||
comment: @comment
|
||||
}
|
||||
|
@ -175,13 +177,13 @@ class ResultCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def check_edit_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = ResultComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? &&
|
||||
can_edit_result_comment_in_module(@comment)
|
||||
end
|
||||
|
||||
def check_destroy_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = ResultComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? &&
|
||||
can_delete_result_comment_in_module(@comment)
|
||||
end
|
||||
|
|
|
@ -39,13 +39,14 @@ class StepCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
@comment = Comment.new(
|
||||
@comment = StepComment.new(
|
||||
message: comment_params[:message],
|
||||
user: current_user)
|
||||
user: current_user,
|
||||
step: @step
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
if (@comment.valid? && @step.comments << @comment)
|
||||
|
||||
if @comment.save
|
||||
# Generate activity (this can only occur in module,
|
||||
# but nonetheless check if my module is not nil)
|
||||
if @protocol.in_module?
|
||||
|
@ -183,13 +184,13 @@ class StepCommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def check_edit_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = StepComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? &&
|
||||
can_edit_step_comment_in_protocol(@comment)
|
||||
end
|
||||
|
||||
def check_destroy_permissions
|
||||
@comment = Comment.find_by_id(params[:id])
|
||||
@comment = StepComment.find_by_id(params[:id])
|
||||
render_403 unless @comment.present? &&
|
||||
can_delete_step_comment_in_protocol(@comment)
|
||||
end
|
||||
|
|
|
@ -314,18 +314,18 @@ module PermissionHelper
|
|||
end
|
||||
|
||||
def can_edit_project_comment(comment)
|
||||
comment.project_comment.present? &&
|
||||
comment.project.present? &&
|
||||
(
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(comment.project_comment.project)
|
||||
is_owner_of_project(comment.project)
|
||||
)
|
||||
end
|
||||
|
||||
def can_delete_project_comment(comment)
|
||||
comment.project_comment.present? &&
|
||||
comment.project.present? &&
|
||||
(
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(comment.project_comment.project)
|
||||
is_owner_of_project(comment.project)
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -503,21 +503,21 @@ module PermissionHelper
|
|||
end
|
||||
|
||||
def can_edit_module_comment(comment)
|
||||
comment.my_module_comment.present? &&
|
||||
comment.my_module.present? &&
|
||||
(
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(
|
||||
comment.my_module_comment.my_module.experiment.project
|
||||
comment.my_module.experiment.project
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def can_delete_module_comment(comment)
|
||||
comment.my_module_comment.present? &&
|
||||
comment.my_module.present? &&
|
||||
(
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(
|
||||
comment.my_module_comment.my_module.experiment.project
|
||||
comment.my_module.experiment.project
|
||||
)
|
||||
)
|
||||
end
|
||||
|
@ -555,21 +555,21 @@ module PermissionHelper
|
|||
end
|
||||
|
||||
def can_edit_result_comment_in_module(comment)
|
||||
comment.result_comment.present? &&
|
||||
comment.result.present? &&
|
||||
(
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(
|
||||
comment.result_comment.result.my_module.experiment.project
|
||||
comment.result.my_module.experiment.project
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def can_delete_result_comment_in_module(comment)
|
||||
comment.result_comment.present? &&
|
||||
comment.result.present? &&
|
||||
(
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(
|
||||
comment.result_comment.result.my_module.experiment.project
|
||||
comment.result.my_module.experiment.project
|
||||
)
|
||||
)
|
||||
end
|
||||
|
@ -953,9 +953,9 @@ module PermissionHelper
|
|||
end
|
||||
|
||||
def can_edit_step_comment_in_protocol(comment)
|
||||
return false if comment.step_comment.blank?
|
||||
return false if comment.step.blank?
|
||||
|
||||
protocol = comment.step_comment.step.protocol
|
||||
protocol = comment.step.protocol
|
||||
if protocol.in_module?
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(
|
||||
|
@ -967,9 +967,9 @@ module PermissionHelper
|
|||
end
|
||||
|
||||
def can_delete_step_comment_in_protocol(comment)
|
||||
return false if comment.step_comment.blank?
|
||||
return false if comment.step.blank?
|
||||
|
||||
protocol = comment.step_comment.step.protocol
|
||||
protocol = comment.step.protocol
|
||||
if protocol.in_module?
|
||||
comment.user == current_user ||
|
||||
is_owner_of_project(
|
||||
|
|
|
@ -7,16 +7,9 @@ class Comment < ActiveRecord::Base
|
|||
length: { maximum: Constants::TEXT_MAX_LENGTH }
|
||||
validates :user, presence: true
|
||||
|
||||
validate :belongs_to_only_one_object
|
||||
|
||||
belongs_to :user, inverse_of: :comments
|
||||
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User'
|
||||
|
||||
has_one :step_comment, inverse_of: :comment, dependent: :destroy
|
||||
has_one :my_module_comment, inverse_of: :comment, dependent: :destroy
|
||||
has_one :result_comment, inverse_of: :comment, dependent: :destroy
|
||||
has_one :sample_comment, inverse_of: :comment, dependent: :destroy
|
||||
has_one :project_comment, inverse_of: :comment, dependent: :destroy
|
||||
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id',
|
||||
class_name: 'User'
|
||||
|
||||
def self.search(
|
||||
user,
|
||||
|
@ -27,52 +20,44 @@ class Comment < ActiveRecord::Base
|
|||
project_ids =
|
||||
Project
|
||||
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
||||
.select("id")
|
||||
.select(:id)
|
||||
my_module_ids =
|
||||
MyModule
|
||||
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
||||
.select("id")
|
||||
.select(:id)
|
||||
step_ids =
|
||||
Step
|
||||
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
||||
.select("id")
|
||||
.select(:id)
|
||||
result_ids =
|
||||
Result
|
||||
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
||||
.select("id")
|
||||
|
||||
.select(:id)
|
||||
|
||||
if query
|
||||
a_query = query.strip
|
||||
.gsub("_","\\_")
|
||||
.gsub("%","\\%")
|
||||
.split(/\s+/)
|
||||
.map {|t| "%" + t + "%" }
|
||||
.gsub('_', '\\_')
|
||||
.gsub('%', '\\%')
|
||||
.split(/\s+/)
|
||||
.map { |t| '%' + t + '%' }
|
||||
else
|
||||
a_query = query
|
||||
end
|
||||
|
||||
new_query = Comment
|
||||
.distinct
|
||||
.joins(:user)
|
||||
.joins("LEFT JOIN project_comments ON project_comments.comment_id = comments.id")
|
||||
.joins("LEFT JOIN my_module_comments ON my_module_comments.comment_id = comments.id")
|
||||
.joins("LEFT JOIN step_comments ON step_comments.comment_id = comments.id")
|
||||
.joins("LEFT JOIN result_comments ON result_comments.comment_id = comments.id")
|
||||
.where(
|
||||
"project_comments.project_id IN (?) OR " +
|
||||
"my_module_comments.my_module_id IN (?) OR " +
|
||||
"step_comments.step_id IN (?) OR " +
|
||||
"result_comments.result_id IN (?)",
|
||||
project_ids,
|
||||
my_module_ids,
|
||||
step_ids,
|
||||
result_ids
|
||||
)
|
||||
.where_attributes_like(
|
||||
[ :message, "users.full_name" ],
|
||||
a_query
|
||||
)
|
||||
new_query =
|
||||
Comment.distinct
|
||||
.joins(:user)
|
||||
.where(
|
||||
'(comments.associated_id IN (?) AND comments.type = ?) OR ' \
|
||||
'(comments.associated_id IN (?) AND comments.type = ?) OR ' \
|
||||
'(comments.associated_id IN (?) AND comments.type = ?) OR ' \
|
||||
'(comments.associated_id IN (?) AND comments.type = ?)',
|
||||
project_ids, 'ProjectComment',
|
||||
my_module_ids, 'TaskComment',
|
||||
step_ids, 'StepComment',
|
||||
result_ids, 'ResultComment'
|
||||
)
|
||||
.where_attributes_like([:message, 'users.full_name'], a_query)
|
||||
|
||||
# Show all results if needed
|
||||
if page == Constants::SEARCH_NO_LIMIT
|
||||
|
@ -83,21 +68,4 @@ class Comment < ActiveRecord::Base
|
|||
.offset((page - 1) * Constants::SEARCH_LIMIT)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def belongs_to_only_one_object
|
||||
# We must allow all elements to be blank because of GUI
|
||||
# (eventhough it's not really a "valid" comment)
|
||||
cntr = 0
|
||||
cntr += 1 if step_comment.present?
|
||||
cntr += 1 if my_module_comment.present?
|
||||
cntr += 1 if result_comment.present?
|
||||
cntr += 1 if sample_comment.present?
|
||||
cntr += 1 if project_comment.present?
|
||||
|
||||
if cntr > 1
|
||||
errors.add(:base, "Comment can only belong to 1 'parent' object.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,8 +23,7 @@ class MyModule < ActiveRecord::Base
|
|||
has_many :results, inverse_of: :my_module, :dependent => :destroy
|
||||
has_many :my_module_tags, inverse_of: :my_module, :dependent => :destroy
|
||||
has_many :tags, through: :my_module_tags
|
||||
has_many :my_module_comments, inverse_of: :my_module, :dependent => :destroy
|
||||
has_many :comments, through: :my_module_comments
|
||||
has_many :task_comments, foreign_key: :associated_id, dependent: :destroy
|
||||
has_many :inputs, :class_name => 'Connection', :foreign_key => "input_id", inverse_of: :to, :dependent => :destroy
|
||||
has_many :outputs, :class_name => 'Connection', :foreign_key => "output_id", inverse_of: :from, :dependent => :destroy
|
||||
has_many :my_modules, through: :outputs, source: :to
|
||||
|
@ -176,11 +175,11 @@ class MyModule < ActiveRecord::Base
|
|||
# using last comment id and per_page parameters.
|
||||
def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT)
|
||||
last_id = Constants::INFINITY if last_id <= 1
|
||||
comments = Comment.joins(:my_module_comment)
|
||||
.where(my_module_comments: { my_module_id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments = TaskComment.joins(:my_module)
|
||||
.where(my_modules: { id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments.reverse
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
class MyModuleComment < ActiveRecord::Base
|
||||
validates :comment, :my_module, presence: true
|
||||
validates :my_module_id, uniqueness: { scope: :comment_id }
|
||||
|
||||
belongs_to :comment, inverse_of: :my_module_comment
|
||||
belongs_to :my_module, inverse_of: :my_module_comments
|
||||
end
|
|
@ -19,8 +19,7 @@ class Project < ActiveRecord::Base
|
|||
has_many :user_projects, inverse_of: :project
|
||||
has_many :users, through: :user_projects
|
||||
has_many :experiments, inverse_of: :project
|
||||
has_many :project_comments, inverse_of: :project
|
||||
has_many :comments, through: :project_comments
|
||||
has_many :project_comments, foreign_key: :associated_id, dependent: :destroy
|
||||
has_many :activities, inverse_of: :project
|
||||
has_many :tags, inverse_of: :project
|
||||
has_many :reports, inverse_of: :project, dependent: :destroy
|
||||
|
@ -110,11 +109,11 @@ class Project < ActiveRecord::Base
|
|||
# using last comment id and per_page parameters.
|
||||
def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT)
|
||||
last_id = Constants::INFINITY if last_id <= 1
|
||||
comments = Comment.joins(:project_comment)
|
||||
.where(project_comments: { project_id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments = ProjectComment.joins(:project)
|
||||
.where(projects: { id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments.reverse
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
class ProjectComment < ActiveRecord::Base
|
||||
validates :comment, :project, presence: true
|
||||
validates :project_id, uniqueness: { scope: :comment_id }
|
||||
class ProjectComment < Comment
|
||||
belongs_to :project, foreign_key: :associated_id
|
||||
|
||||
belongs_to :comment, inverse_of: :project_comment
|
||||
belongs_to :project, inverse_of: :project_comments
|
||||
validates :project, presence: true
|
||||
end
|
||||
|
|
|
@ -22,10 +22,7 @@ class Result < ActiveRecord::Base
|
|||
has_one :result_text,
|
||||
inverse_of: :result,
|
||||
dependent: :destroy
|
||||
has_many :result_comments,
|
||||
inverse_of: :result,
|
||||
dependent: :destroy
|
||||
has_many :comments, through: :result_comments
|
||||
has_many :result_comments, foreign_key: :associated_id, dependent: :destroy
|
||||
has_many :report_elements, inverse_of: :result, dependent: :destroy
|
||||
|
||||
accepts_nested_attributes_for :result_text
|
||||
|
@ -74,11 +71,11 @@ class Result < ActiveRecord::Base
|
|||
|
||||
def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT)
|
||||
last_id = Constants::INFINITY if last_id <= 1
|
||||
comments = Comment.joins(:result_comment)
|
||||
.where(result_comments: { result_id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments = ResultComment.joins(:result)
|
||||
.where(results: { id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments.reverse
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
class ResultComment < ActiveRecord::Base
|
||||
validates :result, :comment, presence: true
|
||||
validates :result_id, uniqueness: { scope: :comment_id }
|
||||
class ResultComment < Comment
|
||||
belongs_to :result, foreign_key: :associated_id
|
||||
|
||||
belongs_to :result, inverse_of: :result_comments
|
||||
belongs_to :comment, inverse_of: :result_comment
|
||||
validates :result, presence: true
|
||||
end
|
||||
|
|
|
@ -14,8 +14,6 @@ class Sample < ActiveRecord::Base
|
|||
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_comments, inverse_of: :sample, dependent: :destroy
|
||||
has_many :comments, through: :sample_comments, dependent: :destroy
|
||||
has_many :sample_custom_fields, inverse_of: :sample, dependent: :destroy
|
||||
has_many :custom_fields, through: :sample_custom_fields
|
||||
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
class SampleComment < ActiveRecord::Base
|
||||
validates :comment, :sample, presence: true
|
||||
validates :sample_id, uniqueness: { scope: :comment_id }
|
||||
|
||||
belongs_to :comment, inverse_of: :sample_comment
|
||||
belongs_to :sample, inverse_of: :sample_comments
|
||||
end
|
|
@ -16,9 +16,7 @@ class Step < ActiveRecord::Base
|
|||
belongs_to :protocol, inverse_of: :steps
|
||||
has_many :checklists, inverse_of: :step,
|
||||
dependent: :destroy
|
||||
has_many :step_comments, inverse_of: :step,
|
||||
dependent: :destroy
|
||||
has_many :comments, through: :step_comments
|
||||
has_many :step_comments, foreign_key: :associated_id, dependent: :destroy
|
||||
has_many :step_assets, inverse_of: :step,
|
||||
dependent: :destroy
|
||||
has_many :assets, through: :step_assets
|
||||
|
@ -77,9 +75,8 @@ class Step < ActiveRecord::Base
|
|||
def destroy(current_user)
|
||||
@current_user = current_user
|
||||
|
||||
# Store IDs of comments, assets & tables so they
|
||||
# Store IDs of assets & tables so they
|
||||
# can be destroyed in after_destroy
|
||||
@c_ids = self.comments.collect { |c| c.id }
|
||||
@a_ids = self.assets.collect { |a| a.id }
|
||||
@t_ids = self.tables.collect { |t| t.id }
|
||||
|
||||
|
@ -92,11 +89,11 @@ class Step < ActiveRecord::Base
|
|||
|
||||
def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT)
|
||||
last_id = Constants::INFINITY if last_id <= 1
|
||||
comments = Comment.joins(:step_comment)
|
||||
.where(step_comments: { step_id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments = StepComment.joins(:step)
|
||||
.where(steps: { id: id })
|
||||
.where('comments.id < ?', last_id)
|
||||
.order(created_at: :desc)
|
||||
.limit(per_page)
|
||||
comments.reverse
|
||||
end
|
||||
|
||||
|
@ -116,8 +113,6 @@ class Step < ActiveRecord::Base
|
|||
protected
|
||||
|
||||
def cascade_after_destroy
|
||||
Comment.destroy(@c_ids)
|
||||
@c_ids = nil
|
||||
# Assets already deleted by here
|
||||
@a_ids = nil
|
||||
Table.destroy(@t_ids)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
class StepComment < ActiveRecord::Base
|
||||
validates :comment, :step, presence: true
|
||||
validates :step_id, uniqueness: { scope: :comment_id }
|
||||
class StepComment < Comment
|
||||
belongs_to :step, foreign_key: :associated_id
|
||||
|
||||
belongs_to :comment, inverse_of: :step_comment
|
||||
belongs_to :step, inverse_of: :step_comments
|
||||
validates :step, presence: true
|
||||
end
|
||||
|
|
5
app/models/task_comment.rb
Normal file
5
app/models/task_comment.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class TaskComment < Comment
|
||||
belongs_to :my_module, foreign_key: :associated_id
|
||||
|
||||
validates :my_module, presence: true
|
||||
end
|
|
@ -946,10 +946,11 @@ module FirstTimeDataGenerator
|
|||
|
||||
def generate_project_comment(project, user, message, created_at = nil)
|
||||
created_at ||= generate_random_time(project.created_at, 1.week)
|
||||
project.comments << Comment.create(
|
||||
ProjectComment.create(
|
||||
user: user,
|
||||
message: message,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
project: project
|
||||
)
|
||||
Activity.new(
|
||||
type_of: :add_comment_to_project,
|
||||
|
@ -964,10 +965,11 @@ module FirstTimeDataGenerator
|
|||
|
||||
def generate_module_comment(my_module, user, message, created_at = nil)
|
||||
created_at ||= generate_random_time(my_module.created_at, 1.day)
|
||||
my_module.comments << Comment.create(
|
||||
TaskComment.create(
|
||||
user: user,
|
||||
message: message,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
my_module: my_module
|
||||
)
|
||||
Activity.new(
|
||||
type_of: :add_comment_to_module,
|
||||
|
@ -983,10 +985,11 @@ module FirstTimeDataGenerator
|
|||
|
||||
def generate_result_comment(result, user, message, created_at = nil)
|
||||
created_at ||= generate_random_time(result.created_at, 1.days)
|
||||
result.comments << Comment.new(
|
||||
ResultComment.new(
|
||||
user: user,
|
||||
message: message,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
result: result
|
||||
)
|
||||
Activity.new(
|
||||
type_of: :add_comment_to_result,
|
||||
|
@ -1002,10 +1005,11 @@ module FirstTimeDataGenerator
|
|||
|
||||
def generate_step_comment(step, user, message, created_at = nil)
|
||||
created_at ||= generate_random_time(step.created_at, 2.hours)
|
||||
step.comments << Comment.new(
|
||||
StepComment.new(
|
||||
user: user,
|
||||
message: message,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
step: step
|
||||
)
|
||||
Activity.new(
|
||||
type_of: :add_comment_to_step,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<%= bootstrap_form_for(@comment, url: @update_url, remote: true, html: { method: :put, class: 'comment-form edit-comment-form' }, data: { role: 'edit-comment-message-form' }) do |f| %>
|
||||
<%= bootstrap_form_for(:comment, url: @update_url, remote: true, html: { method: :put, class: 'comment-form edit-comment-form' }, data: { role: 'edit-comment-message-form' }) do |f| %>
|
||||
<div class="form-group">
|
||||
<div class="input-group">
|
||||
<%= f.smart_text_area :message,
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="edit-comment"
|
||||
data-url="<%= edit_my_module_my_module_comment_path(comment.my_module_comment.my_module, comment, format: :json) %>">
|
||||
data-url="<%= edit_my_module_my_module_comment_path(comment.my_module, comment, format: :json) %>">
|
||||
<%= t('comments.options_dropdown.edit') %>
|
||||
</a>
|
||||
</li>
|
||||
|
@ -27,7 +27,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="delete-comment"
|
||||
data-url="<%= my_module_my_module_comment_path(comment.my_module_comment.my_module, comment, format: :json) %>"
|
||||
data-url="<%= my_module_my_module_comment_path(comment.my_module, comment, format: :json) %>"
|
||||
data-confirm-message="<%= t('comments.delete_confirm') %>">
|
||||
<%= t('comments.options_dropdown.delete') %>
|
||||
</a>
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="edit-comment"
|
||||
data-url="<%= edit_project_project_comment_path(comment.project_comment.project, comment, format: :json) %>">
|
||||
data-url="<%= edit_project_project_comment_path(comment.project, comment, format: :json) %>">
|
||||
<%= t('comments.options_dropdown.edit') %>
|
||||
</a>
|
||||
</li>
|
||||
|
@ -27,7 +27,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="delete-comment"
|
||||
data-url="<%= project_project_comment_path(comment.project_comment.project, comment, format: :json) %>"
|
||||
data-url="<%= project_project_comment_path(comment.project, comment, format: :json) %>"
|
||||
data-confirm-message="<%= t('comments.delete_confirm') %>">
|
||||
<%= t('comments.options_dropdown.delete') %>
|
||||
</a>
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="edit-comment"
|
||||
data-url="<%= edit_result_result_comment_path(comment.result_comment.result, comment, format: :json) %>">
|
||||
data-url="<%= edit_result_result_comment_path(comment.result, comment, format: :json) %>">
|
||||
<%= t('comments.options_dropdown.edit') %>
|
||||
</a>
|
||||
</li>
|
||||
|
@ -28,7 +28,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="delete-comment"
|
||||
data-url="<%= result_result_comment_path(comment.result_comment.result, comment, format: :json) %>"
|
||||
data-url="<%= result_result_comment_path(comment.result, comment, format: :json) %>"
|
||||
data-confirm-message="<%= t('comments.delete_confirm') %>">
|
||||
<%= t('comments.options_dropdown.delete') %>
|
||||
</a>
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
|
||||
<p>
|
||||
<span class="glyphicon glyphicon-comment"></span>
|
||||
<% if comment.project_comment.present? %>
|
||||
<% if comment.is_a?(ProjectComment) && comment.project.present? %>
|
||||
<%=t "search.index.comments.project" %>
|
||||
<% elsif comment.my_module_comment.present? %>
|
||||
<% elsif comment.is_a?(TaskComment) && comment.my_module.present? %>
|
||||
<%=t "search.index.comments.my_module" %>
|
||||
<% elsif comment.step_comment.present? %>
|
||||
<% elsif comment.is_a?(StepComment) && comment.step.present? %>
|
||||
<%=t "search.index.comments.step" %>
|
||||
<% elsif comment.result_comment.present? %>
|
||||
<% elsif comment.is_a?(ResultComment) && comment.result.present? %>
|
||||
<%=t "search.index.comments.result" %>
|
||||
<% end %>
|
||||
</p>
|
||||
|
@ -31,29 +31,29 @@
|
|||
</p>
|
||||
|
||||
<p>
|
||||
<% if comment.project_comment.present? %>
|
||||
<% if comment.is_a?(ProjectComment) && comment.project.present? %>
|
||||
<span>
|
||||
<%=t "search.index.project" %>
|
||||
<%= render partial: "search/results/partials/project_text.html.erb",
|
||||
locals: { project: comment.project_comment.project, link_to_page: :root } %>
|
||||
locals: { project: comment.project, link_to_page: :root } %>
|
||||
</span>
|
||||
<% elsif comment.my_module_comment.present? %>
|
||||
<% elsif comment.is_a?(TaskComment) && comment.my_module.present? %>
|
||||
<span>
|
||||
<%=t "search.index.module" %>
|
||||
<%= render partial: "search/results/partials/my_module_text.html.erb",
|
||||
locals: { my_module: comment.my_module_comment.my_module, link_to_page: :canvas } %>
|
||||
locals: { my_module: comment.my_module, link_to_page: :canvas } %>
|
||||
</span>
|
||||
<% elsif comment.step_comment.present? %>
|
||||
<% elsif comment.is_a?(StepComment) && comment.step.present? %>
|
||||
<span>
|
||||
<%=t "search.index.step" %>
|
||||
<%= render partial: "search/results/partials/step_text.html.erb",
|
||||
locals: { step: comment.step_comment.step, target: :comment } %>
|
||||
locals: { step: comment.step, target: :comment } %>
|
||||
</span>
|
||||
<% elsif comment.result_comment.present? %>
|
||||
<% elsif comment.is_a?(ResultComment) && comment.result.present? %>
|
||||
<span>
|
||||
<%=t "search.index.result" %>
|
||||
<%= render partial: "search/results/partials/result_text.html.erb",
|
||||
locals: { result: comment.result_comment.result, target: :comment } %>
|
||||
locals: { result: comment.result, target: :comment } %>
|
||||
</span>
|
||||
<% end %>
|
||||
</p>
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="edit-comment"
|
||||
data-url="<%= edit_step_step_comment_path(comment.step_comment.step, comment, format: :json) %>">
|
||||
data-url="<%= edit_step_step_comment_path(comment.step, comment, format: :json) %>">
|
||||
<%= t('comments.options_dropdown.edit') %>
|
||||
</a>
|
||||
</li>
|
||||
|
@ -29,7 +29,7 @@
|
|||
<li>
|
||||
<a href="#"
|
||||
data-action="delete-comment"
|
||||
data-url="<%= step_step_comment_path(comment.step_comment.step, comment, format: :json) %>"
|
||||
data-url="<%= step_step_comment_path(comment.step, comment, format: :json) %>"
|
||||
data-confirm-message="<%= t('comments.delete_confirm') %>">
|
||||
<%= t('comments.options_dropdown.delete') %>
|
||||
</a>
|
||||
|
|
72
db/migrate/20170306121855_convert_comments_to_sti_model.rb
Normal file
72
db/migrate/20170306121855_convert_comments_to_sti_model.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
class ConvertCommentsToStiModel < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :comments, :type, :string
|
||||
add_column :comments, :associated_id, :integer
|
||||
add_index :comments, :associated_id
|
||||
|
||||
Comment.find_each do |comment|
|
||||
res = ActiveRecord::Base.connection.execute(
|
||||
"SELECT my_module_id FROM my_module_comments
|
||||
WHERE comment_id = #{comment.id}"
|
||||
)
|
||||
if res.ntuples > 0
|
||||
comment.update_columns(type: 'TaskComment')
|
||||
comment.update_columns(associated_id: res[0]['my_module_id'].to_i)
|
||||
next
|
||||
end
|
||||
|
||||
res = ActiveRecord::Base.connection.execute(
|
||||
"SELECT project_id FROM project_comments
|
||||
WHERE comment_id = #{comment.id}"
|
||||
)
|
||||
if res.ntuples > 0
|
||||
comment.update_columns(type: 'ProjectComment')
|
||||
comment.update_columns(associated_id: res[0]['project_id'].to_i)
|
||||
next
|
||||
end
|
||||
|
||||
res = ActiveRecord::Base.connection.execute(
|
||||
"SELECT result_id FROM result_comments WHERE comment_id = #{comment.id}"
|
||||
)
|
||||
if res.ntuples > 0
|
||||
comment.update_columns(type: 'ResultComment')
|
||||
comment.update_columns(associated_id: res[0]['result_id'].to_i)
|
||||
next
|
||||
end
|
||||
|
||||
res = ActiveRecord::Base.connection.execute(
|
||||
"SELECT step_id FROM step_comments WHERE comment_id = #{comment.id}"
|
||||
)
|
||||
if res.ntuples > 0
|
||||
comment.update_columns(type: 'StepComment')
|
||||
comment.update_columns(associated_id: res[0]['step_id'].to_i)
|
||||
next
|
||||
end
|
||||
end
|
||||
|
||||
drop_table :sample_comments do |t|
|
||||
t.integer :sample_id, null: false
|
||||
t.integer :comment_id, null: false
|
||||
end
|
||||
|
||||
drop_table :project_comments do |t|
|
||||
t.integer :project_id, null: false
|
||||
t.integer :comment_id, null: false
|
||||
end
|
||||
|
||||
drop_table :my_module_comments do |t|
|
||||
t.integer :my_module_id, null: false
|
||||
t.integer :comment_id, null: false
|
||||
end
|
||||
|
||||
drop_table :result_comments do |t|
|
||||
t.integer :result_id, null: false
|
||||
t.integer :comment_id, null: false
|
||||
end
|
||||
|
||||
drop_table :step_comments do |t|
|
||||
t.integer :step_id, null: false
|
||||
t.integer :comment_id, null: false
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1173,10 +1173,11 @@ namespace :db do
|
|||
if rand <= RATIO_COMMENTS
|
||||
user = pluck_random(users)
|
||||
created_at = Faker::Time.backward(500)
|
||||
step.comments << Comment.create(
|
||||
StepComment.create(
|
||||
user: user,
|
||||
message: Faker::Hipster.sentence,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
step: step
|
||||
)
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_step,
|
||||
|
@ -1232,10 +1233,11 @@ namespace :db do
|
|||
def generate_fake_project_comment(project)
|
||||
user = pluck_random(project.users)
|
||||
created_at = Faker::Time.backward(500)
|
||||
project.comments << Comment.create(
|
||||
ProjectComment.create(
|
||||
user: user,
|
||||
message: Faker::Hipster.sentence,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
project: project
|
||||
)
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_project,
|
||||
|
@ -1251,10 +1253,11 @@ namespace :db do
|
|||
def generate_fake_module_comment(my_module)
|
||||
user = pluck_random(my_module.experiment.project.users)
|
||||
created_at = Faker::Time.backward(500)
|
||||
my_module.comments << Comment.create(
|
||||
TaskComment.create(
|
||||
user: user,
|
||||
message: Faker::Hipster.sentence,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
my_module: my_module
|
||||
)
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_module,
|
||||
|
@ -1271,10 +1274,11 @@ namespace :db do
|
|||
def generate_fake_result_comment(result)
|
||||
user = pluck_random(result.my_module.experiment.project.users)
|
||||
created_at = Faker::Time.backward(500)
|
||||
result.comments << Comment.create(
|
||||
ResultComment.create(
|
||||
user: user,
|
||||
message: Faker::Hipster.sentence,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
result: result
|
||||
)
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_result,
|
||||
|
@ -1293,10 +1297,11 @@ namespace :db do
|
|||
def generate_fake_step_comment(step)
|
||||
user = pluck_random(step.protocol.my_module.experiment.project.users)
|
||||
created_at = Faker::Time.backward(500)
|
||||
step.comments << Comment.create(
|
||||
StepComment.create(
|
||||
user: user,
|
||||
message: Faker::Hipster.sentence,
|
||||
created_at: created_at
|
||||
created_at: created_at,
|
||||
step: step
|
||||
)
|
||||
Activity.create(
|
||||
type_of: :add_comment_to_step,
|
||||
|
|
Loading…
Reference in a new issue