Skip to content

Commit 2fec08f

Browse files
Refactor Variant Detail presentation (#47)
* split presenter goes away and VariantDetail handles view related details
1 parent 7344a4e commit 2fec08f

7 files changed

Lines changed: 76 additions & 90 deletions

File tree

app/controllers/admin/splits_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ def index
44
end
55

66
def show
7-
@split = SplitPresenter.new(Split.find(params[:id]))
7+
@split = Split.find(params[:id])
88
end
99
end

app/models/variant_detail.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ def display_name
88
super || variant
99
end
1010

11+
def weight
12+
@weight ||= split.variant_weight(variant)
13+
end
14+
15+
def assignment_count
16+
@assignment_count ||= split.assignment_count_for_variant(variant)
17+
end
18+
19+
def retirable?
20+
weight == 0 && assignment_count > 0
21+
end
22+
1123
private
1224

1325
def variant_must_exist

app/presenters/split_presenter.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/presenters/variant_presenter.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/views/admin/splits/_variants.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<th></th>
1414
</thead>
1515
<tbody>
16-
<% split.variant_details.each do |variant_detail| %>
16+
<% split.detail.variant_details.each do |variant_detail| %>
1717
<tr>
1818
<td><%= variant_detail.display_name %></td>
1919
<td><%= variant_detail.description %></td>

spec/models/variant_detail_spec.rb

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,68 @@
11
require 'rails_helper'
22

33
RSpec.describe VariantDetail do
4-
let(:split) { FactoryGirl.create(:split, name: 'split_enabled', registry: { true: 50, false: 50 }) }
4+
let(:split) { FactoryGirl.create(:split, name: "some_feature_enabled", registry: { true: 40, false: 60 }) }
5+
6+
describe "#weight" do
7+
subject { described_class.new(split: split, variant: 'true') }
8+
9+
it "is the weight of the given variant" do
10+
expect(subject.weight).to eq 40
11+
end
12+
end
13+
14+
describe "#assignment_count" do
15+
let!(:true_assignment) { FactoryGirl.create(:assignment, split: split, variant: "true") }
16+
let!(:false_assignment) { FactoryGirl.create_pair(:assignment, split: split, variant: "false") }
17+
18+
let(:true_presenter) { described_class.new(split: split, variant: 'true') }
19+
let(:false_presenter) { described_class.new(split: split, variant: 'false') }
20+
21+
it "is the number of assignments of given variant" do
22+
expect(true_presenter.assignment_count).to eq 1
23+
expect(false_presenter.assignment_count).to eq 2
24+
end
25+
end
26+
27+
describe "#retirable?" do
28+
subject { described_class.new(split: split, variant: 'true') }
29+
30+
context 'with a 0% weight' do
31+
let(:split) { FactoryGirl.create(:split, name: "some_feature_enabled", registry: { true: 0, false: 100 }) }
32+
33+
context 'with no assignments' do
34+
it "is false" do
35+
expect(subject).not_to be_retirable
36+
end
37+
end
38+
39+
context 'with some assignments' do
40+
let!(:assignment) { FactoryGirl.create(:assignment, split: split, variant: "true") }
41+
42+
it "is true" do
43+
expect(subject).to be_retirable
44+
end
45+
end
46+
end
47+
48+
context 'with a non0% weight' do
49+
let(:split) { FactoryGirl.create(:split, name: "some_feature_enabled", registry: { true: 1, false: 99 }) }
50+
51+
context 'with no assignments' do
52+
it "is false" do
53+
expect(subject).not_to be_retirable
54+
end
55+
end
56+
57+
context 'with some assignments' do
58+
let!(:assignment) { FactoryGirl.create(:assignment, split: split, variant: "true") }
59+
60+
it "is false for a non 0% weight that has assignments" do
61+
expect(subject).not_to be_retirable
62+
end
63+
end
64+
end
65+
end
566

667
describe '#valid?' do
768
context 'with a variant that exists' do

spec/models/variant_presenter_spec.rb

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)