Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local targets to related list selector #4327

Merged
merged 4 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add method to easily access a local target by code
Similar to what we do in goals and targets.
  • Loading branch information
javierm authored and taitus committed Jan 26, 2021
commit cb57a4696da6aa083ff99dfc58ac92c6ff2d043f
4 changes: 4 additions & 0 deletions app/models/sdg/local_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class SDG::LocalTarget < ApplicationRecord

belongs_to :target

def self.[](code)
taitus marked this conversation as resolved.
Show resolved Hide resolved
find_by!(code: code)
end

def <=>(any_target)
if any_target.class == self.class
[target, numeric_subcode] <=> [any_target.target, any_target.numeric_subcode]
Expand Down
12 changes: 12 additions & 0 deletions spec/models/sdg/local_target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,16 @@
expect(local_target).to be < greater_target
end
end

describe ".[]" do
it "finds existing local targets by code" do
create(:sdg_local_target, code: "1.1.1")

expect(SDG::LocalTarget["1.1.1"].code).to eq "1.1.1"
end

it "raises an exception for non-existing codes" do
expect { SDG::LocalTarget["1.1.99"] }.to raise_exception ActiveRecord::RecordNotFound
end
end
end
9 changes: 4 additions & 5 deletions spec/system/sdg_management/relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@
end

scenario "local target filter" do
schools = create(:sdg_local_target, code: "4.1.1")
teachers = create(:sdg_local_target, code: "4.1.2")

create(:debate, title: "Rebuild local schools", sdg_local_targets: [schools])
create(:debate, title: "Hire teachers", sdg_local_targets: [teachers])
create(:sdg_local_target, code: "4.1.1")
create(:sdg_local_target, code: "4.1.2")
create(:debate, title: "Rebuild local schools", sdg_local_targets: [SDG::LocalTarget["4.1.1"]])
create(:debate, title: "Hire teachers", sdg_local_targets: [SDG::LocalTarget["4.1.2"]])

visit sdg_management_debates_path
select "4.1.1", from: "target_code"
Expand Down