Commit f31e7703 authored by Sara Kuqi's avatar Sara Kuqi
Browse files

pdf in development, row colors, adding results, about input, submitted_at & measured_at

parent 143c86e5
......@@ -33,10 +33,10 @@ gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'listen', '~> 3.0.5'
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
......
......@@ -100,6 +100,8 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (6.0.1)
railties (>= 3.2.16)
json (2.0.2)
kgio (2.10.0)
listen (3.0.8)
......@@ -230,6 +232,7 @@ DEPENDENCIES
font-awesome-rails
jbuilder (~> 2.5)
jquery-rails
jquery-ui-rails
json
listen (~> 3.0.5)
mail
......
#$(document).ready ->
# $('button').click ->
# $('.status').replaceWith 'Measured'
# return
#return
$(document).ready(function(){
$('#tb tr.y_n').each(function(i){
let good = true;
$(this).find('td.y_n').each(function(j){
if ($(this).text() == 'NaN'){ return good = false}
if ($(this).text() > 0.4){ return good = false}
});
good ? $(this).css('background-color','#a9edb8') : $(this).css('background-color','#eda9ca')
});
console.log('it works too');
$('tbody#resultTable').on('cocoon:before-insert', function() {
console.log('yepyep');
});
})
// $( document ).on('turbolinks:load', function() {
// $('#tb tr.y_n').each(function(i){
// let good = true;
// $(this).find('td.y_n').each(function(j){
// if ($(this).text() == 'NaN'){ return good = false}
// if ($(this).text() > 0.4){ return good = false}
// });
// good ? $(this).css('background-color','#a9edb8') : $(this).css('background-color','#eda9ca')
// });
// })
......@@ -14,3 +14,15 @@
*= require_self
*= require font-awesome
*/
.sample-submitted {
background-color: #e2f7e7;
}
.sample-in_preparation {
background-color: #feffea;
}
.sample-measured {
background-color: #f9e5ef;
}
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: :destroy
def new
@result = Result.new
......@@ -18,7 +17,7 @@ class ResultsController < ApplicationController
def create
@result = Result.new(result_params)
#@result.user_id = current_user.id
respond_to do |format|
if @result.save
format.html { redirect_to @result, notice: 'Result was successfully created.' }
......@@ -32,7 +31,6 @@ class ResultsController < ApplicationController
def update
@result = Result.find(result_params[:result_id])
#result = current_user.id
@result.row_order_position = result_params[:row_order_position]
respond_to do |format|
if @result.update(result_params)
......@@ -58,13 +56,8 @@ class ResultsController < ApplicationController
def set_result
@result = Result.find(params[:id])
end
def correct_user
@result = current_user.results.find_by(id: params[:id])
#redirect_to root_url if @result.nil?
end
def result_params
params.require(:result).permit(:sample_id, :user_id, :measurement, :name, :element_C,:element_H,:element_N,:element_S)
params.require(:result).permit(:sample_id, :measurement, :name)
params.fetch(:result, {})
end
end
......@@ -93,6 +93,7 @@ class SamplesController < ApplicationController
sample = Sample.find(params[:id])
if sample.status == "in preparation"
sample.status = "submitted"
sample.submitted_at = Time.new
sample.save
end
redirect_to request.referer + "?display_sample=#{sample.id}"
......@@ -103,8 +104,9 @@ class SamplesController < ApplicationController
@user = sample && sample.user
if sample.status == "submitted"
sample.status = "measured"
sample.measured_at = Time.new
# sample.operator=current_user.name
sample.save
@operator = current_user.id
end
UserMailer.delay.measured_email(@user,sample)
redirect_to request.referer + "?display_sample=#{sample.id}"
......
......@@ -3,16 +3,31 @@ module SamplesHelper
def add_subscript formula
render inline: formula.to_s.scan(/([A-Z][a-z]{0,2})(\d*)/).map { |el| "#{el[0]}<sub>#{el[1]}</sub>" }.join('')
end
#render :inline =>
def percentage_diff(expected,measured)
e = expected.to_f
m = measured.to_f
if m == e
0.0
else
# 200 * (m - e)/(m + e)
m - e
def percentage_diff(expected,measured)
e = expected.to_f
m = measured.to_f
if m == e
0.00
elsif e == 0
'NaN'
else
((m - e).abs/e).round(2)
end
end
def sum_other sample
sum,i = 0,0
sample.formatted_calculation.each_pair do |k,v|
if i<4
sum=sum.to_f+v.to_f
i+=1
else
break
end
end
s = 100 - sum
s.abs.round(2)
end
end
......@@ -8,16 +8,25 @@ class SamplePdf < Prawn::Document
@with_results = with_results
text "name: \ #{@user.name}
id: \ #{@sample.id}
name: \ #{@sample.name}
formula_name: \ #{@sample.name}
formula:\ #{@sample.formula}
status: \ #{@sample.status}", size: 15, style: :bold
if @sample.status=="submitted"
text "Submitted at: \ #{@sample.submitted_at}", size: 15, style: :bold
elsif @sample.status=="measured"
text "Measured at: \ #{@sample.measured_at}",size: 15, style: :bold
else
text "Created at: \ #{@sample.created_at}",size: 15, style: :bold
end
move_down 20
table(table_data,width: bounds.width)
table_rows, style: :bold
move_down 100
text I18n.l(DateTime.now.to_date, format: "%d %B %Y").to_s
move_down 30
text "Signature_____________________"
end
def table_header
......@@ -81,10 +90,10 @@ class SamplePdf < Prawn::Document
diff[k] = percentage_diff(v,result.send('element_'+k))
end if @sample.calculation
update +=["%.2f" %diff['C']]
update +=["%.2f" %diff['N']]
update +=["%.2f" %diff['H']]
update +=["%.2f" %diff['S']]
update +=[ diff['C']]
update +=[ diff['N']]
update +=[ diff['H']]
update +=[ diff['S']]
update +=[""]
update +=[""]
@sample.formatted_calculation.each_pair do |k,v|
......@@ -100,9 +109,11 @@ class SamplePdf < Prawn::Document
e = expected.to_f
m = measured.to_f
if m == e
0.0
else
(m - e).abs
"%.2f" % 0.00
elsif e == 0
'NaN'
else
"%.2f" % ((m - e).abs/e).round(2)
end
end
......
require_relative "pdfs/sample_pdf.rb"
class SamplePdf
end
class Result < ApplicationRecord
belongs_to :sample
belongs_to :user
#belongs_to :user
#validates :user_id, presence: true
before_save :measurement
......
class User < ApplicationRecord
has_many :samples, dependent: :destroy
has_many :microposts, dependent: :destroy
has_many :results, dependent: :destroy
#has_many :results, dependent: :destroy
self.inheritance_column = :type
attr_accessor :remember_token, :activation_token, :reset_token
......@@ -9,9 +9,11 @@ class User < ApplicationRecord
before_save { self.email = email.downcase }
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
#VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
#format: { with: VALID_EMAIL_REGEX }
format: { with: /\b[A-Z0-9._%a-z\-+@]kit\.edu\z/,
message: "must be a 'kit.edu' account" },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
......
<%= javascript_include_tag "application" %>
<% provide(:title, "Edit sample") %>
<%if @sample.status=="in preparation"%>
<!-- <%= form_for(@sample) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<fieldset class="form-group">
<%= f.label :Name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :Formula %>
<%= f.text_field :formula, class: 'form-control' %>
</fieldset>
<%= f.submit "update", class: "btn btn-success" %>
<% end %> -->
<div class="div-table">
<div class="div-table-row">
<div class="div-table-col" style="font-weight:bold">ID</div>
<div class="div-table-col" style="font-weight:bold"> Name</div>
<div class="div-table-col" style="font-weight:bold">Formula</div>
<div class="div-table-col" style="font-weight:bold">Status</div>
<div class="div-table-col" style="font-weight:bold">Action</div>
</div>
<div class="div-table-row">
<%= form_for(@sample) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="div-table-col" style="text-align:left"><%= @sample.id%></div>
<div class="div-table-col" style="padding:0px"> <%= f.text_field :name, class: 'form-control' %></div>
<div class="div-table-col" style="padding:0px"><%= f.text_field :formula, class: 'form-control' %></div>
<div class="div-table-col" style="text-align:left"><%= @sample.status%></div>
<div class="div-table-col" style="padding:7px"> <%= f.submit "save", class: "button-link"%></div>
<%end%>
</div>
</div>
<% end %>
<div style='margin-top:50px'>
<div class="div-table">
<div class="div-table-row">
<div class="div-table-col" style="font-weight:bold">ID</div>
<div class="div-table-col" style="font-weight:bold"> Name</div>
<div class="div-table-col" style="font-weight:bold">Formula</div>
<div class="div-table-col" style="font-weight:bold">Status</div>
<div class="div-table-col" style="font-weight:bold">Action</div>
</div>
<div class="div-table-row">
<%= form_for(@sample) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="div-table-col" style="text-align:left"><%= @sample.id%></div>
<div class="div-table-col" style="padding:0px"> <%= f.text_field :name, class: 'form-control' %></div>
<div class="div-table-col" style="padding:0px"><%= f.text_field :formula, class: 'form-control' %></div>
<div class="div-table-col" style="text-align:left"><%= @sample.status%></div>
<div class="div-table-col" style="padding:7px"> <%= f.submit "save", class: "button-link"%></div>
<%end%>
</div>
</div>
<% end %>
<div style='margin-top:50px'>
<% if operator? && @sample.status=="submitted"%>
<%= render 'form_operator' %>
<%#else %>
<%#= render 'shared/sample_form' %>
<%end%>
<div class="container">
<strong><em>
<%#= link_to 'Back', root_path %>
</strong></em>
</form>
</div>
<script>
$('#icol').click(function(e){
e.preventDefault();
var counter = 0;
var newElem = function(){
return $('#col').val();}
if($('#col').val()){
$('#myTable tr').append($("<td>"));
$('#myTable thead tr>td:last').html($('#col').val());
$('#myTable tbody tr').each(function(){
$(this).children('td:last').append('<input type = "text" name="sample[results_attributes][' + counter +'][measurement]['+newElem()+']" id="percentage' + counter + '" value="">');
counter++;
});
} else {
alert('Enter Text');
}
});
</script>
<%end%>
</div>
......@@ -14,53 +14,48 @@
<div class="col-sm-11">
<table class="table table-bordered" id = "myTable" style="margin-left:20px;">
<thead>
<tr>
<th style=" border-bottom:1pt solid black;">ELEMENTS:</th>
<thead>
<tr>
<th >ELEMENTS:</th>
<% Sample::PREDEF_ELEMENTS.each do |k| %>
<th style=" border-bottom:1pt solid black;"><%= k %></th>
<th ><%= k %></th>
<%end if @sample.calculation%>
<th style=" border-bottom:1pt solid black;">Other</th>
<th >Other</th>
<% (@sample.calculation.keys-Sample::PREDEF_ELEMENTS).each do|k| %>
<th><%= k %></th>
<%end if @sample.calculation%>
</tr>
</thead>
<tbody id= "resultTable">
<tr style=" border:1pt solid black;">
<td style=" border:1pt solid black;">Expected:</td>
<% Sample::PREDEF_ELEMENTS.each do |k| %>
<td style=" border:1pt solid black;"> <%= "%.2f" % (@sample.calculation[k] || 0) %></td>
<%end if @sample.calculation %>
<%sum,i=0,0 %>
<% @sample.formatted_calculation.each_pair do |k,v| %>
<%if i<4 %>
<% sum=sum.to_f+v.to_f %>
<%i=i+1 %>
<%else
break%>
<%end%>
<%end%>
<% s = 100 - sum %>
<td style=" border:1pt solid black;"><%=s.abs.round(2)%></td>
<tr>
<td >Expected:</td>
<% Sample::PREDEF_ELEMENTS.each do |k| %>
<td> <%= "%.2f" % (@sample.calculation[k] || 0) %></td>
<%end if @sample.calculation %>
<td ><%=sum_other(@sample)%></td>
<% (@sample.calculation.keys-Sample::PREDEF_ELEMENTS).each do|k| %>
<td> <%= "%.2f" % @sample.calculation[k] %></td>
<%end if @sample.calculation%>
</tr>
<% (@sample.calculation.keys-Sample::PREDEF_ELEMENTS).each do|k| %>
<td> <%= "%.2f" % @sample.calculation[k] %></td>
<%end if @sample.calculation%>
</tr>
<fieldset id="sample-results">
<%= f.fields_for :results do |result|;render 'result_fields', f: result;end %>
</fieldset>
<fieldset id="sample-results">
<%= f.fields_for :results do |result| %>
<%= render 'result_fields', f: result %>
<% end %>
</fieldset>
</tbody>
</table>
<div class="links" style="margin-left:30px">
<%= link_to_add_association 'Add Result', f, :results, 'data-association-insertion-node' => "tbody#resultTable ", 'data-association-insertion-method' => "append",
:wrap_object => Proc.new {|quantity| quantity } , :style=>'color: Black;' , :class=>"btn btn-block btn-success " %>
<%= link_to_add_association 'Add Result', f, :results,
'data-association-insertion-node' => "tbody#resultTable ",
'data-association-insertion-method' => "append",
:style=>'color: Black;' , :class=>"btn btn-block btn-success " %>
<br>
<%= f.submit %>
</div>
<% end %>
<script>
$('tbody#resultTable').on('cocoon:before-insert', function() {
console.log('yepyep');
});
</script>
......@@ -4,5 +4,4 @@
<td><%= f.text_field :element_N %></td>
<td><%= f.text_field :element_H %></td>
<td><%= f.text_field :element_S %></td>
</tr>
<script type="text/javascript">
$(document).ready(function(){
$('#tab_logic tr.selekt .status').each(function(){
console.log($(this).parent(".selekt"))
if ($(this).text() =="in preparation") {
$(this).parents(".selekt").css('background-color','#feffea');
}
else if ($(this).text() =="submitted") {
$(this).parents(".selekt").css('background-color','#e2f7e7')
}
else {
$(this).parents(".selekt").css('background-color','#f9e5ef')
}
});
});
</script>
<%= javascript_include_tag "application" %>
<% if (current_page?('/samples, /index') || current_page?('/'))%>
<div class="panel panel-default">
<%=render 'shared/sample_form'%><br>
......@@ -43,6 +27,9 @@ $(document).ready(function(){
<%end%>
</th>
<%if operator?%>
<th class="text-center">
User Name
</th>
<th class="text-center">
Email
</th>
......@@ -54,7 +41,7 @@ $(document).ready(function(){
</thead>
<tbody>
<%@samples.each do |sample|%>
<tr class="selekt">
<tr class="<%="selekt sample-#{sample.status.gsub(/ /,"_")}" %>" >
<td>
<%="#{sample.id}"%>
<%=link_to '<i class="fa fa-caret-square-o-left" aria-hidden="true"></i>'.html_safe, sample, remote: true%>
......@@ -70,6 +57,9 @@ $(document).ready(function(){
<span class="status"><%= sample.status %></span>
</td>
<%if operator?%>
<td>
<span class="name"><%= sample.try(:user).try(:name) %></span>
</td>
<td>
<span class="email"><%= sample.try(:user).try(:email) %></span>
</td>
......
<script>
$(document).ready(function() {
$('#tab_logic tr.selekt .status').each(function() {
console.log($(this).parent('.selekt'));
if ($(this).text() =="in preparation") {
$(this).parents(".selekt").css('background-color','#feffea');
}
else if ($(this).text() =="submitted") {
$(this).parents(".selekt").css('background-color','#e2f7e7')
}
else {
$(this).parents(".selekt").css('background-color','#f9e5ef')
}
});
});
</script>
<%= javascript_include_tag "application" %>
<br>
<%if @sample.status=="submitted"%>
<p>Submitted at: <%=@sample.submitted_at%></p>
<%elsif @sample.status=="measured"%>
<p>Measured at: <%=@sample.measured_at%></p>
<%else%>
<p>Created at: <%= @sample.created_at%></p>
<%end%>
<table id="tab_logic" class="table table-bordered table-hover table-width" style="margin-top:30px;margin-left:25px;width:95%" >
<thead>
<tr>
......@@ -26,7 +19,7 @@ $(document).ready(function() {
</thead>
<tbody>
<%[@sample].each do |sample|%>
<tr class="selekt">
<tr class="<%="selekt sample-#{sample.status.gsub(/ /,"_")}" %>">
<td><%= @sample.id%></td>
<td><span class="name"><%= sample.name %></span></td>
<td><span class="formula"><%=add_subscript(sample.formula) %></span></td>
......@@ -55,19 +48,8 @@ $(document).ready(function() {
</div>
<%end%>
<script type="text/javascript">
$(document).ready(function(){
$('#myTable td.y_n').each(function(){
if ($(this).text() < 0.4) {
$(this).css('background-color','#a9edb8');
}
else {
$(this).css('background-color','#eda9ca');
}
});
});
</script>
<table class="table table-bordered table-hover", id = "myTable", style="margin-top:50px;margin-left:25px;width:95%">
<table class="table table-bordered table-hover", id = "tb", style="margin-top:50px;margin-left:25px;width:95%">
<thead>
<tr>
<th>ELEMENTS:</th>
......@@ -117,17 +99,17 @@ $(document).ready(function() {
<td><%= "%.2f" % sum2.round(2)%></td>
<%sum2=0%>
</tr>
<tr>
<tr class="y_n">
<td> Difference (%) </td>
<%diff={}%>
<%Sample::PREDEF_ELEMENTS.each do |k| %>
<% v = @sample.calculation[k]%>
<% diff[k] = percentage_diff(v,result.send('element_'+k)) %>
<%end if @sample.calculation %>
<td class="y_n"><%=diff['C'].abs.round(2)%></td>
<td class="y_n"><%=diff['N'].abs.round(2)%></td>
<td class="y_n"><%=diff['H'].abs.round(2)%></td>
<td class="y_n"><%=diff['S'].abs.round(2)%></td>
<td class="y_n"><%=diff['C']%></td>
<td class="y_n"><%=diff['N']%></td>
<td class="y_n"><%=diff['H']%></td>
<td class="y_n"><%=diff['S']%></td>
<%@sample.formatted_calculation.each_pair do |k,v| %>
<%( result.measurement.keys - ["C","H","N","S"]).each do |k| %>
<%=( (v.to_f - result.measurement[k].to_f) / ((v.to_f + result.measurement[k].to_f)/2) ) * 100 %>
......@@ -138,4 +120,4 @@ $(document).ready(function() {
<%end %>
</tbody>
</table>
<p><%=link_to "Download PDF", sample_path(@sample, format: "pdf") %><p>