samples_controller.rb 6.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'RMagick'
class SamplesController < ApplicationController
  include SamplesHelper
   before_action :authenticate_user!
   before_action :correct_user,   only: :destroy
   before_action :set_sample, only: [:show, :edit, :update, :destroy,:update_status]
   respond_to? :html, :js

   def new
     @sample=Sample.new
     @admin=false
   end

   def create
      @sample = current_user.samples.build(sample_params.merge(status:"in preparation"))
     if @sample.save
      flash[:success] = "Sample created!"
     else
       flash[:danger] = @sample.errors.full_messages.to_sentence
       @samples = current_user.samples[0..-2]
ira.maier's avatar
changes    
ira.maier committed
21
22
23
     end
      redirect_back fallback_location: root_path
   end
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

  def index
    @sample = if params[:display_sample].present?
               Sample.find_by(id: params[:display_sample].to_i)
              else
               current_user.samples.build
             end
    @title = "Listing Samples"
    status = params[:status]
    @admin = (current_user.type == 'Operator') && (@admin || params[:is_admin].present?)
    status = @sample.status if (status == nil || status == "")
    case status
    when "in preparation"
        @samples = current_user.samples.where('status IN (?)' , ["in preparation"])
    when "measured"
       @samples = current_user.samples.where('status IN (?)', ["measured"]) unless @admin
       @samples = Sample.where('status IN (?)', ["measured"]) if @admin
    when "submitted"
      @samples = current_user.samples.where('status IN (?)', ["submitted"]) unless @admin
      @samples = Sample.where('status IN (?)', ["submitted"]) if @admin
    else
      redirect_to root_url if @admin
      redirect_to root_url+'chemists/'+current_user.id.to_s unless @admin
   end
  end

  def edit
    @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"
ira.maier's avatar
changes    
ira.maier committed
55
      redirect_back fallback_location: root_path
56
57
58
59
60
    end
  end

   def update
     sample = Sample.find(params[:id])
ira.maier's avatar
changes    
ira.maier committed
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
     unless sample_params.nil?
       sample_params['results_attributes'] && sample_params['results_attributes'].each do |key, val|
         Sample::PREDEF_ELEMENTS.each do |el|
           if val["element_#{el}"]&.include? ','
             sample_params['results_attributes'][key]["element_#{el}"] = val["element_#{el}"].gsub!(',', '.')
           end
         end
       end
      if sample.update_attributes(sample_params)
        sample.touch
        flash[:success] = "Sample updated"
      else
        flash[:danger] = sample.errors.full_messages.to_sentence
      end
ira.maier's avatar
changes    
ira.maier committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

       # files processing:
     if params[:sample][:add_files]
       sample.files.attach(params[:sample][:add_files])
     end
     if params[:sample][:delete_files] == "1"
       sample.files.purge_later
     elsif params[:sample][:delete_files] == "0" and params[:sample][:delete_file]
       params[:sample][:delete_file].each{ |id|
         unless id && id.to_s.strip.empty?
           @file = ActiveStorage::Attachment.find(id)
           @file.purge_later
         end
       }
     end

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    else
      flash[:warning] = "no results"
     end

    redirect_to URI(request.referer).path + "?display_sample=#{sample.id}"
  end

  def show
    if operator?
      @sample = Sample.find(params[:id])
      @user = @sample && @sample.user
    else
      @sample = current_user.samples.find(params[:id])
      @user = @sample && @sample.user
    end
    file_type = params["format"]
    if file_type == 'pdf'
      respond_to do |format|
        format.html
        format.js
        format.pdf do
          pdf = SamplePdf.new(@sample,operator?,@user)
          send_data pdf.render, filename: "sample_#{@sample.id}.pdf",
                                type:"application/pdf",
                                disposition: "inline"
        end
      end
    end
    if file_type == 'png'
      respond_to do |format|
        format.html
        format.js
        format.png do
          pdf = SamplePdf.new(@sample,operator?,@user)
          send_image(pdf)
        end
      end
    end
  end

  def destroy
    @sample.destroy
    flash[:success] = "Sample deleted"
ira.maier's avatar
changes    
ira.maier committed
135
    redirect_back fallback_location: root_path
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  end

  def Measured
    sample = Sample.find(params[:id])
    if sample.status == "in preparation"
      sample.status = "submitted"
      sample.submitted_at = Time.new
      sample.save
    end
     redirect_to URI(request.referer).path + "?display_sample=#{sample.id}"
  end

  def Withdraw
    sample = Sample.find(params[:id])
    if sample.status == "submitted"
      sample.status = "in preparation"
      sample.submitted_at = nil
      sample.save
    end
     redirect_to URI(request.referer).path + "?display_sample=#{sample.id}"
  end

  def ReleaseMeasurment
    sample = Sample.find(params[:id])
    @user = sample && sample.user
    if sample.status == "submitted"
       sample.status = "measured"
       sample.measured_at = Time.new
       sample.operator_name = current_user.name
       sample.save
    end
      UserMailer.delay.measured_email(@user,sample)
     redirect_to URI(request.referer).path + "?display_sample=#{sample.id}"
     flash[:success]= "Sending e-mail to user"
  end

  def send_image(pdf)
    pdf_file_name = "sample_#{@sample.id}.pdf"
    png_file_name = "sample_#{@sample.id}.png"
    result = pdf_to_png(pdf, pdf_file_name, png_file_name)
    send_data result.to_blob, filename: png_file_name, type: "image/png" , disposition: "inline"
  end

ira.maier's avatar
changes    
ira.maier committed
179
180
181
182
183
  # new
  def files
    @sample = Sample.find(params[:id])
    render  partial: 'samples/index_files'
  end
184

ira.maier's avatar
changes    
ira.maier committed
185
  private
186
187
  def sample_params
    keys=Sample::ELEMENTS.keys
ira.maier's avatar
changes    
ira.maier committed
188
    params.require(:sample).permit(:name, :formula, :comment_by_operator, :comment_by_user, results_attributes: [:id, :_destroy, :element_N, :element_C,:element_H, :element_S], files: []) if params[:sample].present?
189
190
191
192
193
194
195
  end

  def correct_user
     @sample = current_user.samples.find_by(id: params[:id])
     redirect_to root_url if @sample.nil?
   end

ira.maier's avatar
changes    
ira.maier committed
196
197
198
  def set_sample
    @sample = Sample.find(params[:id])
    @result = @sample.results
199
200
  end
end