entity scaffold

This commit is contained in:
Torsten 2023-01-13 20:50:41 +02:00
parent c46ecb73e0
commit 26ea76c8e9
15 changed files with 272 additions and 1 deletions

View File

@ -0,0 +1,58 @@
class EntitiesController < ApplicationController
before_action :set_entity, only: %i[ show edit update destroy ]
# GET /entities
def index
@entities = Entity.all
end
# GET /entities/1
def show
end
# GET /entities/new
def new
@entity = Entity.new
end
# GET /entities/1/edit
def edit
end
# POST /entities
def create
@entity = Entity.new(entity_params)
if @entity.save
redirect_to @entity, notice: "Entity was successfully created."
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /entities/1
def update
if @entity.update(entity_params)
redirect_to @entity, notice: "Entity was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /entities/1
def destroy
@entity.destroy
redirect_to entities_url, notice: "Entity was successfully destroyed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_entity
@entity = Entity.find(params[:id])
end
# Only allow a list of trusted parameters through.
def entity_params
params.require(:entity).permit(:name, :ha_id, :type , :member_id)
end
end

View File

@ -0,0 +1,2 @@
module EntitiesHelper
end

7
app/models/entity.rb Normal file
View File

@ -0,0 +1,7 @@
class Entity < ApplicationRecord
validate :name , presence: true
validate :ha_id , presence: true
validate :type , presence: true
validate :member_id , presence: true
end

View File

@ -0,0 +1,19 @@
= form_for @entity do |f|
- if @entity.errors.any?
#error_explanation
%h2= "#{pluralize(@entity.errors.count, "error")} prohibited this entity from being saved:"
%ul
- @entity.errors.full_messages.each do |message|
%li= message
.field
= f.label :name
= f.text_field :name
.field
= f.label :ha_id
= f.text_field :ha_id
.field
= f.label :type
= f.text_field :type
.actions
= f.submit 'Save'

View File

@ -0,0 +1,7 @@
%h1 Editing entity
= render 'form'
= link_to 'Show', @entity
\|
= link_to 'Back', entities_path

View File

@ -0,0 +1,25 @@
%h1 Listing entities
%table
%thead
%tr
%th Name
%th Ha
%th Type
%th
%th
%th
%tbody
- @entities.each do |entity|
%tr
%td= entity.name
%td= entity.ha_id
%td= entity.type
%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?' }
%br
= link_to 'New Entity', new_entity_path

View File

@ -0,0 +1,5 @@
%h1 New entity
= render 'form'
= link_to 'Back', entities_path

View File

@ -0,0 +1,15 @@
%p#notice= notice
%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

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :entities
devise_for :members

View File

@ -0,0 +1,11 @@
class CreateEntities < ActiveRecord::Migration[7.0]
def change
create_table :entities do |t|
t.string :name
t.string :ha_id
t.string :type
t.references :member
t.timestamps
end
end
end

12
db/schema.rb generated
View File

@ -10,10 +10,20 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_12_31_154221) do
ActiveRecord::Schema[7.0].define(version: 2023_01_13_170732) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "entities", force: :cascade do |t|
t.string "name"
t.string "ha_id"
t.string "type"
t.bigint "member_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["member_id"], name: "index_entities_on_member_id"
end
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false

View File

@ -0,0 +1,48 @@
require "test_helper"
class EntitiesControllerTest < ActionDispatch::IntegrationTest
setup do
@entity = entities(:one)
end
test "should get index" do
get entities_url
assert_response :success
end
test "should get new" do
get new_entity_url
assert_response :success
end
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 } }
end
assert_redirected_to entity_url(Entity.last)
end
test "should show entity" do
get entity_url(@entity)
assert_response :success
end
test "should get edit" do
get edit_entity_url(@entity)
assert_response :success
end
test "should update entity" do
patch entity_url(@entity), params: { entity: { ha_id: @entity.ha_id, name: @entity.name, type: @entity.type } }
assert_redirected_to entity_url(@entity)
end
test "should destroy entity" do
assert_difference("Entity.count", -1) do
delete entity_url(@entity)
end
assert_redirected_to entities_url
end
end

11
test/fixtures/entities.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
ha_id: MyString
type:
two:
name: MyString
ha_id: MyString
type:

View File

@ -0,0 +1,7 @@
require "test_helper"
class EntityTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,45 @@
require "application_system_test_case"
class EntitiesTest < ApplicationSystemTestCase
setup do
@entity = entities(:one)
end
test "visiting the index" do
visit entities_url
assert_selector "h1", text: "Entities"
end
test "should create entity" do
visit entities_url
click_on "New entity"
fill_in "Ha", with: @entity.ha_id
fill_in "Name", with: @entity.name
fill_in "Type", with: @entity.type
click_on "Create Entity"
assert_text "Entity was successfully created"
click_on "Back"
end
test "should update Entity" do
visit entity_url(@entity)
click_on "Edit this entity", match: :first
fill_in "Ha", with: @entity.ha_id
fill_in "Name", with: @entity.name
fill_in "Type", with: @entity.type
click_on "Update Entity"
assert_text "Entity was successfully updated"
click_on "Back"
end
test "should destroy Entity" do
visit entity_url(@entity)
click_on "Destroy this entity", match: :first
assert_text "Entity was successfully destroyed"
end
end