Skip to content

Commit

Permalink
spectrum cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
jywarren committed Sep 29, 2015
1 parent c0745cb commit 8e67f5b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 22 deletions.
Expand Up @@ -218,6 +218,7 @@ SpectralWorkbench.Tag = Class.extend({
else if (_tag.key == "transform") return "Filters this spectrum with a math expression.";
else if (_tag.key == "subtract") return "Subtracts another spectrum from this.";
else if (_tag.key == "calibration") return "Copies calibration from another spectrum.";
else if (_tag.key == "cloneOf") return "Spectrum is a copy of <a href='/spectrums/" + _tag.value + "'>Spectrum #" + _tag.value + "</a>.";
else return "No description yet.";
}

Expand Down
5 changes: 1 addition & 4 deletions app/controllers/capture_controller.rb
Expand Up @@ -2,9 +2,6 @@
require 'will_paginate/array'

class CaptureController < ApplicationController
#protect_from_forgery :only => [:clone, :extract, :calibrate]
# http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html
# we no longer use recaptcha; start requiring auth token:
skip_before_filter :verify_authenticity_token

before_filter :require_login, :except => [:index, :recent_calibrations]
Expand Down Expand Up @@ -68,7 +65,7 @@ def save

# clone calibration? Do it based on a tag.
# But can we clone all tags, and normalization, and sample row?
@spectrum.clone(params[:spectrum][:calibration_id]) if params[:clone]
@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]
Expand Down
35 changes: 26 additions & 9 deletions app/controllers/spectrums_controller.rb
Expand Up @@ -2,9 +2,9 @@
class SpectrumsController < ApplicationController
respond_to :html, :xml, :js, :csv, :json
# expand this:
protect_from_forgery :only => [:clone, :extract, :calibrate, :save]
protect_from_forgery :only => [:clone_calibration, :extract, :calibrate, :save]
# http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html
before_filter :require_login, :only => [ :new, :edit, :create, :upload, :save, :update, :destroy, :calibrate, :extract, :clone, :setsamplerow, :find_brightest_row, :rotate, :reverse, :choose ]
before_filter :require_login, :only => [ :new, :edit, :create, :upload, :save, :update, :destroy, :calibrate, :extract, :clone_calibration, :clone, :setsamplerow, :find_brightest_row, :rotate, :reverse, :choose ]

def stats
end
Expand Down Expand Up @@ -192,7 +192,7 @@ def create
@spectrum.extract_data

if params[:spectrum][:calibration_id] && !params[:is_calibration] && params[:spectrum][:calibration_id] != "calibration" && params[:spectrum][:calibration_id] != "undefined"
@spectrum.clone(params[:spectrum][:calibration_id])
@spectrum.clone_calibration(params[:spectrum][:calibration_id])
@spectrum.tag("calibration:#{params[:spectrum][:calibration_id]}", current_user.id)
end

Expand Down Expand Up @@ -312,16 +312,32 @@ def extract
redirect_to spectrum_path(@spectrum)
end

def clone
@spectrum = Spectrum.find(params[:id])
@new = @spectrum.dup
@new.author = current_user.login
@new.user_id = current_user.id
@new.photo = @spectrum.photo
@new.save!
@new.tag("cloneOf:#{@spectrum.id}", current_user.id)
# now copy over all tags:
@spectrum.tags.each do |tag|
@new.tag(tag.name, tag.user_id) unless tag.name.split(':').first == "cloneOf"
end
flash[:notice] = "You successfully cloned <a href='#{spectrum_path(@spectrum)}'>Spectrum ##{@spectrum.id}</a>"
redirect_to spectrum_path(@new)
end

# Copy calibration from an existing calibrated spectrum.
# Start doing this client side!
def clone
def clone_calibration
@spectrum = Spectrum.find(params[:id])
@cloneSpectrum = Spectrum.find(params[:clone_id])
@calibration_clone_source = Spectrum.find(params[:clone_id])
require_ownership(@spectrum)
@spectrum.clone(@cloneSpectrum.id)
@spectrum.clone_calibration(@calibration_clone_source.id)
@spectrum.save
@spectrum.remove_powertags('calibration')
@spectrum.tag("calibration:#{@cloneSpectrum.id}", current_user.id)
@spectrum.tag("calibration:#{@calibration_clone_source.id}", current_user.id)

