Add SDG LocalTarget model

This commit is contained in:
Senén Rodero Rodríguez
2020-11-23 17:00:17 +01:00
parent 427dec8cbd
commit 2ad66409e2
10 changed files with 198 additions and 3 deletions

View File

@@ -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,

View 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

View File

@@ -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")