basic entity get/show
This commit is contained in:
parent
26ea76c8e9
commit
38f51a5de6
3
Gemfile
3
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"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -6,6 +6,8 @@ class Member < ApplicationRecord
|
||||
|
||||
mount_uploader :picture, PictureUploader
|
||||
|
||||
has_many :entity
|
||||
|
||||
def admin
|
||||
true
|
||||
end
|
||||
|
@ -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'
|
||||
|
@ -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?' }
|
||||
|
@ -1,5 +1,7 @@
|
||||
%h1 New entity
|
||||
|
||||
= render 'form'
|
||||
.grid.grid-cols-3
|
||||
%div
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Back', entities_path
|
||||
|
@ -1,15 +1,22 @@
|
||||
%p#notice= notice
|
||||
|
||||
%p
|
||||
.grid.grid-cols-3
|
||||
%div
|
||||
%div
|
||||
%p
|
||||
%b Name:
|
||||
= @entity.name
|
||||
%p
|
||||
%p
|
||||
%b Ha:
|
||||
= @entity.ha_id
|
||||
%p
|
||||
%p
|
||||
%b Type:
|
||||
= @entity.type
|
||||
= @entity.ha_type
|
||||
%p
|
||||
%b Type:
|
||||
= @entity.value
|
||||
%p
|
||||
%b Member:
|
||||
= @entity.member.name
|
||||
|
||||
= link_to 'Edit', edit_entity_path(@entity)
|
||||
\|
|
||||
= link_to 'Back', entities_path
|
||||
= link_to 'Edit', edit_entity_path(@entity)
|
||||
\|
|
||||
= link_to 'Back', entities_path
|
||||
|
@ -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
|
||||
|
5
db/schema.rb
generated
5
db/schema.rb
generated
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
2
test/fixtures/entities.yml
vendored
2
test/fixtures/entities.yml
vendored
@ -4,8 +4,10 @@ one:
|
||||
name: MyString
|
||||
ha_id: MyString
|
||||
type:
|
||||
member: one
|
||||
|
||||
two:
|
||||
name: MyString
|
||||
ha_id: MyString
|
||||
type:
|
||||
member: two
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user