respond_with(@spectrums) do |format|
format.html {
Expand Down Expand Up @@ -382,7 +398,7 @@ def find_brightest_row
require_ownership(@spectrum)
@spectrum.sample_row = @spectrum.find_brightest_row
@spectrum.extract_data
@spectrum.clone(@spectrum.id) # recover calibration
@spectrum.clone_calibration(@spectrum.id) # recover calibration
@spectrum.save
flash[:warning] = "If this spectrum image is not perfectly vertical, you may need to recalibrate after <a href='//publiclab.org/wiki/spectral-workbench-calibration#Cross+section'>setting a new cross-section</a>."
redirect_to spectrum_path(@spectrum)
Expand All @@ -394,7 +410,7 @@ def rotate
require_ownership(@spectrum)
@spectrum.rotate
@spectrum.extract_data
@spectrum.clone(@spectrum.id)
@spectrum.clone_calibration(@spectrum.id)
@spectrum.save
redirect_to spectrum_path(@spectrum)
end
Expand All @@ -410,6 +426,7 @@ def reverse
redirect_to spectrum_path(@spectrum)
end

# search for calibrations to clone from
def clone_search
@spectrum = Spectrum.find(params[:id])
@calibrations = Spectrum.where(calibrated: true)
Expand Down
2 changes: 1 addition & 1 deletion app/models/spectrum.rb
Expand Up @@ -203,7 +203,7 @@ def clean_json
end

# clones calibration from another spectrum (preferably taken from the same device)
def clone(clone_id)
def clone_calibration(clone_id)
clone_source = Spectrum.find clone_id
d = ActiveSupport::JSON.decode(self.clean_json)
cd = ActiveSupport::JSON.decode(clone_source.clean_json)
Expand Down
13 changes: 8 additions & 5 deletions app/views/spectrums/_tools.html.erb
Expand Up @@ -2,9 +2,12 @@

<div class="span4 tool-menu">

<h5>Versioning</h5>
<p><a class="btn btn-small" href="/spectrums/clone/<%= @spectrum.id %>">Clone this spectrum</a> and work on a copy</p>

<h5>Data processing and display</h5>
<p><a class="btn btn-small tool-calibrate">Calibrate this spectrum</a></p>
<p><a class="btn btn-small tool-subtraction">Subtract another spectrum</a></p>
<p><a class="btn btn-small tool-calibrate">Calibrate</a> this spectrum</p>
<p><a class="btn btn-small tool-subtraction">Subtract</a> another spectrum</p>
<p><a class="btn btn-small tool-range">Set wavelength range</a></p>
<p><a class="btn btn-small tool-copy-calibration">Copy calibration</a></p>
<p><a class="btn btn-small tool-transform">Transform</a></p>
Expand All @@ -13,12 +16,12 @@

<h5>Data extraction</h5>
<p><a class="btn btn-small tool-cross-section">Set new cross-section</a></p>
<p><a class="btn btn-small" href="/spectra/reverse/<%= datum.id %>">Flip image horizontally</a></p>
<p><a class="btn btn-small" href="/spectra/find_brightest_row/<%= datum.id %>">Set new cross-section by %</a></p>
<p><a class="btn btn-small" href="/spectra/reverse/<%= datum.id %>">Flip image</a> horizontally</p>
<p><a class="btn btn-small" href="/spectra/find_brightest_row/<%= datum.id %>">Set new cross-section</a> by %</p>
<p><a class="btn btn-small disabled">Detect brightest cross-section</a></p>

<h5>Comparison &amp; Sets</h5>
<p><a class="btn btn-small tool-compare">Compare with another spectrum</a></p>
<p><a class="btn btn-small tool-compare">Compare</a> with another spectrum</p>
<p><a class="btn btn-small tool-similar">Find similar</a></p>
<p><a class="btn btn-small disabled">Add to a set</a></p>
<p><a class="btn btn-small disabled">Save as a set</a></p>
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/spectrums.yml
@@ -1,12 +1,13 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
title: MyString
title: "One nice spectrum"
author: MyString
data: '{"lines":[{"r":10,"g":10,"b":10,"average":10,"wavelength":400},{"r":10,"g":10,"b":10,"average":10,"wavelength":700}]}'
notes: MyText
notes: "A test spectrum."
user_id: 1
version: 1
video_row: 5

two:
title: MyString
Expand Down
15 changes: 14 additions & 1 deletion test/functional/spectrums_controller_test.rb
Expand Up @@ -64,6 +64,7 @@ class SpectrumsControllerTest < ActionController::TestCase

test "should calibrate spectrum" do
session[:user_id] = User.first.id # log in
# we need a real spectrum with an image to work with:
spectrum = Spectrum.last
spectrum.image_from_dataurl("data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7")
spectrum.save
Expand All @@ -75,15 +76,27 @@ class SpectrumsControllerTest < ActionController::TestCase

test "should clone spectrum calibration" do
session[:user_id] = User.first.id # log in
# we need a real spectrum with an image to work with:
spectrum = Spectrum.last
spectrum.image_from_dataurl("data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7")
spectrum.save
put :clone, :id => spectrum.id, :clone_id => spectrums(:one).id
put :clone_calibration, :id => spectrum.id, :clone_id => spectrums(:one).id
assert_response :redirect
assert_equal 'Spectrum was successfully calibrated.', flash[:notice]
assert_redirected_to spectrum_path(spectrum)
end

test "should clone spectrum" do
session[:user_id] = User.first.id # log in
# we need a real spectrum with an image to work with:
spectrum = Spectrum.last
spectrum.image_from_dataurl("data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7")
spectrum.save
get :clone, :id => spectrums(:one).id
assert_response :redirect
assert_equal Spectrum.last.tags.last.name, "cloneOf:#{spectrums(:one).id}"
end

#test "should destroy spectrum" do
# assert_difference('Spectrum.count', -1) do
# delete :destroy, :id => spectrums(:one).to_param
Expand Down

0 comments on commit 8e67f5b

Please sign in to comment.