From 38f51a5de6f1e93e714ec2e3fd24834d34d36967 Mon Sep 17 00:00:00 2001 From: Torsten Date: Fri, 13 Jan 2023 21:47:31 +0200 Subject: [PATCH] basic entity get/show --- Gemfile | 3 -- app/controllers/entities_controller.rb | 5 ++- app/models/entity.rb | 26 ++++++++++++--- app/models/member.rb | 2 ++ app/views/entities/_form.html.haml | 15 ++++----- app/views/entities/index.html.haml | 4 ++- app/views/entities/new.html.haml | 4 ++- app/views/entities/show.html.haml | 35 ++++++++++++-------- db/migrate/20230113170732_create_entities.rb | 5 +-- db/schema.rb | 5 +-- test/controllers/entities_controller_test.rb | 4 +-- test/fixtures/entities.yml | 2 ++ test/system/entities_test.rb | 2 ++ 13 files changed, 74 insertions(+), 38 deletions(-) diff --git a/Gemfile b/Gemfile index 1c5cf3e..0d9c045 100644 --- a/Gemfile +++ b/Gemfile @@ -19,9 +19,6 @@ gem 'carrierwave', '>= 3.0.0.beta', '< 4.0' gem "ruby2js" , git: "https://github.com/ruby2js/ruby2js/" , branch: "haml_fix" gem 'thredded', '~> 1.0' gem "rest-client" -#token = Rails.application.credentials[:ha_token] -#RestClient.get( "10.30.39.238:8123/api/states/sensor.sonoff_pow_2a_209_power" , {"Authorizat -#ion" => "Bearer #{token}" }) gem "simple_form" , "5.1.0" diff --git a/app/controllers/entities_controller.rb b/app/controllers/entities_controller.rb index 2744797..f999cf7 100644 --- a/app/controllers/entities_controller.rb +++ b/app/controllers/entities_controller.rb @@ -1,5 +1,8 @@ class EntitiesController < ApplicationController before_action :set_entity, only: %i[ show edit update destroy ] + before_action :authenticate_member! + +# redirect_to main_app.root_path unless current_member.admin? # GET /entities def index @@ -53,6 +56,6 @@ class EntitiesController < ApplicationController # Only allow a list of trusted parameters through. def entity_params - params.require(:entity).permit(:name, :ha_id, :type , :member_id) + params.require(:entity).permit(:name, :ha_id, :ha_type, :member_id) end end diff --git a/app/models/entity.rb b/app/models/entity.rb index 850860b..8452186 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -1,7 +1,25 @@ class Entity < ApplicationRecord - validate :name , presence: true - validate :ha_id , presence: true - validate :type , presence: true - validate :member_id , presence: true + belongs_to :member + validates :name , presence: true + validates :ha_id , presence: true + validates :ha_type , presence: true + validates :member_id , presence: true + + def value + @data = Entity.response("/" + ha_id) + @data["state"] + end + + def self.ha_entities + res = response() + res.collect{|i| i["entity_id"]}.select{|i| i.include?("4a") || i.include?("2d")} + end + + def self.response(path = "") + token = Rails.application.credentials[:ha_token] + all = RestClient.get( "10.30.39.238:8123/api/states#{path}" , + {"Authorization" => "Bearer #{token}" }) + JSON.parse(all.body) + end end diff --git a/app/models/member.rb b/app/models/member.rb index 84302b5..53c5e95 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -6,6 +6,8 @@ class Member < ApplicationRecord mount_uploader :picture, PictureUploader + has_many :entity + def admin true end diff --git a/app/views/entities/_form.html.haml b/app/views/entities/_form.html.haml index 6a87421..3b20b00 100644 --- a/app/views/entities/_form.html.haml +++ b/app/views/entities/_form.html.haml @@ -1,4 +1,4 @@ -= form_for @entity do |f| += simple_form_for @entity do |f| - if @entity.errors.any? #error_explanation %h2= "#{pluralize(@entity.errors.count, "error")} prohibited this entity from being saved:" @@ -7,13 +7,12 @@ %li= message .field - = f.label :name - = f.text_field :name + = f.input :name .field - = f.label :ha_id - = f.text_field :ha_id + = f.input :ha_id , collection: Entity.ha_entities .field - = f.label :type - = f.text_field :type - .actions + = f.input :ha_type , collection: ["text" , "bool"] + .field + = f.input :member_id , collection: Member.all.collect{|m| [m.name , m.id ]} + .actions{class: button_classes} = f.submit 'Save' diff --git a/app/views/entities/index.html.haml b/app/views/entities/index.html.haml index 8d49b34..44242db 100644 --- a/app/views/entities/index.html.haml +++ b/app/views/entities/index.html.haml @@ -6,6 +6,7 @@ %th Name %th Ha %th Type + %th Member %th %th %th @@ -15,7 +16,8 @@ %tr %td= entity.name %td= entity.ha_id - %td= entity.type + %td= entity.ha_type + %td= entity.member %td= link_to 'Show', entity %td= link_to 'Edit', edit_entity_path(entity) %td= link_to 'Destroy', entity, method: :delete, data: { confirm: 'Are you sure?' } diff --git a/app/views/entities/new.html.haml b/app/views/entities/new.html.haml index d68415c..dd490c0 100644 --- a/app/views/entities/new.html.haml +++ b/app/views/entities/new.html.haml @@ -1,5 +1,7 @@ %h1 New entity -= render 'form' +.grid.grid-cols-3 + %div + = render 'form' = link_to 'Back', entities_path diff --git a/app/views/entities/show.html.haml b/app/views/entities/show.html.haml index c0d16d4..9d06372 100644 --- a/app/views/entities/show.html.haml +++ b/app/views/entities/show.html.haml @@ -1,15 +1,22 @@ -%p#notice= notice +.grid.grid-cols-3 + %div + %div + %p + %b Name: + = @entity.name + %p + %b Ha: + = @entity.ha_id + %p + %b Type: + = @entity.ha_type + %p + %b Type: + = @entity.value + %p + %b Member: + = @entity.member.name -%p - %b Name: - = @entity.name -%p - %b Ha: - = @entity.ha_id -%p - %b Type: - = @entity.type - -= link_to 'Edit', edit_entity_path(@entity) -\| -= link_to 'Back', entities_path + = link_to 'Edit', edit_entity_path(@entity) + \| + = link_to 'Back', entities_path diff --git a/db/migrate/20230113170732_create_entities.rb b/db/migrate/20230113170732_create_entities.rb index 855398b..36ba797 100644 --- a/db/migrate/20230113170732_create_entities.rb +++ b/db/migrate/20230113170732_create_entities.rb @@ -3,8 +3,9 @@ class CreateEntities < ActiveRecord::Migration[7.0] create_table :entities do |t| t.string :name t.string :ha_id - t.string :type - t.references :member + t.string :ha_type + t.references :member, null: false, foreign_key: true + t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb index fcd0ded..b21f953 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -17,8 +17,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_170732) do create_table "entities", force: :cascade do |t| t.string "name" t.string "ha_id" - t.string "type" - t.bigint "member_id" + t.string "ha_type" + t.bigint "member_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["member_id"], name: "index_entities_on_member_id" @@ -287,6 +287,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_170732) do t.index ["user_id", "postable_id"], name: "thredded_user_topic_read_states_user_postable", unique: true end + add_foreign_key "entities", "members" add_foreign_key "thredded_messageboard_users", "thredded_messageboards", on_delete: :cascade add_foreign_key "thredded_messageboard_users", "thredded_user_details", on_delete: :cascade add_foreign_key "thredded_user_post_notifications", "members", column: "user_id", on_delete: :cascade diff --git a/test/controllers/entities_controller_test.rb b/test/controllers/entities_controller_test.rb index fd51aa0..1b0ae62 100644 --- a/test/controllers/entities_controller_test.rb +++ b/test/controllers/entities_controller_test.rb @@ -17,7 +17,7 @@ class EntitiesControllerTest < ActionDispatch::IntegrationTest test "should create entity" do assert_difference("Entity.count") do - post entities_url, params: { entity: { ha_id: @entity.ha_id, name: @entity.name, type: @entity.type } } + post entities_url, params: { entity: { ha_id: @entity.ha_id, member_id: @entity.member_id, name: @entity.name, type: @entity.type } } end assert_redirected_to entity_url(Entity.last) @@ -34,7 +34,7 @@ class EntitiesControllerTest < ActionDispatch::IntegrationTest end test "should update entity" do - patch entity_url(@entity), params: { entity: { ha_id: @entity.ha_id, name: @entity.name, type: @entity.type } } + patch entity_url(@entity), params: { entity: { ha_id: @entity.ha_id, member_id: @entity.member_id, name: @entity.name, type: @entity.type } } assert_redirected_to entity_url(@entity) end diff --git a/test/fixtures/entities.yml b/test/fixtures/entities.yml index 72e7461..1f47312 100644 --- a/test/fixtures/entities.yml +++ b/test/fixtures/entities.yml @@ -4,8 +4,10 @@ one: name: MyString ha_id: MyString type: + member: one two: name: MyString ha_id: MyString type: + member: two diff --git a/test/system/entities_test.rb b/test/system/entities_test.rb index 93783e0..52cbf5f 100644 --- a/test/system/entities_test.rb +++ b/test/system/entities_test.rb @@ -15,6 +15,7 @@ class EntitiesTest < ApplicationSystemTestCase click_on "New entity" fill_in "Ha", with: @entity.ha_id + fill_in "Member", with: @entity.member_id fill_in "Name", with: @entity.name fill_in "Type", with: @entity.type click_on "Create Entity" @@ -28,6 +29,7 @@ class EntitiesTest < ApplicationSystemTestCase click_on "Edit this entity", match: :first fill_in "Ha", with: @entity.ha_id + fill_in "Member", with: @entity.member_id fill_in "Name", with: @entity.name fill_in "Type", with: @entity.type click_on "Update Entity"