basic entity get/show

This commit is contained in:
2023-01-13 21:47:31 +02:00
parent 26ea76c8e9
commit 38f51a5de6
13 changed files with 74 additions and 38 deletions

View File

@ -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

View File

@ -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

View File

@ -6,6 +6,8 @@ class Member < ApplicationRecord
mount_uploader :picture, PictureUploader
has_many :entity
def admin
true
end

View File

@ -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'

View File

@ -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?' }

View File

@ -1,5 +1,7 @@
%h1 New entity
= render 'form'
.grid.grid-cols-3
%div
= render 'form'
= link_to 'Back', entities_path

View File

@ -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