Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Deli authored and Dennis Deli committed Dec 16, 2024
1 parent 3846b36 commit 71dd64e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 30 deletions.
31 changes: 22 additions & 9 deletions app/assets/javascripts/request_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $(document).ready(function() {



$("#note-textarea").bind('keyup', function(){
checkCount();
$(document).on('keyup', '[id^="note-textarea_"]', function () {
checkCount($(this));
});


Expand All @@ -18,16 +18,29 @@ $(document).ready(function() {
$( "#request_history_log" ).load( data_url);
} );

$(document).on('shown.bs.modal', '[id^="item_history_popup_"]', function () {
var $modal = $(this);
var itemId = $modal.attr('id').split('_').pop();
var dataUrl = $modal.find("#request_history_log_" + itemId).data("url");

if (dataUrl) {
$modal.find("#request_history_log_" + itemId).load(dataUrl);
}
});

});

/* Checks the count of the note text area and changes color to red if limit hit */
function checkCount(){
var textAreaValue = $("#note-textarea").val();
$('.remaining').html(textAreaValue.length);
function checkCount(textArea) {
var textAreaValue = textArea.val();
var itemId = textArea.attr('id').split('_').pop();
var remainingCounter = $(`#item_history_popup_${itemId} .remaining`);

remainingCounter.text(textAreaValue.length);

if (textAreaValue.length > 255) {
$('.remaining').addClass("red");
}
else {
$('.remaining').removeClass("red");
remainingCounter.addClass("red");
} else {
remainingCounter.removeClass("red");
}
}
37 changes: 28 additions & 9 deletions app/controllers/request_history_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ class RequestHistoryController < ApplicationController
authorize_resource User
# skip_authorization_check

def index
@audits = @request.audits | @request.associated_audits # | @request.course.audits
@audits.sort! { |a, b| a.created_at <=> b.created_at }
def index
@audits = @audits = @request.associated_audits.where.not(associated_type: 'item') # | @request.course.audits

@audits_grouped = @audits.reverse.group_by { |a| a.created_at.at_beginning_of_day }

Expand All @@ -17,29 +16,49 @@ def index
render partial: 'history_log', layout: false if request.xhr?
end

def item_history
item_id = params[:item_id]
@audits = @request.audits.where(associated_type: 'item', associated_id: item_id.to_i).order(created_at: :desc)

@audits_grouped = @audits.reverse.group_by { |a| a.created_at.at_beginning_of_day }

@users = User.all
@locations = Location.active

render partial: 'history_log', layout: false if request.xhr?
end



def create
# @aud = Audited::Adapters::ActiveRecord::Audit.new
@item_id = params[:item_id]
@aud = Audited::Audit.new
@aud.action = 'note'
@aud.user = current_user
@aud.auditable = @request
@aud.audited_changes = 'Note Added'
@aud.comment = params[:audit_comment]

if @item_id.present?
@aud.associated_id = @item_id
@aud.associated_type = "item"
end


respond_to do |format|
redirect_path = @item_id.present? ? item_history_request_path(@request, item_id: @item_id) : history_request_path(@request)
if @aud.comment.blank?
format.html { redirect_to history_request_path(@request), alert: 'Note is blank. Nothing updated' }
format.html { redirect_to redirect_path, alert: 'Note is blank. Nothing updated' }
format.js
elsif @aud.comment.length > 255
format.html { redirect_to history_request_path(@request), alert: 'Note is more than 255 characters!' }
format.html { redirect_to redirect_path, alert: 'Note is more than 255 characters!' }
format.js
elsif @aud.save
format.html { redirect_to history_request_path(@request), notice: 'Note saved' }
format.html { redirect_to redirect_path, notice: 'Note saved' }
format.js
else
format.html do
redirect_to history_request_path(@request), alert: 'Note not saved. Please contact application administrator'
end
format.html { redirect_to redirect_path, alert: 'Note not saved. Please contact application administrator' }
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/views/items/_item_actions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
<% end %>

<% if can?(:change_status, item) && item.status != Item::STATUS_DELETED%>
<br/><br/>
<a data-toggle="modal" href="#history_popup" data-target="#history_popup" class="btn btn-xs btn-default">
Add a Note
</a>
<br/><br/>
<%= render partial: 'requests/item_history_log_modal_', locals: { item: item } %>
<!-- Adding a link with text -->
<%= link_to 'Add a Note / History Log', '#', data: { toggle: 'modal', target: "#item_history_popup_#{item.id}" }, class: 'btn btn-xs btn-default' %>
<% end %>
5 changes: 3 additions & 2 deletions app/views/request_history/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<!-- Add a note form -->
<%= form_tag(save_note_request_path(format: "js"), method: "post", class: "form", role: "form", remote: true ) do %>
<%= form_tag(save_note_request_path(@request, format: "js"), method: "post", remote: true) do %>
<div class="row">
<%= hidden_field_tag 'item_id', local_assigns[:item_id] %>
<div class="col-md-2 small black strong"><%= Date.today.strftime('%b %e, %Y')%></div>
<div class="col-md-6">
<%= text_area_tag("audit_comment", nil, class:"form-control", id:"note-textarea", title: "Add a note") %><br/>
<%= text_area_tag("audit_comment", nil, class:"form-control", id: "note-textarea_#{local_assigns[:item_id]}", title: "Add a note") %><br/>
<p class="trivial italic">Character count is <span class="remaining"></span> / 255 maximum allowed</p>
<p class="bg-danger hide" id="history_log_error"></p>
</div>
Expand Down
14 changes: 8 additions & 6 deletions app/views/request_history/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

<% if @aud.comment.blank? %>
$("#history_log_error").text("Note is blank. Please fill it in.").removeClass("hide");
$("#item_history_popup_<%= @aud.associated_id %> #history_log_error").text("Note is blank. Please fill it in.").removeClass("hide");


<% elsif @aud.comment.length > 255 %>
$("#history_log_error").text("Note is more than 255 characters!").removeClass("hide");
$("#item_history_popup_<%= @aud.associated_id %> #history_log_error").text("Note is blank. Please fill it in.").removeClass("hide");
<% else %>
$("#history_log_error").addClass("hide");
$('#history_popup form').trigger("reset");
$("#item_history_popup_<%= @aud.associated_id %> #note-textarea").val("");
$("#item_history_popup_<%= @aud.associated_id %> #history_log_error").addClass("hide");
$('#history_popup_<%= @aud.associated_id %> form').trigger("reset"); <!-- Reset the form for the specific item -->

var url = $("#request_history_log").data("url");
$( "#request_history_log" ).load( url);
var url = $("#request_history_log_<%= @aud.associated_id %>").data("url"); <!-- Get the URL for the specific item -->
$("#request_history_log_<%= @aud.associated_id %>").load(url); <!-- Load the data for the specific item -->

<% end %>
35 changes: 35 additions & 0 deletions app/views/requests/_item_history_log_modal_.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<% if can?(:add_note, @request) %>

<div class="modal fade" id="item_history_popup_<%= item.id %>" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Item History</h4>
</div>

<div class="modal-body">

<div class="panel panel-default">
<div class="panel-heading">
Add a note
</div>
<div class="panel-body">
<%= render partial: 'request_history/form', locals: { remote: true, item_id: item.id } %>
</div>
</div>

<div id="request_history_log_<%= item.id %>" data-url="<%= item_history_request_path(@request, item_id: item.id, format: "js") %>"></div>

</div>

</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
post 'save_note' => 'request_history#create'
post :rollover
get :rollover_confirm
get 'item_history/:item_id', to: 'request_history#item_history', as: 'item_history'
end

resource :copy_items, only: %i[show new create], module: 'requests'
Expand Down

0 comments on commit 71dd64e

Please sign in to comment.