Skip to content

Commit

Permalink
Merge a6930f3 into 9fd5de8
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergeykot committed Nov 10, 2016
2 parents 9fd5de8 + a6930f3 commit 321e95b
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/assets/javascripts/components/models/Reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default class Reaction extends Element {
if(!currentUser) {
return 'New Reaction';
} else {
return `${currentUser.initials}-R${currentUser.reactions_count + 1}`;
let number = currentUser.reactions_count + 1;
let prefix = currentUser.reaction_name_prefix;
return `${currentUser.initials}-${prefix}${number}`;
}
}

Expand Down
Empty file removed app/assets/javascripts/pages.js
Empty file.
12 changes: 12 additions & 0 deletions app/assets/javascripts/pages.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

showExampleLabel = function() {
var prefix = $('input#reaction-name-prefix').val();
var counter = parseInt($('input#reactions-count').val());
var user_name_abbr = $('input#name_abbreviation').val();
var reaction_label = user_name_abbr + '-' + prefix + (counter + 1)
$('span#reaction-label-example').text(reaction_label);
}

$(function() {
showExampleLabel();
});
13 changes: 13 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ class PagesController < ApplicationController
def welcome
end

def update_user
@user = current_user
@user.counters['reactions'] = params[:reactions_count].to_i
@user.reaction_name_prefix = params[:reaction_name_prefix]
if @user.save
flash["success"] = "User settings is successfully saved!"
redirect_to root_path
else
flash.now["danger"] = "Not saved! Please check input fields."
render "user"
end
end

def profiles
current_user.has_profile
@profile = current_user.profile
Expand Down
11 changes: 11 additions & 0 deletions app/models/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Sample < ActiveRecord::Base
before_save :auto_set_short_label, on: :create

after_save :update_data_for_reactions
before_create :check_short_label
after_create :update_counter

def molecule_sum_formular
Expand Down Expand Up @@ -462,6 +463,16 @@ def update_svg_for_reactions
end
end

def check_short_label
return if self.parent
return if !!(self.short_label =~ /(solvent|solvents|reactant|reactants)/)

abbr = self.creator.name_abbreviation
if Sample.where(short_label: self.short_label).count > 0
self.short_label = "#{abbr}-#{self.creator.counters['samples'].to_i + 1}"
end
end

def update_counter
return if (self.short_label == 'reactants' || self.short_label == 'solvents')
self.creator.increment_counter 'samples' unless self.parent
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :initials, :samples_count, :reactions_count, :type
attributes :id, :name, :initials, :samples_count, :reactions_count, :type,
:reaction_name_prefix

def samples_count
object.counters['samples'].to_i
Expand Down
34 changes: 34 additions & 0 deletions app/views/pages/settings.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@

%br/
%br/
.jumbotron
%h2
Reaction Label
%br/
.form-horizontal
= form_for current_user, url: pages_update_user_path do |f|
.form-group
= f.label 'Counter prefix', class: 'control-label col-sm-2'
.col-sm-2
= text_field_tag :reaction_name_prefix,
current_user.reaction_name_prefix,
maxlength: 3,
class: 'form-control',
id: 'reaction-name-prefix',
oninput: 'showExampleLabel();'
.form-group
= f.label 'Counter starts at', class: 'control-label col-sm-2'
.col-sm-2
= number_field_tag :reactions_count,
current_user.counters['reactions'],
min: 0,
class: 'form-control',
id: 'reactions-count',
oninput: 'showExampleLabel();'
= hidden_field_tag :name_abbreviation, current_user.name_abbreviation

%h4
Next reaction label will be:
%span{id: 'reaction-label-example'}

%br/

.actions
= f.submit "Update user settings", class: "btn btn-primary"
- plugin_with_setting_view_list.each do |plugin|
.jumbotron
.row
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get 'pages/settings', to: 'pages#settings'
get 'pages/profiles', to: 'pages#profiles'
patch 'pages/update_profiles', to: 'pages#update_profiles'
patch 'pages/update_user', to: 'pages#update_user'
get 'pages/groups', to: 'pages#groups'
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20161109141353_add_reaction_name_prefix_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddReactionNamePrefixToUser < ActiveRecord::Migration
def change
add_column :users, :reaction_name_prefix, :string, default: 'R', limit: 3
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161024083139) do
ActiveRecord::Schema.define(version: 20161109141353) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -419,6 +419,7 @@
t.string "name_abbreviation", limit: 5
t.string "type", default: "Person"
t.boolean "is_templates_moderator", default: false, null: false
t.string "reaction_name_prefix", limit: 3, default: "R"
end

add_index "users", ["deleted_at"], name: "index_users_on_deleted_at", using: :btree
Expand Down
2 changes: 1 addition & 1 deletion spec/api/user_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Chemotion::UserAPI do
let(:json_options) {
{
only: [:id,:is_templates_moderator,:type],
only: [:id,:is_templates_moderator,:type, :reaction_name_prefix],
methods: [:name, :initials]
}
}
Expand Down

0 comments on commit 321e95b

Please sign in to comment.