-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathcapture_controller.rb
executable file
·148 lines (127 loc) · 5.39 KB
/
capture_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# frozen_string_literal: true
# get rid of this with better ActiveRecord 3 calls:
require 'will_paginate/array'
class CaptureController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :require_login, except: %i(index index2 recent_calibrations)
def index
@offline = 'flush'
if logged_in?
if params[:calibration_id] == 'calibration'
@calibration = nil
elsif params[:calibration_id]
cal = Spectrum.where(id: params[:calibration_id])
@calibration = cal.last unless cal.empty?
elsif current_user.calibrations.count.positive?
@calibration = current_user.last_calibration
end
@calibrations = Spectrum.where(calibrated: true, user_id: current_user.id).uniq
@calibrations << @calibration if @calibration && !@calibrations.include?(@calibration)
@start_wavelength, @end_wavelength = @calibration.wavelength_range if @calibration
end
@spectrums = Spectrum.where(limit: 12).order(id: :desc)
render template: 'capture/index', layout: 'application'
end
def index2
@offline = 'flush'
if logged_in?
if params[:calibration_id] == 'calibration'
@calibration = nil
elsif params[:calibration_id]
cal = Spectrum.where(id: params[:calibration_id])
@calibration = cal.last unless cal.empty?
elsif current_user.calibrations.count.positive?
@calibration = current_user.last_calibration
end
@calibrations = Spectrum.where(calibrated: true, user_id: current_user.id).uniq
@calibrations << @calibration if @calibration && !@calibrations.include?(@calibration)
@start_wavelength, @end_wavelength = @calibration.wavelength_range if @calibration
end
@spectrums = Spectrum.where(limit: 12).order(id: :desc)
render template: 'capture/index2', layout: 'application'
end
def offline
if logged_in?
@offline = true
@calibration = current_user.last_calibration
@calibration = Spectrum.find(params[:calibration_id]) if params[:calibration_id]
@calibrations = Spectrum.where(calibrated: true, user_id: current_user.id)
@start_wavelength, @end_wavelength = @calibration.wavelength_range if @calibration
end
@spectrums = Spectrum.all.order(id: :desc).limit(12)
render template: 'capture/index', layout: 'application'
end
# designed to replace the spectrums_controller method "create" with a
# unified, simplified version which integrates
# rotation/flip, cross section, smoothing, equalizing, and comparison, and maybe calibration?
# geocoding?
# NOT ACTIVE YET
def save
# be sure there's a "login" field here too, so users don't lose data when they're required to log in.
@spectrum = Spectrum.new(
title: params[:title],
author: current_user.login,
notes: params[:notes],
user_id: current_user.id
)
# save everything in an error block or transaction?
# get the photo itself
if params[:img]
@spectrum.image_from_dataurl(params[:img])
elsif params[:photo]
@spectrum.photo = params[:photo]
end
if @spectrum.save
if params[:data] # data supplied by client
@spectrum.data = params[:data]
else # server-side extract:
@spectrum.extract_data
@spectrum.scale_data(params[:endWavelength], params[:startWavelength]) if params[:endWavelength] && params[:startWavelength]
end
# clone calibration? Do it based on a tag.
# But can we clone all tags, and normalization, and sample row?
@spectrum.clone_calibration(params[:spectrum][:calibration_id]) if params[:clone]
@spectrum.tag("calibration:#{params[:spectrum][:calibration_id]}", current_user.id) if params[:clone]
@spectrum.tag("video_row:#{params[:video_row]}", current_user.id) if params[:video_row]
# process all metadata based on tags:
# @spectrum.tag("rotated:90",current_user.id)
# @spectrum.tag("flipped:horizontal",current_user.id)
# @spectrum.tag("normalized:area",current_user.id)
# @spectrum.tag("calibrated:reference",current_user.id)
# @spectrum.tag("cross-section:0-640x131",current_user.id) # X1-X2xY1-Y2 or X1-X2xY1
# @spectrum.tag("lat:xxx",current_user.id)
# @spectrum.tag("lon:xxx",current_user.id)
params[:tags].to_s.split(',').each do |tag|
@spectrum.tag(tag, current_user.id)
end
flash[:notice] = 'Spectrum was successfully created.'
respond_to do |format|
format.html do
redirect_to spectrum_path(@spectrum)
end
format.xml { render xml: @spectrum, status: :created, location: @spectrum }
end
else
# this isn't quite right. Also let's do ajax errors.
render 'spectrums/new-errors'
end
end
def recent_calibrations
if logged_in?
@spectrums = current_user.calibrations.limit(20)
# add the one that's being used in live display:
if params[:calibration_id] && params[:calibration_id] != 'undefined'
new_spectrum = Spectrum.where(id: params[:calibration_id])
@spectrums = new_spectrum + @spectrums
end
@spectrums = @spectrums.uniq
respond_to do |format|
format.json { render json: @spectrums.to_json(methods: %i(created_at_in_words forked)) }
end
else
respond_to do |format|
format.json { render json: { errors: 'You must be logged in to get recent calibrations.' }, status: 422 }
end
end
end
end