Relationships between the CAF bits now in place

Rename the type field to kind, type is reserved
This commit is contained in:
Jez Caudle 2023-02-12 07:01:39 +00:00
parent 410a59fa04
commit d96050c4fb
9 changed files with 55 additions and 10 deletions

View File

@ -6,9 +6,6 @@ class CompaniesController < ApplicationController
@companies = Company.all
end
# GET /companies/1 or /companies/1.json
def show
end
# GET /companies/new
def new
@ -25,7 +22,7 @@ class CompaniesController < ApplicationController
respond_to do |format|
if @company.save
format.html { redirect_to company_url(@company), notice: "Company was successfully created." }
format.html { redirect_to root_url, notice: "Company was successfully created." }
format.json { render :show, status: :created, location: @company }
else
format.html { render :new, status: :unprocessable_entity }

View File

@ -1,6 +1,50 @@
class Caf < ApplicationRecord
after_create :populate_caf_info
belongs_to :company
has_many :objectives, dependent: :destroy
validates :name, presence: true
validates :name, uniqueness: { scope: :company_id, message: "should be unique to the company"}
private
def populate_caf_info
file = YAML.load_file(Rails.root.to_s + '/config/caf_text.yml')
objectives = file["objectives"]
objectives.each do |objective|
objective_info = objective["objective"]
#<p><%= objective_info["name"] %> <br/> <%= objective_info["description"] %></p>
obj = self.objectives.create(name: objective_info["name"], description: objective_info["description"] )
principles = objective_info["principles"]
principles.each do |principle|
principle_info = principle["principle"]
# <p><%= principle_info["name"] %><br><%= principle_info["description"]%></p>
pri = obj.principles.create(name: principle_info["name"], description: principle_info["description"])
sub_principles = principle_info["sub-principles"]
sub_principles.each do |sub_principle|
sub_principle_info = sub_principle["sub-principle"]
# <p><%= sub_principle_info["name"]%><br/><%= sub_principle_info["description"]%></p>
subpri = pri.subprinciples.create(name: sub_principle_info["name"], description: sub_principle_info["description"] )
sub_principle_item_groups = sub_principle_info["subprincipleitemgroups"]
sub_principle_item_groups.each do |key,value|
header = key["subprincipleitemgroup"]
# <%= header["type"] %> - <%= header["condition"] %><br/>
subpriitemgroup = subpri.subprincipleitemgroups.create(type: header["type"], condition: header["condition"])
subprincipleitem = header["subprincipleitem"]
subprincipleitem.each do |subprinciple|
# <%= subprinciple %><br/>
subpriitemgroup.subprinciples.create(description: subprinciple)
end
end
end
end
end
end
end

View File

@ -1,5 +1,5 @@
class Company < ApplicationRecord
has_many :cafs
has_many :cafs, dependent: :destroy
has_and_belongs_to_many :users
validates :name, presence: true
validates :name, uniqueness: true

View File

@ -1,4 +1,4 @@
class Objective < ApplicationRecord
belongs_to :caf
has_many :principles, dependent: :destroy
end

View File

@ -1,3 +1,4 @@
class Principle < ApplicationRecord
belongs_to :caf
belongs_to :objective
has_many :subprinciples, dependent: :destroy
end

View File

@ -1,3 +1,4 @@
class Subprinciple < ApplicationRecord
belongs_to :principle
has_many :subprincipleitemgroups, dependent: :destroy
end

View File

@ -1,3 +1,4 @@
class Subprincipleitemgroup < ApplicationRecord
belongs_to :subprinciple
has_many :subprincipleitems, dependent: :destroy
end

View File

@ -1,10 +1,7 @@
<p style="color: green"><%= notice %></p>
<%= render @company %>
<div>
<%= link_to "Edit this company", edit_company_path(@company) %> |
<%= link_to "Back to companies", companies_path %>
<%= button_to "Destroy this company", @company, method: :delete %>
</div>

View File

@ -0,0 +1,4 @@
class RenameTypeToKindInSubprincipleitemgroup < ActiveRecord::Migration[7.0]
def change
end
end