initial commit
This commit is contained in:
3
app/models/application_record.rb
Normal file
3
app/models/application_record.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
primary_abstract_class
|
||||
end
|
||||
8
app/models/client.rb
Normal file
8
app/models/client.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Client < ApplicationRecord
|
||||
has_many :client_subprojects, class_name: 'Subproject', foreign_key: 'client_id'
|
||||
has_many :owner_subprojects, class_name: 'Subproject', foreign_key: 'owner_id'
|
||||
has_many :builder_subprojects, class_name: 'Subproject', foreign_key: 'builder_id'
|
||||
|
||||
validates :company_name, :firstname, :lastname, presence: true
|
||||
# Add other validations as needed
|
||||
end
|
||||
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
6
app/models/project.rb
Normal file
6
app/models/project.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class Project < ApplicationRecord
|
||||
has_many :subprojects, inverse_of: :project
|
||||
accepts_nested_attributes_for :subprojects, allow_destroy: true, reject_if: :all_blank
|
||||
|
||||
validates :name, presence: true
|
||||
end
|
||||
53
app/models/subproject.rb
Normal file
53
app/models/subproject.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
class Subproject < ApplicationRecord
|
||||
belongs_to :project
|
||||
|
||||
belongs_to :client, class_name: 'Client', optional: true
|
||||
belongs_to :owner, class_name: 'Client', optional: true
|
||||
belongs_to :builder, class_name: 'Client', optional: true
|
||||
|
||||
accepts_nested_attributes_for :client, reject_if: :all_blank
|
||||
accepts_nested_attributes_for :owner, reject_if: :all_blank
|
||||
accepts_nested_attributes_for :builder, reject_if: :all_blank
|
||||
|
||||
validates :subproject_name, presence: true
|
||||
validate :client_presence_check
|
||||
validate :owner_presence_check
|
||||
validate :builder_presence_check
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def client_presence_check
|
||||
if client_id.blank? && (client_attributes_blank?)
|
||||
errors.add(:client, "must be selected or a new client must be provided")
|
||||
end
|
||||
end
|
||||
|
||||
def owner_presence_check
|
||||
if owner_id.blank? && (owner_attributes_blank?)
|
||||
errors.add(:owner, "must be selected or a new owner must be provided")
|
||||
end
|
||||
end
|
||||
|
||||
def builder_presence_check
|
||||
if builder_id.blank? && (builder_attributes_blank?)
|
||||
errors.add(:builder, "must be selected or a new builder must be provided")
|
||||
end
|
||||
end
|
||||
|
||||
def client_attributes_blank?
|
||||
return true if client.nil?
|
||||
client.attributes.except("id", "created_at", "updated_at").values.all?(&:blank?)
|
||||
end
|
||||
|
||||
def owner_attributes_blank?
|
||||
return true if client.nil?
|
||||
owner_attributes.except("id", "created_at", "updated_at").values.all?(&:blank?)
|
||||
end
|
||||
|
||||
def builder_attributes_blank?
|
||||
return true if client.nil?
|
||||
builder_attributes.except("id", "created_at", "updated_at").values.all?(&:blank?)
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user