basic integration of entities to profile

This commit is contained in:
Torsten 2023-01-13 22:23:23 +02:00
parent 38f51a5de6
commit d869eab476
6 changed files with 24 additions and 17 deletions

View File

@ -3,7 +3,6 @@ class Entity < ApplicationRecord
validates :name , presence: true validates :name , presence: true
validates :ha_id , presence: true validates :ha_id , presence: true
validates :ha_type , presence: true
validates :member_id , presence: true validates :member_id , presence: true
def value def value
@ -11,6 +10,10 @@ class Entity < ApplicationRecord
@data["state"] @data["state"]
end end
def type
ha_id.split(".").first.capitalize
end
def self.ha_entities def self.ha_entities
res = response() res = response()
res.collect{|i| i["entity_id"]}.select{|i| i.include?("4a") || i.include?("2d")} res.collect{|i| i["entity_id"]}.select{|i| i.include?("4a") || i.include?("2d")}
@ -20,6 +23,6 @@ class Entity < ApplicationRecord
token = Rails.application.credentials[:ha_token] token = Rails.application.credentials[:ha_token]
all = RestClient.get( "10.30.39.238:8123/api/states#{path}" , all = RestClient.get( "10.30.39.238:8123/api/states#{path}" ,
{"Authorization" => "Bearer #{token}" }) {"Authorization" => "Bearer #{token}" })
JSON.parse(all.body) res = JSON.parse(all.body)
end end
end end

View File

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

View File

@ -10,8 +10,6 @@
= f.input :name = f.input :name
.field .field
= f.input :ha_id , collection: Entity.ha_entities = f.input :ha_id , collection: Entity.ha_entities
.field
= f.input :ha_type , collection: ["text" , "bool"]
.field .field
= f.input :member_id , collection: Member.all.collect{|m| [m.name , m.id ]} = f.input :member_id , collection: Member.all.collect{|m| [m.name , m.id ]}
.actions{class: button_classes} .actions{class: button_classes}

View File

@ -16,8 +16,8 @@
%tr %tr
%td= entity.name %td= entity.name
%td= entity.ha_id %td= entity.ha_id
%td= entity.ha_type %td= entity.type
%td= entity.member %td= entity.member.name
%td= link_to 'Show', entity %td= link_to 'Show', entity
%td= link_to 'Edit', edit_entity_path(entity) %td= link_to 'Edit', edit_entity_path(entity)
%td= link_to 'Destroy', entity, method: :delete, data: { confirm: 'Are you sure?' } %td= link_to 'Destroy', entity, method: :delete, data: { confirm: 'Are you sure?' }

View File

@ -9,9 +9,9 @@
= @entity.ha_id = @entity.ha_id
%p %p
%b Type: %b Type:
= @entity.ha_type = @entity.type
%p %p
%b Type: %b Value:
= @entity.value = @entity.value
%p %p
%b Member: %b Member:

View File

@ -17,11 +17,17 @@
%svg.inline-block.w-3.ml-2{:fill => "currentColor", :viewbox => "0 0 12 12"} %svg.inline-block.w-3.ml-2{:fill => "currentColor", :viewbox => "0 0 12 12"}
%path{:d => "M9.707,5.293l-5-5A1,1,0,0,0,3.293,1.707L7.586,6,3.293,10.293a1,1,0,1,0,1.414,1.414l5-5A1,1,0,0,0,9.707,5.293Z"} %path{:d => "M9.707,5.293l-5-5A1,1,0,0,0,3.293,1.707L7.586,6,3.293,10.293a1,1,0,1,0,1.414,1.414l5-5A1,1,0,0,0,9.707,5.293Z"}
-if @member == current_member or current_member.admin?
.flex.justify-around .flex.justify-around
= link_to edit_member_path(@member) do = link_to edit_member_path(@member) do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400 %button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
Edit Edit
.grid.grid-cols-3.gap-4
-@member.entities.each do |entity|
%div= entity.type
%div= entity.name
%div= entity.value
= form_tag( destroy_member_session_path , {method: :delete } ) do = form_tag( destroy_member_session_path , {method: :delete } ) do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400{type: :submit} %button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400{type: :submit}
Sign out Sign out