Rename Enigma => Alert

pull/1165/head
Rick Carlino 2019-04-19 10:56:45 -07:00
parent 06a98ea6e0
commit 9ea9fcb95e
21 changed files with 152 additions and 136 deletions

View File

@ -0,0 +1,17 @@
module Api
class AlertsController < Api::AbstractController
def index
render json: current_device.alerts
end
def destroy
mutate Alerts::Destroy.run(alert: alert)
end
private
def alert
@alert ||= current_device.alerts.find(params[:id])
end
end
end

View File

@ -1,17 +0,0 @@
module Api
class EnigmasController < Api::AbstractController
def index
render json: current_device.enigmas
end
def destroy
mutate Enigmas::Destroy.run(enigma: enigma)
end
private
def enigma
@enigma ||= current_device.enigmas.find(params[:id])
end
end
end

View File

@ -4,7 +4,7 @@ module Api
skip_before_action :check_fbos_version
def show
render json: GlobalBulletinSerializer.new(search_results).as_json
render json: search_results
end
private

View File

@ -1,4 +1,4 @@
class Enigma < ApplicationRecord
class Alert < ApplicationRecord
belongs_to :device
PROBLEM_TAGS = [
SEED_DATA = "api.seed_data.missing",

View File

@ -12,7 +12,7 @@ class Device < ApplicationRecord
"Resuming log storage."
CACHE_KEY = "devices.%s"
has_many :enigmas, dependent: :destroy
has_many :alerts, dependent: :destroy
has_many :farmware_envs, dependent: :destroy
has_many :farm_events, dependent: :destroy
has_many :farmware_installations, dependent: :destroy

View File

@ -1,9 +1,5 @@
class GlobalBulletin < ApplicationRecord
class GlobalBulletin < ActiveRecord::Base
self.inheritance_column = "none"
validates_uniqueness_of :slug
validates_presence_of :content, :href, :slug, :type
def maybe_broadcast
# Opt out of auto sync
end
end

View File

@ -1,14 +1,14 @@
module Enigmas
module Alerts
class Create < Mutations::Command
required do
model :device
string :problem_tag, in: Enigma::PROBLEM_TAGS
string :problem_tag, in: Alert::PROBLEM_TAGS
end
def execute
Enigma.create!(device: device,
Alert.create!(device: device,
problem_tag: problem_tag,
uuid: SecureRandom.uuid)
slug: SecureRandom.uuid)
end
end
end

View File

@ -1,11 +1,11 @@
module Enigmas
module Alerts
class Destroy < Mutations::Command
required do
model :enigma
model :alert
end
def execute
enigma.destroy!
alert.destroy!
end
end
end

View File

@ -13,14 +13,14 @@ module Devices
def execute
merge_default_values
device = Device.create!({name: "Farmbot"}.merge(inputs.except(:user)))
Enigmas::Create.run!(device: device,
problem_tag: Enigma::SEED_DATA)
Enigmas::Create.run!(device: device,
problem_tag: Enigma::TOUR)
Enigmas::Create.run!(device: device,
problem_tag: Enigma::USER)
Enigmas::Create.run!(device: device,
problem_tag: Enigma::DOCUMENTATION)
Alerts::Create.run!(device: device,
problem_tag: Alert::SEED_DATA)
Alerts::Create.run!(device: device,
problem_tag: Alert::TOUR)
Alerts::Create.run!(device: device,
problem_tag: Alert::USER)
Alerts::Create.run!(device: device,
problem_tag: Alert::DOCUMENTATION)
ActiveRecord::Base.transaction do
# TODO: This is a really, really, really old

View File

@ -1,5 +1,5 @@
class EnigmaSerializer < ApplicationSerializer
attributes :created_at, :id, :priority, :problem_tag, :updated_at, :uuid
class AlertSerializer < ApplicationSerializer
attributes :created_at, :id, :priority, :problem_tag, :updated_at, :slug
def created_at
object.created_at.to_i

View File

@ -1,3 +1,3 @@
class GlobalBulletinSerializer < BasePointSerializer
attributes :content, :href, :slug, :type
class GlobalBulletinSerializer < ApplicationSerializer
attributes :href, :slug, :title, :type, :content
end

View File

@ -8,7 +8,7 @@ FarmBot::Application.routes.draw do
# Standard API Resources:
{
diagnostic_dumps: [:create, :destroy, :index],
enigmas: [:create, :destroy, :index],
alerts: [:create, :destroy, :index],
farm_events: [:create, :destroy, :index, :show, :update],
farmware_envs: [:create, :destroy, :index, :show, :update],
global_bulletins: [:show],

View File

@ -1,11 +1,11 @@
class CreateGlobalBulletin < ActiveRecord::Migration[5.2]
def change
create_table :global_bulletins do |t|
t.text :content
t.string :href
t.string :slug
t.string :title
t.string :type
t.string :name
t.text :content
t.timestamps
end
end

View File

@ -0,0 +1,5 @@
class DropEnigmasTable < ActiveRecord::Migration[5.2]
def change
drop_table :enigmas
end
end

View File

@ -0,0 +1,11 @@
class CreateAlertsTable < ActiveRecord::Migration[5.2]
def change
create_table :alerts do |t|
t.string :problem_tag, null: false
t.integer :priority, default: 100, null: false
t.string :slug, null: false
t.references :device, foreign_key: true, null: false
t.timestamps
end
end
end

View File

@ -42,6 +42,40 @@ SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: alerts; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.alerts (
id bigint NOT NULL,
problem_tag character varying NOT NULL,
priority integer DEFAULT 100 NOT NULL,
slug character varying NOT NULL,
device_id bigint NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
--
-- Name: alerts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.alerts_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: alerts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.alerts_id_seq OWNED BY public.alerts.id;
--
-- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: -
--
@ -272,40 +306,6 @@ CREATE SEQUENCE public.edge_nodes_id_seq
ALTER SEQUENCE public.edge_nodes_id_seq OWNED BY public.edge_nodes.id;
--
-- Name: enigmas; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.enigmas (
id bigint NOT NULL,
problem_tag character varying NOT NULL,
priority integer DEFAULT 100 NOT NULL,
uuid character varying NOT NULL,
device_id bigint NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
--
-- Name: enigmas_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.enigmas_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: enigmas_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.enigmas_id_seq OWNED BY public.enigmas.id;
--
-- Name: farm_events; Type: TABLE; Schema: public; Owner: -
--
@ -624,11 +624,11 @@ ALTER SEQUENCE public.fragments_id_seq OWNED BY public.fragments.id;
CREATE TABLE public.global_bulletins (
id bigint NOT NULL,
content text,
href character varying,
slug character varying,
title character varying,
type character varying,
name character varying,
content text,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
@ -1603,6 +1603,13 @@ CREATE SEQUENCE public.webcam_feeds_id_seq
ALTER SEQUENCE public.webcam_feeds_id_seq OWNED BY public.webcam_feeds.id;
--
-- Name: alerts id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.alerts ALTER COLUMN id SET DEFAULT nextval('public.alerts_id_seq'::regclass);
--
-- Name: arg_names id; Type: DEFAULT; Schema: public; Owner: -
--
@ -1645,13 +1652,6 @@ ALTER TABLE ONLY public.diagnostic_dumps ALTER COLUMN id SET DEFAULT nextval('pu
ALTER TABLE ONLY public.edge_nodes ALTER COLUMN id SET DEFAULT nextval('public.edge_nodes_id_seq'::regclass);
--
-- Name: enigmas id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.enigmas ALTER COLUMN id SET DEFAULT nextval('public.enigmas_id_seq'::regclass);
--
-- Name: farm_events id; Type: DEFAULT; Schema: public; Owner: -
--
@ -1869,6 +1869,14 @@ ALTER TABLE ONLY public.web_app_configs ALTER COLUMN id SET DEFAULT nextval('pub
ALTER TABLE ONLY public.webcam_feeds ALTER COLUMN id SET DEFAULT nextval('public.webcam_feeds_id_seq'::regclass);
--
-- Name: alerts alerts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.alerts
ADD CONSTRAINT alerts_pkey PRIMARY KEY (id);
--
-- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -1925,14 +1933,6 @@ ALTER TABLE ONLY public.edge_nodes
ADD CONSTRAINT edge_nodes_pkey PRIMARY KEY (id);
--
-- Name: enigmas enigmas_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.enigmas
ADD CONSTRAINT enigmas_pkey PRIMARY KEY (id);
--
-- Name: farm_events farm_events_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -2196,6 +2196,13 @@ ALTER TABLE ONLY public.webcam_feeds
CREATE INDEX delayed_jobs_priority ON public.delayed_jobs USING btree (priority, run_at);
--
-- Name: index_alerts_on_device_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_alerts_on_device_id ON public.alerts USING btree (device_id);
--
-- Name: index_arg_sets_on_fragment_id; Type: INDEX; Schema: public; Owner: -
--
@ -2252,13 +2259,6 @@ CREATE INDEX index_edge_nodes_on_primary_node_id ON public.edge_nodes USING btre
CREATE INDEX index_edge_nodes_on_sequence_id ON public.edge_nodes USING btree (sequence_id);
--
-- Name: index_enigmas_on_device_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_enigmas_on_device_id ON public.enigmas USING btree (device_id);
--
-- Name: index_farm_events_on_device_id; Type: INDEX; Schema: public; Owner: -
--
@ -2722,14 +2722,6 @@ ALTER TABLE ONLY public.sensor_readings
ADD CONSTRAINT fk_rails_04297fb1ff FOREIGN KEY (device_id) REFERENCES public.devices(id);
--
-- Name: enigmas fk_rails_10ebd17bff; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.enigmas
ADD CONSTRAINT fk_rails_10ebd17bff FOREIGN KEY (device_id) REFERENCES public.devices(id);
--
-- Name: pin_bindings fk_rails_1f1c3b6979; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -2770,6 +2762,14 @@ ALTER TABLE ONLY public.farmware_envs
ADD CONSTRAINT fk_rails_bdadc396eb FOREIGN KEY (device_id) REFERENCES public.devices(id);
--
-- Name: alerts fk_rails_c0132c78be; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.alerts
ADD CONSTRAINT fk_rails_c0132c78be FOREIGN KEY (device_id) REFERENCES public.devices(id);
--
-- Name: diagnostic_dumps fk_rails_c5df7fdc83; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -2954,6 +2954,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190416035406'),
('20190417165636'),
('20190419001321'),
('20190419052844');
('20190419052844'),
('20190419174728'),
('20190419174811');

View File

@ -1,23 +1,23 @@
require "spec_helper"
describe Api::EnigmasController do
describe Api::AlertsController do
include Devise::Test::ControllerHelpers
let(:user) { FactoryBot.create(:user) }
it "lists all Enigmas for a user" do
it "lists all Alerts for a user" do
sign_in user
e = FactoryBot.create(:enigma, device: user.device)
e = FactoryBot.create(:alert, device: user.device)
process :index, method: :get
expect(response.status).to eq(200)
expect(json.length).to eq(1)
expect(json.first.fetch(:problem_tag)).to eq(e.problem_tag)
end
it "lists all Enigmas for a user" do
it "lists all Alerts for a user" do
sign_in user
FactoryBot.create(:enigma, device: user.device)
process :destroy, method: :get, params: { id: user.device.enigmas.last.id }
FactoryBot.create(:alert, device: user.device)
process :destroy, method: :get, params: { id: user.device.alerts.last.id }
expect(response.status).to eq(200)
expect(user.device.reload.enigmas.count).to eq(0)
expect(user.device.reload.alerts.count).to eq(0)
end
end

View File

@ -6,8 +6,10 @@ describe Api::GlobalBulletinsController do
describe "#show" do
it "shows bulletins" do
gb = FactoryBot.create(:global_bulletin)
get :show, params: { id: gb.slug }, format: :json
get :show, params: { id: gb.slug }
expect(response.status).to eq(200)
keys = [:content, :href, :slug, :type, :title]
keys.map { |k| expect(json[k]).to eq(gb[k]) }
end
end
end

View File

@ -130,12 +130,12 @@ describe Api::UsersController do
expect(user.name).to eq("Frank")
expect(user.email).to eq(email)
expect(user.valid_password?("Password123")).to be_truthy
expect(user.device.enigmas.count).to eq(4)
tags = user.device.enigmas.pluck(:problem_tag)
expect(tags).to include(Enigma::SEED_DATA)
expect(tags).to include(Enigma::TOUR)
expect(tags).to include(Enigma::USER)
expect(tags).to include(Enigma::DOCUMENTATION)
expect(user.device.alerts.count).to eq(4)
tags = user.device.alerts.pluck(:problem_tag)
expect(tags).to include(Alert::SEED_DATA)
expect(tags).to include(Alert::TOUR)
expect(tags).to include(Alert::USER)
expect(tags).to include(Alert::DOCUMENTATION)
end
end
end

View File

@ -0,0 +1,8 @@
FactoryBot.define do
factory :alert do
problem_tag { Alert::PROBLEM_TAGS.sample }
priority { 100 }
slug { SecureRandom.uuid }
device
end
end

View File

@ -1,8 +0,0 @@
FactoryBot.define do
factory :enigma do
problem_tag { Enigma::PROBLEM_TAGS.sample }
priority { 100 }
uuid { SecureRandom.uuid }
device
end
end