Add SDG LocalTarget model
This commit is contained in:
@@ -4,7 +4,7 @@ module SDG::Relatable
|
||||
included do
|
||||
has_many :sdg_relations, as: :relatable, dependent: :destroy, class_name: "SDG::Relation"
|
||||
|
||||
%w[SDG::Goal SDG::Target].each do |sdg_type|
|
||||
%w[SDG::Goal SDG::Target SDG::LocalTarget].each do |sdg_type|
|
||||
has_many sdg_type.constantize.table_name.to_sym,
|
||||
through: :sdg_relations,
|
||||
source: :related_sdg,
|
||||
|
||||
37
app/models/sdg/local_target.rb
Normal file
37
app/models/sdg/local_target.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class SDG::LocalTarget < ApplicationRecord
|
||||
include Comparable
|
||||
include SDG::Related
|
||||
|
||||
delegate :goal, to: :target
|
||||
|
||||
translates :title, touch: true
|
||||
translates :description, touch: true
|
||||
include Globalizable
|
||||
|
||||
validates_translation :title, presence: true
|
||||
validates_translation :description, presence: true
|
||||
|
||||
validates :code, presence: true, uniqueness: true,
|
||||
format: ->(local_target) { /\A#{local_target.target&.code}\.\d+/ }
|
||||
validates :target, presence: true
|
||||
|
||||
belongs_to :target
|
||||
|
||||
def <=>(local_target)
|
||||
return unless local_target.class == self.class
|
||||
|
||||
[target, numeric_subcode] <=> [local_target.target, local_target.numeric_subcode]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def numeric_subcode
|
||||
subcode.to_i
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def subcode
|
||||
code.split(".").last
|
||||
end
|
||||
end
|
||||
@@ -6,6 +6,7 @@ class SDG::Target < ApplicationRecord
|
||||
validates :goal, presence: true
|
||||
|
||||
belongs_to :goal
|
||||
has_many :local_targets, dependent: :destroy
|
||||
|
||||
def title
|
||||
I18n.t("sdg.goals.goal_#{goal.code}.targets.target_#{code_key}.title")
|
||||
|
||||
Reference in New Issue
Block a user