diff --git a/app/models/concerns/sdg/relatable.rb b/app/models/concerns/sdg/relatable.rb index ef92696a7..0c6908f2d 100644 --- a/app/models/concerns/sdg/relatable.rb +++ b/app/models/concerns/sdg/relatable.rb @@ -4,33 +4,41 @@ module SDG::Relatable included do has_many :sdg_relations, as: :relatable, dependent: :destroy, class_name: "SDG::Relation" - %w[SDG::Goal SDG::Target SDG::LocalTarget].each do |sdg_type| + %w[SDG::Goal SDG::LocalTarget].each do |sdg_type| has_many sdg_type.constantize.table_name.to_sym, through: :sdg_relations, source: :related_sdg, source_type: sdg_type end + has_many :sdg_global_targets, + through: :sdg_relations, + source: :related_sdg, + source_type: "SDG::Target" + alias_method :sdg_targets, :sdg_global_targets + alias_method :sdg_targets=, :sdg_global_targets= has_one :sdg_review, as: :relatable, dependent: :destroy, class_name: "SDG::Review" end class_methods do def by_goal(code) - by_sdg_related(SDG::Goal, code) + by_sdg_related(:sdg_goals, code) end def by_target(code) if SDG::Target.find_by(code: code) - by_sdg_related(SDG::Target, code) + by_sdg_related(:sdg_global_targets, code) else - by_sdg_related(SDG::LocalTarget, code) + by_sdg_related(:sdg_local_targets, code) end end - def by_sdg_related(sdg_class, code) + def by_sdg_related(association, code) return all if code.blank? - joins(sdg_class.table_name.to_sym).merge(sdg_class.where(code: code)) + sdg_class = reflect_on_association(association).options[:source_type].constantize + + joins(association).merge(sdg_class.where(code: code)) end def sdg_reviewed @@ -51,12 +59,12 @@ module SDG::Relatable end def sdg_target_list - sdg_targets.sort.map(&:code).join(", ") + sdg_global_targets.sort.map(&:code).join(", ") end def sdg_related_list sdg_goals.order(:code).map do |goal| - [goal, sdg_targets.where(goal: goal).sort] + [goal, sdg_global_targets.where(goal: goal).sort] end.flatten.map(&:code).join(", ") end @@ -66,7 +74,7 @@ module SDG::Relatable goals = goal_codes.map { |code| SDG::Goal[code] } transaction do - self.sdg_targets = targets + self.sdg_global_targets = targets self.sdg_goals = (targets.map(&:goal) + goals).uniq end end diff --git a/spec/models/sdg/relatable_spec.rb b/spec/models/sdg/relatable_spec.rb index 3065f89b0..31dad7a84 100644 --- a/spec/models/sdg/relatable_spec.rb +++ b/spec/models/sdg/relatable_spec.rb @@ -36,9 +36,9 @@ describe SDG::Relatable do end end - describe "#sdg_targets" do + describe "#sdg_global_targets" do it "can assign targets to a model" do - relatable.sdg_targets = [target, another_target] + relatable.sdg_global_targets = [target, another_target] expect(SDG::Relation.count).to be 2 expect(SDG::Relation.first.relatable).to eq relatable @@ -48,9 +48,9 @@ describe SDG::Relatable do end it "can obtain the list of targets" do - relatable.sdg_targets = [target, another_target] + relatable.sdg_global_targets = [target, another_target] - expect(relatable.reload.sdg_targets).to match_array [target, another_target] + expect(relatable.reload.sdg_global_targets).to match_array [target, another_target] end end @@ -190,7 +190,7 @@ describe SDG::Relatable do it "does not return records not associated with that target" do create(:proposal) - create(:proposal, sdg_targets: [another_target]) + create(:proposal, sdg_global_targets: [another_target]) expect(relatable.class.by_target(target.code)).to be_empty end