Commit 5ed88515 authored by Sara Kuqi's avatar Sara Kuqi
Browse files

Edit sample

Create edit_sample page, show page
Create Helper for subscript at formula
Create Result controller and Result table
Add measurment to results
Add calsulation to samples
Creating periodic table with elements in lib folder
parent e0a64ace
......@@ -13,7 +13,8 @@ gem 'bootstrap-will_paginate', '~> 0.0.10'
#bootstrap
gem 'bootstrap-sass', '~>3.3.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
#gem 'sqlite3'
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
......
......@@ -89,6 +89,7 @@ GEM
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
pg (0.18.4)
pkg-config (1.1.7)
puma (3.4.0)
rack (2.0.1)
......@@ -139,7 +140,6 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.11)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
......@@ -172,12 +172,12 @@ DEPENDENCIES
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
pg
puma (~> 3.0)
rails (~> 5.0.0)
sass-rails (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
......
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
#$(document).ready ->
# $('button').click ->
# $('.status').replaceWith 'Measured'
# return
#return
......@@ -212,3 +212,18 @@ span.picture {
.table-nonfluid {
width: auto !important;
}
input.transparent-input{
background-color:transparent !important;
border:none !important;
}
.button-link {
background-color: transparent;
border: none;
color:#008fb3;
}
.button-link:hover {
color: #006680;
text-decoration: underline;
}
// Place all the styles related to the Results controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class ResultsController < ApplicationController
def new
end
end
class SamplesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def new
@sample=Sample.new
end
def create
@sample = current_user.samples.build(sample_params.merge(status:"In preparation"))
if @sample.save
flash[:success] = "Sample created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
def new
@sample=Sample.new
end
end
def create
@sample = current_user.samples.build(sample_params.merge(status:"in preparation"))
if @sample.save
flash[:success] = "Sample created!"
redirect_to root_url
else
@samples = current_user.samples[0..-2]
render 'static_pages/home'
end
end
def index
@samples = Sample.all
end
def edit
@sample = Sample.find(params[:id])
@sample = Sample.find(params[:id])
end
def update
@sample = Sample.find(params[:id])
sample = Sample.find(params[:id])
if sample.update_attributes(sample_params)
flash[:success] = "Sample updated"
redirect_to @sample
redirect_to sample
else
render 'edit'
end
end
def show
@samples = @user.samples.paginate(page: params[:page])
@sample = current_user.samples.find(params[:id])#.paginate(page: params[:page])
end
def destroy
......@@ -42,16 +45,23 @@ class SamplesController < ApplicationController
redirect_to request.referrer || root_url
end
private
def sample_params
params.require(:sample).permit(:name, :formula)
end
def correct_user
@sample = current_user.samples.find_by(id: params[:id])
redirect_to root_url if @sample.nil?
def Measured
sample = Sample.find(params[:id])
if sample.status == "in preparation"
sample.status = "submitted"
sample.save
end
redirect_to sample_path(sample)
end
private
end
def sample_params
params.require(:sample).permit(:name, :formula)
end
def correct_user
@sample = current_user.samples.find_by(id: params[:id])
redirect_to root_url if @sample.nil?
end
end
......@@ -2,12 +2,12 @@ class StaticPagesController < ApplicationController
def home
if logged_in?
@sample = current_user.samples.build
@samples = current_user.samples
@feed_items = current_user.feed.paginate(page: params[:page],per_page: 2 )
end
end
@samples = current_user.samples[0..-1]
@sample = current_user.samples.build
@feed_items = current_user.feed.paginate(page: params[:page],per_page: 2 )
end
end
def help
end
......
module ResultsHelper
end
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 =>
end
class Result < ApplicationRecord
end
class Sample < ApplicationRecord
attr_reader :formul
attr_accessor :mw
belongs_to :user
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :name, presence: true, length: { maximum: 50 }
validates :formula, presence: true, length: { maximum: 140 }
validates :status, presence: true, length: { maximum: 140 }
end
def parse_formula
left_over = formula
@formul={}
while !left_over.blank?
left_over.match(REG)
if !$1
errors.add('Warning','Formula is not valid')
return false
#raise ArgumentError.new('Formula is not valid')
end
left_over = $'
if $2 == ""
then value = 1
else
value = $2.to_i
end
@formul[$1] = value
end
@formul
end
def calculate_mw
@mw=0
@formul.each_pair do |key, value|
puts "#{key} is #{value}"
@mw += get_atomic_weight(key)*value
end
@mw
end
def calculate_percentage
@expected_result={}
return false unless parse_formula
calculate_mw
@formul.each_pair{|k,v| @expected_result[k]=((v*get_atomic_weight(k)/@mw)*100).to_f}
assign_attributes(calculation: @expected_result)
end
......@@ -43,7 +43,7 @@ class User < ApplicationRecord
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
update_attribute(:remember_token, User.digest(remember_token))
end
def authenticated?(remember_token)
......@@ -52,7 +52,7 @@ class User < ApplicationRecord
end
# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
update_attribute(:remember_token, nil)
end
end
class Operator < User; end
......
<h1>Results#new</h1>
<p>Find me in app/views/results/new.html.erb</p>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover table-nonfluid" id="tab_logic">
<table class="table table-bordered table-hover table-nonfluid" id="tab_logic" style="margin-top:30px">
<thead>
<tr >
<th class="text-center">
......@@ -22,31 +22,29 @@
</tr>
</thead>
<tbody>
<%@samples.each_with_index do |sample,index|%>
<tr id='addr0'>
<%@samples.each do |sample|%>
<tr>
<td>
<%= index + 1 %>
<%="#{sample.id}"%>
</td>
<td>
<span class="name"><%= sample.name %></span>
<span class="name"><%= sample.name %></span>
</td>
<td>
<span class="formula"><%= sample.formula %></span>
<span class="formula"><%= add_subscript(sample.formula) %></span>
</td>
<td>
<span class="status"><%= sample.status %></span>
</td>
<td>
<%= link_to "show", samples_path %>
<%= link_to "edit" %>
<% if current_user?(sample.user) %>
<%= link_to "delete", sample, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</td>
</tr>
<%end%>
<span class="user"><%#= link_to sample.user.name, sample.user %></span><br>
<%= link_to "show", sample %>
<% if current_user?(sample.user) && sample.status == "in preparation" %>
<%= link_to "delete", sample, method: :delete,
data: { confirm: "You sure?" }%>
<%end%>
</td>
</tr>
<%end%>
</tbody>
</table>
</div>
......
<% provide(:title, 'Samples') %>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover table-nonfluid" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
Formula
</th>
<th class="text-center">
Staus
</th>
<th class="text-center">
Action
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<span class="name"><%= sample.name %></span>
</td>
<td>
<span class="formula"><%= sample.formula %></span>
</td>
<td>
<span class="status"><%= sample.status %></span>
</td>
<p> <%= f.submit "Save changes", class: "btn btn-primary" %> </p>
<li id="sample-<%= sample.id %>">
<tr id='addr1'><span class="user"><%= link_to sample.user.name, sample.user %></span><br></tr>
</tbody>
</table>
</div>
</div>
</div>
<% provide(:title, "Edit sample") %>
<table class="table table-bordered table-nonfluid" id="tab_logic" style="margin-top:50px">
<thead>
<tr >
<th class="text-center">
ID
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
Formula
</th>
<th class="text-center">
Action
</th>
</tr>
</thead>
<tbody>
<%= form_for(@sample) do |f| %>
<td>
<%= @sample.id%>
</td>
<td>
<%= f.text_field :name, class: 'form-control transparent-input' %>
</td>
<td>
<%= f.text_field :formula, class: 'form-control transparent-input' %>
</td>
<td>
<%= f.submit "save", class: "button-link" %>
</td>
</tr>
<%end%>
</tbody>
</table>
<% provide(:title, 'Samples') %>
<div class="col-md-8">
<h3>Samples (<%= @user.samples.count %>)</h3>
<ol class="samples">
<%= render @samples %>
</ol>
<%= will_paginate @samples %>
<% end %>
</div>
</div>
<li id="sample-<%= sample.id %>">
<%= link_to gravatar_for(sample.user, size: 50), sample.user %>
<span class="user"><%= link_to sample.user.name, sample.user %></span><br>
<span class="name"><%= sample.name %></span><br>
<span class="formula"><%= sample.formula %></span>,
<span class="status"><%= sample.status %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(sample.created_at) %> ago.
<% if current_user?(sample.user) %>
<%= link_to "delete", sample, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
<div class="col-md-8">
<% if @user.samples.any? %>
<h3>Samples (<%= @user.samples.count %>)</h3>
<ol class="samples">
<%= render @samples %>
</ol>
<%= will_paginate @samples %>
<% end %>
</div>
</div>
<table class="table table-bordered table-hover table-nonfluid" id="tab_logic" style="margin-top:50px">
<thead>
<tr >
<th class="text-center">
ID
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
Formula
</th>
<th class="text-center">
Status
</th>
<th class="text-center">
Action
</th>
</tr>
</thead>
<tbody>
<%[@sample].each do |sample|%>
<tr id='addr0'>
<td>
<%= @sample.id%>
</td>
<td>
<span class="name"><%= sample.name %></span>
</td>
<td>
<span class="formula"><%=add_subscript(sample.formula) %></span>
</td>
<td>
<span class="status"><%= sample.status %></span>
</td>
<td>
<%= link_to "edit", edit_sample_path%>
<% if current_user?(sample.user) && sample.status == "In preparation" %>
<%= link_to "delete", sample, method: :delete,
data: { confirm: "You sure?" } %>
<%end%>
</td>
</tr>
<%end%>
</tbody>
</table>
<% if @sample.status == "in preparation" %>
<b>Change status to:</b> <%=link_to (@sample.status ? "submitted for measurement" : "No"),
Measured_sample_path(@sample), :action => 'solve', :class => 'btn btn-default' %>
<%end%>
<script type="text/javascript">
$(document).ready(function(){
$('#myTable td.y_n').each(function(){
if ($(this).text() < 0.4) {
$(this).css('background-color','#56bf5b');
}
else {
$(this).css('background-color','#ff0000');
}
});
});
</script>
<table class="table table-bordered table-hover table-nonfluid" id = "myTable" style="margin-top:50px">
<thead>
<tr>
<th>ELEMENTS:</th>
<% (["C","N","H","S"]).each do |k,v| %>
<th><%= k %></th>
<%end if @sample.calculation%>
<th>Rest</TH>
<th>Total</th>
</tr>
</thead>
<tbody >
<tr>
<td>Expected Elemental Analysis:</td>
<%sum=0
i=0
%>
<% @molecule.formatted_calculation.each_pair do |k,v| %>
<%if i<4 %>
<td> <%= "%.2f" %v %>
<% sum=sum.to_f+v.to_f %>
</td>
<%i=i+1%>
<%else
break%>
<%end if%>
<%end if @sample.calculation %>
<% s = 100 - sum %>
<td><%=s.abs.round(2)%></td>
<% sum = sum + s%>
<td><%= "%.2f" %sum %></td></tr>
<tr>
<td> Difference (%) </td>
<%d=0
c=0
b=0
a=0
e=0%>
<td class="y_n"><%=a.abs.round(2)%></td>
<td class="y_n"><%=b.abs.round(2)%></td>
<td class="y_n"><%=c.abs.round(2)%></td>
<td class="y_n"><%=d.abs.round(2)%> </td>
<td class="y_n"> <%=e.abs.round(2) %></td>
<%#end if @molecule.calculation %>
</tr>
</tbody>
</table>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment