registrar el número de serie de las raspberries
This commit is contained in:
parent
ae520e9076
commit
d4e35dbb5e
3 changed files with 35 additions and 6 deletions
|
@ -42,10 +42,21 @@ class ReadingsController < ActionController::API
|
|||
head :bad_request
|
||||
end
|
||||
|
||||
# Registra una Raspberry junto con su llave pública
|
||||
# Registra una Raspberry junto con su llave pública.
|
||||
#
|
||||
# Como el número de serie se agregó después, queremos migrar ad-hoc
|
||||
# las Raspberries que fueron registradas por su nombre y asociarles el
|
||||
# número de serie apenas empiecen a actualizarse los nodemecu.
|
||||
def raspberry
|
||||
@raspberry ||= Raspberry.find_or_create_by!(name: params[:controller_id]).tap do |r|
|
||||
@raspberry ||= begin
|
||||
r = Raspberry.find_by(serial_number: params[:serial_number])
|
||||
r ||= Raspberry.find_by(name: params[:controller_id])
|
||||
r.tap do |r|
|
||||
r.name = params[:controller_id]
|
||||
r.serial_number = params[:serial_number]
|
||||
r.public_keys.find_or_create_by(content: request.headers[:'X-Public-Key'])
|
||||
r.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -5,6 +5,6 @@ class Raspberry < ApplicationRecord
|
|||
has_many :arduinos
|
||||
has_many :public_keys
|
||||
|
||||
validates_presence_of :name
|
||||
validates_uniqueness_of :name
|
||||
validates_presence_of :name, :serial_number
|
||||
validates_uniqueness_of :serial_number
|
||||
end
|
||||
|
|
18
db/migrate/20220314201955_add_serial_number_to_raspberry.rb
Normal file
18
db/migrate/20220314201955_add_serial_number_to_raspberry.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# frozen_string_literal: tru
|
||||
|
||||
# Agrega el número de serie de cada RPi
|
||||
class AddSerialNumberToRaspberry < ActiveRecord::Migration[6.1]
|
||||
def up
|
||||
add_column :raspberries, :serial_number, :string
|
||||
|
||||
remove_index :raspberries, :name
|
||||
add_index :raspberries, :serial_number, unique: true
|
||||
end
|
||||
|
||||
def down
|
||||
remove_index :raspberries, :serial_number
|
||||
add_index :raspberries, :name, unique: true
|
||||
|
||||
remove_column :raspberries, :serial_number
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue