Commit 727a9845 authored by Sara Kuqi's avatar Sara Kuqi
Browse files

results page

parent 5ed88515
......@@ -9,7 +9,9 @@ gem 'bcrypt','~>3.1.11'
gem 'will_paginate', '~> 3.1.0'
gem 'bootstrap-will_paginate', '~> 0.0.10'
gem 'unicorn'# gem 'therubyracer', platforms: :ruby
gem "cocoon"
gem "json"
#bootstrap
gem 'bootstrap-sass', '~>3.3.6'
# Use sqlite3 as the database for Active Record
......
......@@ -49,6 +49,7 @@ GEM
will_paginate
builder (3.2.2)
byebug (9.0.5)
cocoon (1.2.9)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.2.x)
......@@ -71,6 +72,8 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.0.2)
kgio (2.10.0)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
......@@ -118,6 +121,7 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.17.0)
rake (11.2.2)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
......@@ -150,6 +154,9 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.0)
execjs (>= 0.3.0, < 3)
unicorn (5.1.0)
kgio (~> 2.6)
raindrops (~> 0.7)
web-console (3.3.1)
actionview (>= 5.0)
activemodel (>= 5.0)
......@@ -168,9 +175,11 @@ DEPENDENCIES
bootstrap-sass (~> 3.3.6)
bootstrap-will_paginate (~> 0.0.10)
byebug
cocoon
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails
json
listen (~> 3.0.5)
pg
puma (~> 3.0)
......@@ -181,6 +190,7 @@ DEPENDENCIES
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
unicorn
web-console
will_paginate (~> 3.1.0)
......
......@@ -15,3 +15,4 @@
//= require bootstrap
//= require turbolinks
//= require_tree .
//= require cocoon
......@@ -2,13 +2,13 @@
@import "bootstrap";
/* mixins, variables, etc. */
@mixin box_sizing {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
// @mixin box_sizing {
// -moz-box-sizing: border-box;
// -webkit-box-sizing: border-box;
// box-sizing: border-box;
// }
/* universal */
/* universal*/
h1 {
font-size: 4em;
letter-spacing: -3px;
......@@ -74,13 +74,13 @@ textarea {
}
/* miscellaneous */
.debug_dump {
clear: both;
float: left;
width: 100%;
margin-top: 45px;
@include box_sizing;
}
// .debug_dump {
// clear: both;
// float: left;
// width: 100%;
// margin-top: 45px;
// @include box_sizing;
// }
/* sidebar */
aside {
......@@ -123,7 +123,7 @@ input, textarea, select, .uneditable-input {
border: 1px solid #bbb;
width: 100%;
margin-bottom: 10px;
@include box_sizing;
// @include box_sizing;
}
input {
......@@ -212,6 +212,10 @@ span.picture {
.table-nonfluid {
width: auto !important;
}
.table-hover tbody tr:hover td, .table-hover tbody tr:hover th {
background-color: #f2f2f2;
}
input.transparent-input{
background-color:transparent !important;
......
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
def new
@result = Result.new
end
def index
@results = Result.all
end
def show
end
def edit
end
def create
@result = Result.new(result_params)
respond_to do |format|
if @result.save
format.html { redirect_to @result, notice: 'Result was successfully created.' }
format.json { render :show, status: :created, location: @result }
else
format.html { render :new }
format.json { render json: @result.errors, status: :unprocessable_entity }
end
end
end
def update
@result = Result.find(result_params[:result_id])
@result.row_order_position = result_params[:row_order_position]
respond_to do |format|
if @result.update(result_params)
format.html { redirect_to @result, notice: 'Result was successfully updated.' }
format.json { render :show, status: :ok, location: @result }
else
format.html { render :edit }
format.json { render json: @result.errors, status: :unprocessable_entity }
end
end
end
def destroy
@result.destroy
respond_to do |format|
format.html { redirect_to results_url, notice: 'Result was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_result
@result = Result.find(params[:id])
end
def result_params
params.require(:result).permit(:sample_id, :measurement, :name)
params.fetch(:result, {})
end
end
class SamplesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :set_sample, only: [:show, :edit, :update, :destroy,:update_status]
def new
@sample=Sample.new
end
def create
@sample = current_user.samples.build(sample_params.merge(status:"in preparation"))
@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'
flash[:danger] = @sample.errors.full_messages.to_sentence
@samples = current_user.samples[0..-2]
end
redirect_to root_url
end
def index
@samples = Sample.all
@sample=Sample.new
@title = "Listing Samples"
status = params.delete :status
case status
when "submitted"
if operator?
@samples = Sample.where('status IN (?)' , ["submitted"])
end
when "measured"
if operator?
@samples = Sample.where('status IN (?)', ["measured"])
end
else
if operator?
@samples = current_user.samples.where('status IN (?)' , ["in preparation"])
end
#@samples = Sample.all
#if operator?
#@samples = Sample.where('status <> ? OR user_id = ?','in preparation',current_user.id).paginate(page: params[:page])
#end
end
end
def edit
@sample = Sample.find(params[:id])
#@sample = Sample.find(params[:id])
@sample = params[:id] && Sample.find(params[:id]) || nil
redirect_to(root_url) unless operator? || @user == current_user || @sample.status=="in preparation"
if @sample.status == "measured"
redirect_to(root_url)
end
end
def update
......@@ -36,7 +64,11 @@ class SamplesController < ApplicationController
end
def show
@sample = current_user.samples.find(params[:id])#.paginate(page: params[:page])
if operator?
@sample = Sample.find(params[:id])
else
@sample = current_user.samples.find(params[:id])
end
end
def destroy
......@@ -54,14 +86,29 @@ class SamplesController < ApplicationController
redirect_to sample_path(sample)
end
def ReleaseMeasurment
sample = Sample.find(params[:id])
if sample.status == "submitted"
sample.status = "measured"
sample.save
end
redirect_to sample_path(sample)
end
private
def sample_params
params.require(:sample).permit(:name, :formula)
keys=Sample::ELEMENTS.keys
params.require(:sample).permit(:name, :formula, results_attributes: [:id, :_destroy, :element_C,:element_H,:element_N,:element_O])
end
def correct_user
@sample = current_user.samples.find_by(id: params[:id])
redirect_to root_url if @sample.nil?
end
def set_sample
@sample = Sample.find(params[:id])
@result = @sample.results
end
end
class StaticPagesController < ApplicationController
def home
if logged_in?
if (logged_in? && !operator? )
@samples = current_user.samples[0..-1]
@sample = current_user.samples.build
@feed_items = current_user.feed.paginate(page: params[:page],per_page: 2 )
@sample = current_user.samples.build
elsif (logged_in? && operator?)
@samples = Sample.where('status <> ? OR user_id = ?','in preparation',current_user.id)
@sample = current_user.samples.build
end
end
def help
......
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
before_action :correct_user, only: [:index, :show, :edit, :update]
before_action :set_type
def index
@users = type_class.all
@users = User.paginate(page: params[:page], per_page: 4)
if operator?
@users = type_class.all
@users = User.paginate(page: params[:page], per_page: 4)
end
end
def show
@sample=Sample.new
@user = User.find(params[:id])
@samples = @user.samples.paginate(page: params[:page])
if operator?
@samples = @user.samples.where('status <> ? OR user_id = ?','in preparation',current_user.id).paginate(page: params[:page])
else
@samples = @user.samples.paginate(page: params[:page])
end
end
......@@ -43,7 +51,6 @@ class UsersController < ApplicationController
end
end
private
def user_params
......@@ -52,8 +59,8 @@ class UsersController < ApplicationController
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
@user = params[:id] && User.find(params[:id]) || nil
redirect_to(root_url) unless operator? || @user == current_user
end
def logged_in_user
......@@ -68,12 +75,13 @@ class UsersController < ApplicationController
@type = type
end
def type
User.types.include?(params[:type])? params[:type] : "User"
end
def type
User.types.include?(params[:type])? params[:type] : "User"
end
def type_class
type.constantize
end
end
......@@ -38,6 +38,11 @@ module SessionsHelper
def logged_in?
!current_user.nil?
end
def operator?
current_user.type == "Operator"
end
def forget(user)
user.forget
cookies.delete(:user_id)
......
class Result < ApplicationRecord
belongs_to :sample
before_save :measurement
def measurement
measurement = Hash.new{}
end
end
......@@ -2,25 +2,38 @@ class Sample < ApplicationRecord
attr_reader :formul
attr_accessor :mw
has_many :results
accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true
belongs_to :user
default_scope -> { order(created_at: :desc) }
default_scope { order(updated_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 }
before_save :calculate_percentage
end
def parse_formula
include MolApp::PeriodicTable
PREDEF_ELEMENTS = %w(C N H S)
REG = /(#{ELEMENTS.keys.map(&:to_s).sort.reverse.join("|")})(\d*)/
def formatted_calculation
self.calculation
keys = ( PREDEF_ELEMENTS+(self.calculation.keys - PREDEF_ELEMENTS) ).uniq
result = {}
keys.each do |key|
result[key] = self.calculation[key] || 0
end
result
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
return false unless $1
left_over = $'
if $2 == ""
then value = 1
......@@ -30,23 +43,26 @@ def parse_formula
@formul[$1] = value
end
@formul
end
end
def calculate_mw
@mw=0
@formul.each_pair do |key, value|
def calculate_mw
@mw=0
@formul.each_pair do |key, value|
puts "#{key} is #{value}"
@mw += get_atomic_weight(key)*value
@mw += get_atomic_weight(key)*value
end
@mw
@mw
end
def calculate_percentage
@expected_result={}
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)
unless parse_formula
errors.add('Warning','Formula is not valid')
throw(:abort)
end
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
end
<footer class="container-fluid text-center">
<p>Copyright © 2008 Design Shack</p>
<p>Copyright © 2016 Design Shack</p>
</footer>
......@@ -4,17 +4,19 @@
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"> <%= link_to "Home",root_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Contact", contact_path %></li>
<li class="active"> <%= link_to "Home",root_path,{:class => "glyphicon glyphicon-home"}%></li>
<li><%= link_to "About", about_path, {:class => "glyphicon glyphicon-info-sign"} %></li>
<li><%= link_to "Help", help_path,{:class => "glyphicon glyphicon-search"} %></li>
<li><%= link_to "Contact", contact_path,{:class => "glyphicon glyphicon-user"} %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<% if logged_in? %>
<% if logged_in? && operator?%>
<li><%= link_to "Users", users_path %></li>
<%end%>
<%if logged_in? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
<%= current_user.name %> <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
......@@ -26,7 +28,7 @@
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<li><%= link_to "Login", login_path ,{:class => "glyphicon glyphicon-log-in glyphicon-bold"}%></li>
<% end %>
</ul>
</nav>
......
<!DOCTYPE html>
<html>
<head>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track': 'reload' %>
......@@ -10,15 +11,15 @@
</script>
<![endif]-->
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
</div>
<div class="row content", style=" margin: 50px 80px 50px 80px;">
<div class="col-sm-8 text-left">
<div class="container"></div>
......@@ -28,13 +29,12 @@
<%= yield %>
</div>
<div class="col-sm-2 sidenav">
</div>
</div>
</div>
</div>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
<%#= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
<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','#ccf3ff');
}
else if ($(this).text() =="submitted") {
$(this).parents(".selekt").css('background-color','#e6ffe6')
}
else {
$(this).parents(".selekt").css('background-color','#ffe5cc')
}
});
});
</script>
<% if (current_page?('/samples, /index') || current_page?('/'))%>
<%=render 'shared/sample_form'%><br>
<%end%>
<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" style="margin-top:30px">
<thead>
<tr >
<th class="text-center">
ID
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
Formula
</th>
<th class="text-center">