Skip to content

Commit 18bb5d9

Browse files
author
Chris Smith
committed
Merge branch 'staging'
2 parents 974590a + f19a72e commit 18bb5d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+890
-527
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ bower.json
4242

4343
#ignore docs
4444
/doc/app
45+
46+
.generators
47+
.idea

app/assets/javascripts/views/sub_batch.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Mixtio.Views.SubBatch extends Backbone.View
3030
render: () ->
3131
@$el.html(JST['batches/sub_batch'](
3232
sub_batch: @model
33-
quantity: @model.get("quantity")
33+
size: @model.get("quantity") ? @model.get("size")
3434
volume: @model.get("volume")
3535
selected_unit: @model.get("unit") # string representation eg "mL"
3636
units: Mixtio.Bootstrap.Units

app/assets/stylesheets/layout.scss

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ form[data-behavior="batch-form"] {
125125
border-top: 2px solid grey;
126126
}
127127

128-
.float-top-right {
129-
position: absolute;
130-
top: 0; right: 0;
131-
padding-right: 15px;
132-
margin-top: 30px;
128+
div.batch-index-buttons {
129+
text-align: right;
130+
padding-top: 20px;
133131
}
134132

135133
@media (min-width: 768px) {

app/assets/templates/batches/sub_batch.jst.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<td>
2-
<input class="form-control" type="number" value="<%= quantity == 0 ? null : quantity %>"
2+
<input class="form-control" type="number" value="<%= size == 0 ? null : size %>"
33
name="mixable[sub_batches][][quantity]" id="mixable_sub_batches__quantity" />
44
</td>
55
<td>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Batches::PrintController < ApplicationController
2+
3+
# Save the label type ID when printing so it can be shown as default choice later
4+
before_action :save_label_id, :current_resource, only: [:create]
5+
6+
# Disable editing once the batch has been printed
7+
after_action :set_editable_false, only: [:create]
8+
9+
def create
10+
print_job = PrintJob.new(print_params)
11+
12+
if print_job.execute!
13+
flash[:notice] = ["Your labels have been printed"]
14+
else
15+
flash[:error] = ["Your labels could not be printed"]
16+
print_job.errors.to_a.each do |error|
17+
flash[:error] << error
18+
end
19+
end
20+
21+
redirect_to batch_path(current_resource)
22+
end
23+
24+
private
25+
26+
def current_resource
27+
@batch ||= Batch.find(params[:id])
28+
end
29+
30+
def print_params
31+
params.permit(:label_template_id, :printer).merge(batch: current_resource)
32+
end
33+
34+
def save_label_id
35+
current_resource.consumable_type.update_column(:last_label_id, params[:label_template_id].to_i)
36+
end
37+
38+
def set_editable_false
39+
current_resource.update_column(:editable, false)
40+
end
41+
42+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Batches::SupportController < ApplicationController
2+
3+
def show
4+
@batch_id = params[:id]
5+
@support_email = Rails.configuration.support_email
6+
end
7+
8+
end

app/controllers/batches_controller.rb

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,76 @@
11
class BatchesController < ApplicationController
22

33
before_action :authenticate!, except: [:index]
4-
before_action :current_resource, only: [:show, :print, :edit]
5-
6-
# Save the label type ID when printing so it can be shown as default choice later
7-
before_action :save_label_id, only: [:print]
8-
9-
# Disable editing once the batch has been printed
10-
after_action :set_editable_false, only: [:print]
4+
before_action :current_resource, only: [:show, :update, :edit]
115

126
def index
137
@view_model = Batches::Index.new(filter_params)
148
end
159

1610
def edit
17-
@batch_form = BatchForm.new(current_resource.attributes.symbolize_keys.merge(edit_batch_params.merge(sub_batches: current_resource.sub_batches)))
18-
unless current_resource.editable
11+
@batch_form = BatchForm.new
12+
unless current_resource.editable?
1913
redirect_to batches_path
2014
flash[:error] = "This batch has already been printed, so can't be modified."
2115
end
2216
end
2317

2418
def update
25-
@batch_form = BatchForm.new(batch_params.merge(current_user: current_user))
26-
if @batch_form.update(current_resource)
27-
redirect_to batch_path, notice: "Reagent batch successfully updated!"
19+
@batch_form = BatchForm.new(params: BatchFormParameters.new(update_batch_params))
20+
@batch = @batch_form.batch
21+
22+
if @batch_form.save
23+
redirect_to batch_path(@batch), notice: "Reagent batch successfully updated!"
2824
else
2925
render :edit
3026
end
3127
end
3228

3329
def create
34-
@batch_form = BatchForm.new(batch_params.merge(current_user: current_user))
35-
if @batch_form.create
36-
redirect_to batch_path(@batch_form.batch), notice: "Reagent batch successfully created"
30+
@batch_form = BatchForm.new(params: BatchFormParameters.new(batch_params))
31+
@batch = @batch_form.batch
32+
33+
if @batch_form.save
34+
redirect_to batch_path(@batch), notice: "Reagent batch successfully created"
3735
else
3836
render :new
3937
end
4038
end
4139

4240
def new
43-
batch = Batch.new_with_sub_batch
44-
@batch_form = BatchForm.new(batch.attributes.symbolize_keys.merge(sub_batches: batch.sub_batches))
41+
@batch = Batch.new.tap { |b| b.sub_batches.build }
42+
@batch_form = BatchForm.new
4543
end
4644

4745
def show
4846
end
4947

50-
def support
51-
@batch_id = params[:id]
52-
@support_email = Rails.configuration.support_email
53-
end
54-
55-
def print
56-
print_job = PrintJob.new(print_params.merge(:batch => current_resource))
57-
58-
if print_job.execute!
59-
flash[:notice] = ["Your labels have been printed"]
60-
else
61-
flash[:error] = ["Your labels could not be printed"]
62-
print_job.errors.to_a.each do |error|
63-
flash[:error] << error
64-
end
65-
end
66-
67-
redirect_to batch_path(@batch)
68-
end
69-
7048
protected
7149

72-
def batch_params
73-
params.require(:mixable)
74-
.permit(:consumable_type_id, :consumable_name, :expiry_date, :single_barcode,
75-
mixture_criteria: [:consumable_type_id, :number, :kitchen_id, :quantity, :unit_id],
76-
sub_batches: [:quantity, :volume, :unit, :barcode_type, :project_id]
77-
)
50+
def current_resource
51+
@batch ||= Batch.find(params[:id])
7852
end
7953

80-
def edit_batch_params
81-
{ mixture_criteria: @batch.mixture_criteria }
54+
def batch_params
55+
@batch_params ||= params.require(:mixable)
56+
.permit(:consumable_type_id, :expiry_date,
57+
mixture_criteria: [:consumable_type_id, :number, :kitchen_id, :quantity, :unit_id],
58+
sub_batches: [:quantity, :volume, :unit, :barcode_type, :project_id]
59+
)
60+
.merge(user: current_user.user, action: params[:action])
61+
.tap do |batch_params|
62+
batch_params[:mixture_criteria] ||= []
63+
batch_params[:sub_batches] ||= []
64+
batch_params[:mixture_params] = batch_params[:mixture_criteria].map { |mxc| MixtureParameters.new(mxc) }
65+
end
8266
end
8367

84-
def print_params
85-
params.permit(:label_template_id, :printer)
68+
def update_batch_params
69+
batch_params.merge(batch: current_resource)
8670
end
8771

8872
def filter_params
8973
params.permit(:consumable_type_id, :created_after, :created_before, :page)
9074
end
9175

92-
def current_resource
93-
@batch = Batch.find(params[:id])
94-
end
95-
96-
def save_label_id
97-
@batch.consumable_type.update_column(:last_label_id, params[:label_template_id].to_i)
98-
end
99-
100-
def set_editable_false
101-
@batch.update_column(:editable, false)
102-
end
103-
10476
end

app/forms/batch_form.rb

Lines changed: 42 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,65 @@
1+
# Responsible for creating a new Batch, along with its SubBatches and Mixtures
12
class BatchForm
3+
include ActiveModel::Model
24

3-
extend ActiveModel::Naming
4-
include ActiveModel::Conversion
5-
include ActiveModel::Validations
6-
include MixableForm
5+
attr_accessor :params
76

8-
attr_accessor :consumable_type_id, :expiry_date, :current_user, :sub_batches
7+
validates :params, valid: true
98

10-
def initialize(attributes = {})
11-
attributes.each do |attribute, value|
12-
send("#{attribute}=", value) if respond_to?("#{attribute}=")
13-
end
14-
end
15-
16-
def persisted?
17-
false
18-
end
9+
def save
10+
begin
11+
ActiveRecord::Base.transaction do
12+
batch.assign_attributes(batch_attributes)
1913

20-
validates :consumable_type_id, :expiry_date, :current_user, presence: true
14+
return false if invalid?
2115

22-
validate do
23-
if sub_batches.nil?
24-
errors[:batch] << "must contain at least 1 sub-batch"
25-
else
26-
sub_batches.each do |sub_batch|
27-
errors["Sub-Batch"] << "aliquots can't be empty" if sub_batch[:quantity].empty?
28-
errors["Sub-Batch"] << "aliquots must be at least 1" if sub_batch[:quantity].to_i < 1 && sub_batch[:quantity].present?
16+
# Clear the SubBatches before using #build to build new ones
17+
# Can't do `batch.sub_batches = new_sub_batches` because it immediately throws an unhelpful error
18+
# if the new_sub_batches aren't valid
19+
batch.sub_batches.clear
20+
batch.sub_batches.build(params.sub_batches)
2921

30-
errors["Sub-Batch"] << "volume can't be empty" if sub_batch[:volume].empty?
31-
errors["Sub-Batch"] << "volume must be positive" if sub_batch[:volume].to_f <= 0 && sub_batch[:volume].present?
22+
# Always create new mixtures
23+
batch.mixtures = build_mixtures
3224

33-
errors["Sub-Batch"] << "project can't be empty" if sub_batch[:project_id].nil?
25+
batch.save!
26+
batch.create_audit(user: params.user, action: params.action)
3427
end
28+
true
29+
rescue => e
30+
# Put whatever the errors were on the Batch onto the BatchForm
31+
promote_errors(batch.errors)
32+
Rails.logger.error(([e.message] + e.backtrace).join("\n "))
33+
false
3534
end
36-
37-
errors[:expiry_date] << "can't be in the past" if expiry_date.present? && expiry_date.to_date < Date.today
3835
end
3936

37+
# Batch will be either the one passed in on params or a new one
38+
# @return [Batch]
4039
def batch
41-
@batch ||= Batch.new(consumable_type_id: consumable_type_id, expiry_date: expiry_date,
42-
mixtures: mixtures, kitchen: current_user.team,
43-
user: current_user.user)
40+
@batch ||= (params.batch.nil?) ? Batch.new : params.batch
4441
end
4542

46-
def create
47-
return false unless valid?
48-
49-
begin
50-
ActiveRecord::Base.transaction do
51-
batch.save!
52-
# Create the consumables for each sub-batch
53-
sub_batches.each do |sub_batch|
54-
sub_batch_record = create_sub_batch(batch, sub_batch)
55-
56-
create_consumables(sub_batch_record, sub_batch[:quantity].to_i, {sub_batch_id: sub_batch_record.id})
57-
if sub_batch[:barcode_type] == "single"
58-
generate_single_barcode(sub_batch_record.consumables)
59-
end
60-
end
43+
private
6144

62-
batch.create_audit(user: current_user, action: 'create')
63-
end
64-
rescue => e
65-
errors[:exception] << e.to_s
66-
Rails.logger.error ([e.message] + e.backtrace).join("\n ")
67-
return false
68-
end
45+
def batch_attributes
46+
{
47+
consumable_type_id: params.consumable_type_id,
48+
expiry_date: params.expiry_date,
49+
user: params.user,
50+
kitchen: params.kitchen,
51+
mixture_criteria: params.mixture_criteria
52+
}
6953
end
7054

71-
def update(batch)
72-
return false unless valid?
73-
74-
begin
75-
ActiveRecord::Base.transaction do
76-
# Delete all existing consumables for the batch
77-
batch.sub_batches.each do |old_sub_batch|
78-
old_sub_batch.consumables.destroy_all
79-
end
80-
batch.sub_batches.destroy_all
81-
82-
# Update attributes for the batch record
83-
batch.update_attributes!(consumable_type_id: consumable_type_id,
84-
expiry_date: expiry_date, mixtures: mixtures,
85-
kitchen: current_user.team, user: current_user.user)
86-
87-
# Create the consumables for each sub-batch
88-
sub_batches.each do |sub_batch|
89-
sub_batch_record = create_sub_batch(batch, sub_batch)
90-
create_consumables(sub_batch_record, sub_batch[:quantity].to_i, {sub_batch_id: sub_batch_record.id})
91-
if sub_batch[:barcode_type] == "single"
92-
generate_single_barcode(sub_batch_record.consumables)
93-
end
94-
end
95-
96-
batch.create_audit(user: current_user, action: 'update')
97-
end
98-
rescue => e
99-
errors[:exception] << e.to_s
100-
Rails.logger.error ([e.message] + e.backtrace).join("\n ")
101-
return false
102-
end
55+
def build_mixtures
56+
params.mixture_params.map { |mixture_param| Mixture.from_params(mixture_param) }
10357
end
10458

105-
private
106-
def create_sub_batch(batch, sub_batch)
107-
batch.sub_batches.create!(sub_batch.except(:barcode_type, :quantity))
108-
end
109-
110-
def create_consumables(sub_batch, quantity, attributes)
111-
sub_batch.consumables.create!(Array.new(quantity, attributes))
112-
end
113-
114-
def generate_single_barcode(sub_batch_consumables)
115-
barcode = sub_batch_consumables.first.barcode
116-
sub_batch_consumables.each do |consumable|
117-
consumable.barcode = barcode
118-
consumable.save!
119-
end
59+
def promote_errors(child_errors)
60+
child_errors.each do |attribute, message|
61+
errors.add(attribute, message)
12062
end
63+
end
12164

12265
end

0 commit comments

Comments
 (0)