Merge pull request #1369 from okriuchykhin/ok_SCI_2830

Refactor result and step comments [SCI-2830]
This commit is contained in:
Alex Kriuchykhin 2018-11-20 00:13:15 +01:00 committed by GitHub
commit a6bc02a2cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 108 additions and 78 deletions

View file

@ -16,18 +16,21 @@ var Comments = (function() {
$.each(comments, function(){
var that = $(this);
var link = that.attr('data-href');
$.ajax({ method: 'GET',
url: link,
beforeSend: animateSpinner(that, true) })
.done(function(data) {
that.html(data.html);
initCommentForm(that);
initCommentsLink(that);
scrollBottom(that.find('.content-comments'));
})
.always(function() {
animateSpinner(that, false);
});
if(that.html().length === 0) {
$.ajax({ method: 'GET',
url: link,
beforeSend: animateSpinner(that, true) })
.done(function(data) {
that.html(data.html);
})
.always(function() {
animateSpinner(that, false);
});
}
initCommentForm(that);
initCommentsLink(that);
scrollBottom(that.find('.content-comments'));
});
}
}
@ -238,7 +241,8 @@ var Comments = (function() {
}
function initDeleteComments(parent) {
$(parent).on('click', '[data-action=delete-comment]', function() {
$(parent).on('click', '[data-action=delete-comment]', function(ev) {
ev.preventDefault();
var $this = $(this);
if (confirm($this.attr('data-confirm-message'))) {
$.ajax({

View file

@ -873,23 +873,21 @@ function bindEditTagsAjax(elements) {
// Reload tags HTML element when modal is closed
manageTagsModal.on("hide.bs.modal", function(){
var tasks = $("div.panel");
var task = $("div.panel[data-module-id='" +
manageTagsModal.data('module-id') + "']");
tasks.each(function(){
var task = $(this);
// Load HTML
$.ajax({
url: task.attr("data-module-tags-url"),
type: "GET",
dataType: "json",
success: function(data){
task.find(".edit-tags-link")
.html(data.html_canvas);
},
error: function(data){
// TODO
}
})
// Load HTML
$.ajax({
url: task.attr("data-module-tags-url"),
type: "GET",
dataType: "json",
success: function(data){
task.find(".edit-tags-link")
.html(data.html_canvas);
},
error: function(data){
// TODO
}
});
});

View file

@ -18,9 +18,9 @@ class ResultCommentsController < ApplicationController
# messages. 'list' partial is used for showing more
# comments.
partial = 'index.html.erb'
partial = 'list.html.erb' if @last_comment_id > 0
partial = 'list.html.erb' if @last_comment_id.positive?
more_url = ''
if @comments.count > 0
if @comments.size.positive?
more_url = url_for(result_result_comments_path(@result,
format: :json,
from: @comments
@ -28,11 +28,14 @@ class ResultCommentsController < ApplicationController
end
render json: {
perPage: @per_page,
resultsNumber: @comments.length,
resultsNumber: @comments.size,
moreUrl: more_url,
html: render_to_string(partial: partial,
locals: { comments: @comments,
more_comments_url: more_url })
html: render_to_string(
partial: partial,
locals: {
result: @result, comments: @comments, per_page: @per_page
}
)
}
end
end

View file

@ -19,20 +19,21 @@ class StepCommentsController < ApplicationController
# messages. 'list' partial is used for showing more
# comments.
partial = 'index.html.erb'
partial = 'list.html.erb' if @last_comment_id > 0
partial = 'list.html.erb' if @last_comment_id.positive?
more_url = ''
if @comments.count > 0
if @comments.size.positive?
more_url = url_for(step_step_comments_path(@step,
format: :json,
from: @comments.first.id))
end
render json: {
perPage: @per_page,
resultsNumber: @comments.length,
resultsNumber: @comments.size,
moreUrl: more_url,
html: render_to_string(partial: partial,
locals: { comments: @comments,
more_comments_url: more_url })
html: render_to_string(
partial: partial,
locals: { step: @step, comments: @comments, per_page: @per_page }
)
}
end
end

View file

@ -71,6 +71,7 @@ class Asset < ApplicationRecord
# Needed because Paperclip validatates on creation
after_initialize :filter_paperclip_errors, if: :new_record?
before_destroy :paperclip_delete, prepend: true
after_save { result&.touch; step&.touch }
attr_accessor :file_content, :file_info, :preview_cached

View file

@ -7,7 +7,7 @@ class Checklist < ApplicationRecord
length: { maximum: Constants::TEXT_MAX_LENGTH }
validates :step, presence: true
belongs_to :step, inverse_of: :checklists, optional: true
belongs_to :step, inverse_of: :checklists, touch: true, optional: true
belongs_to :created_by,
foreign_key: 'created_by_id',
class_name: 'User',

View file

@ -19,17 +19,11 @@ class Result < ApplicationRecord
class_name: 'User',
optional: true
belongs_to :my_module, inverse_of: :results, optional: true
has_one :result_asset,
inverse_of: :result,
dependent: :destroy
has_one :result_asset, inverse_of: :result, dependent: :destroy
has_one :asset, through: :result_asset
has_one :result_table,
inverse_of: :result,
dependent: :destroy
has_one :result_table, inverse_of: :result, dependent: :destroy
has_one :table, through: :result_table
has_one :result_text,
inverse_of: :result,
dependent: :destroy
has_one :result_text, inverse_of: :result, dependent: :destroy
has_many :result_comments, foreign_key: :associated_id, dependent: :destroy
has_many :report_elements, inverse_of: :result, dependent: :destroy

View file

@ -1,7 +1,7 @@
class ResultAsset < ApplicationRecord
validates :result, :asset, presence: true
belongs_to :result, inverse_of: :result_asset, optional: true
belongs_to :result, inverse_of: :result_asset, touch: true, optional: true
belongs_to :asset,
inverse_of: :result_asset,
dependent: :destroy,

View file

@ -2,6 +2,7 @@ class ResultComment < Comment
belongs_to :result,
foreign_key: :associated_id,
inverse_of: :result_comments,
touch: true,
optional: true
validates :result, presence: true

View file

@ -1,7 +1,7 @@
class ResultTable < ApplicationRecord
validates :result, :table, presence: true
belongs_to :result, inverse_of: :result_table, optional: true
belongs_to :result, inverse_of: :result_table, touch: true, optional: true
belongs_to :table,
inverse_of: :result_table,
dependent: :destroy,

View file

@ -4,6 +4,6 @@ class ResultText < ApplicationRecord
presence: true,
length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
validates :result, presence: true
belongs_to :result, inverse_of: :result_text, optional: true
belongs_to :result, inverse_of: :result_text, touch: true, optional: true
has_many :tiny_mce_assets, inverse_of: :result_text, dependent: :destroy
end

View file

@ -3,6 +3,7 @@ class StepAsset < ApplicationRecord
belongs_to :step,
inverse_of: :step_assets,
touch: true,
optional: true
belongs_to :asset,
inverse_of: :step_asset,

View file

@ -2,6 +2,7 @@ class StepComment < Comment
belongs_to :step,
foreign_key: :associated_id,
inverse_of: :step_comments,
touch: true,
optional: true
validates :step, presence: true

View file

@ -1,6 +1,6 @@
class StepTable < ApplicationRecord
validates :step, :table, presence: true
belongs_to :step, inverse_of: :step_tables, optional: true
belongs_to :step, inverse_of: :step_tables, touch: true, optional: true
belongs_to :table, inverse_of: :step_table, optional: true
end

View file

@ -25,6 +25,7 @@ class Table < ApplicationRecord
has_many :report_elements, inverse_of: :table, dependent: :destroy
after_save :update_ts_index
after_save { result&.touch; step&.touch }
#accepts_nested_attributes_for :table
def self.search(user,

View file

@ -5,8 +5,11 @@ class TinyMceAsset < ApplicationRecord
after_destroy :release_team_space
belongs_to :team, inverse_of: :tiny_mce_assets, optional: true
belongs_to :step, inverse_of: :tiny_mce_assets, optional: true
belongs_to :result_text, inverse_of: :tiny_mce_assets, optional: true
belongs_to :step, inverse_of: :tiny_mce_assets, touch: true, optional: true
belongs_to :result_text,
inverse_of: :tiny_mce_assets,
touch: true,
optional: true
has_attached_file :image,
styles: { large: [Constants::LARGE_PIC_FORMAT, :jpg] },
convert_options: { large: '-quality 100 -strip' }

View file

@ -1,7 +1,7 @@
class UserMyModule < ApplicationRecord
validates :user, :my_module, presence: true
belongs_to :user, inverse_of: :user_my_modules, optional: true
belongs_to :user, inverse_of: :user_my_modules, touch: true, optional: true
belongs_to :assigned_by,
foreign_key: 'assigned_by_id',
class_name: 'User',

View file

@ -5,7 +5,7 @@ class UserTeam < ApplicationRecord
validates :user, presence: true
validates :team, presence: true
belongs_to :user, inverse_of: :user_teams, optional: true
belongs_to :user, inverse_of: :user_teams, touch: true, optional: true
belongs_to :assigned_by,
foreign_key: 'assigned_by_id',
class_name: 'User',

View file

@ -14,6 +14,6 @@
</span>
</div>
<span class="help-block hide"></span>
<a data-action="cancel" href="#"><%= t('general.cancel') %></a>
<a data-action="cancel" data-turbolinks="false" href="#"><%= t('general.cancel') %></a>
</div>
<% end %>

View file

@ -51,6 +51,8 @@
<div class="result-comment"
id="result-comments-<%= result.id %>"
data-href="<%= result_result_comments_url(result) %>">
<%= render partial: "result_comments/index.html.erb",
locals: { result: result, comments: result.last_comments, per_page: Constants::COMMENTS_SEARCH_LIMIT } %>
</div>
</div>
<% end %>

View file

@ -48,7 +48,9 @@
<div id="results" data-module-id="<%= @my_module.id %>">
<% ordered_result_of(@my_module).each do |result| %>
<%= render partial: "result", locals: { result: result } %>
<% cache [current_user, result] do %>
<%= render partial: "result", locals: { result: result } %>
<% end %>
<% end %>
</div>
</div>

View file

@ -25,7 +25,9 @@
<div id="steps">
<% @protocol.steps.order(:position).each do |step| %>
<%= render partial: "steps/step.html.erb", locals: { step: step } %>
<% cache [current_user, step] do %>
<%= render partial: "steps/step.html.erb", locals: { step: step } %>
<% end %>
<% end %>
</div>
<% if @protocol.steps.count == 0 %>

View file

@ -1,3 +1,4 @@
<div>
<strong>
<%=t "my_modules.results.comment_title", user: comment.user.full_name, time: l(comment.created_at, format: :time) %>
</strong>
@ -28,7 +29,7 @@
data-action="delete-comment"
data-url="<%= result_result_comment_path(comment.result, comment, format: :json) %>"
data-confirm-message="<%= t('comments.delete_confirm') %>"
data-no-turbolink="true">
data-turbolinks="false">
<%= t('comments.options_dropdown.delete') %>
</a>
</li>

View file

@ -4,24 +4,30 @@
</div>
<hr>
<ul class="no-style double-line content-comments">
<% if @comments.length == @per_page %>
<% if comments.size == per_page %>
<li class="comment-more text-center">
<a class="btn btn-default btn-more-comments" href="<%= more_comments_url %>" data-remote="true">
<a class="btn btn-default btn-more-comments"
href="<%= url_for(result_result_comments_path(result, format: :json, from: comments.first.id)) %>"
data-remote="true">
<%=t "general.more_comments" %>
</a>
</li>
<% end %>
<% if @comments.size == 0 then %>
<% if comments.size == 0 then %>
<li class="no-comments"><em><%= t 'general.no_comments' %></em></li>
<% else %>
<%= render 'result_comments/list.html.erb', comments: @comments %>
<%= render 'result_comments/list.html.erb', comments: comments %>
<% end %>
</ul>
<% if can_create_comments_in_module?(@my_module) then %>
<ul class="no-style double-line">
<li>
<hr>
<%= bootstrap_form_for :comment, url: { format: :json }, method: :post, remote: true, html: { class: 'comment-form' } do |f| %>
<%= bootstrap_form_for :comment,
url: result_result_comments_path(result, format: :json),
method: :post,
remote: true,
html: { class: 'comment-form' } do |f| %>
<%= f.smart_text_area :message,
single_line: true,
hide_label: true,

View file

@ -28,7 +28,8 @@
<a href="#"
data-action="delete-comment"
data-url="<%= step_step_comment_path(comment.step, comment, format: :json) %>"
data-confirm-message="<%= t('comments.delete_confirm') %>">
data-confirm-message="<%= t('comments.delete_confirm') %>"
data-turbolinks="false">
<%= t('comments.options_dropdown.delete') %>
</a>
</li>

View file

@ -4,24 +4,30 @@
</div>
<hr>
<ul class="no-style double-line content-comments">
<% if @comments.length == @per_page %>
<li class="comment-more text-center">
<a class="btn btn-default btn-more-comments" href="<%= more_comments_url %>" data-remote="true">
<%=t "general.more_comments" %>
</a>
</li>
<% if comments.size == per_page %>
<li class="comment-more text-center">
<a class="btn btn-default btn-more-comments"
href="<%= url_for(step_step_comments_path(step, format: :json, from: comments.first.id)) %>"
data-remote="true">
<%=t "general.more_comments" %>
</a>
</li>
<% end %>
<% if @comments.size == 0 then %>
<% if comments.size == 0 %>
<li class="no-comments"><em><%= t 'general.no_comments' %></em></li>
<% else %>
<%= render 'step_comments/list.html.erb', comments: @comments %>
<%= render 'step_comments/list.html.erb', comments: comments %>
<% end %>
</ul>
<% if can_create_comments_in_module?(@protocol.my_module) %>
<ul class="no-style double-line">
<li>
<hr>
<%= bootstrap_form_for :comment, url: { format: :json }, html: { class: 'comment-form', id: "step-comment-#{@step.id}" }, method: :post, remote: true do |f| %>
<%= bootstrap_form_for :comment,
url: step_step_comments_path(step, format: :json),
html: { class: 'comment-form', id: "step-comment-#{step.id}" },
method: :post,
remote: true do |f| %>
<%= f.smart_text_area :message,
single_line: true,
hide_label: true,

View file

@ -151,6 +151,8 @@
<div class="step-comment"
id="step-comments-<%= step.id %>"
data-href="<%= url_for step_step_comments_path(step_id: step.id, format: :json) %>">
<%= render partial: "step_comments/index.html.erb",
locals: { step: step, comments: step.last_comments, per_page: Constants::COMMENTS_SEARCH_LIMIT } %>
</div>
</div>
<% end %>