detour 0.0.12 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/models/detour/group.rb +1 -0
- data/lib/detour/acts_as_flaggable.rb +22 -0
- data/lib/detour/flaggable.rb +9 -40
- data/lib/detour/version.rb +1 -1
- data/spec/lib/detour/acts_as_flaggable_spec.rb +25 -0
- data/spec/lib/detour/flaggable_spec.rb +9 -7
- data/spec/models/detour/{defined_group_spec.rb → group_spec.rb} +1 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e523669f071b4f7fafbedabf9f38352bb33cc014
|
4
|
+
data.tar.gz: d8a1cdbcec53f6c854db8626e47bab1037dcdbcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa7e64b13cbee206eef6b6f458ffb8a3e8dd9552e8ad157c77a81a68bb8715cfc974c393baadd01610c6d4eb49add4852e0fd2e39c9716c8e24e66445d931a44
|
7
|
+
data.tar.gz: bafb4b8f3fe1105dc4f71d7ba3a1093933bf273f7c0e921ed426ef4c86e596958ed311dd17e5a16eab15d1e5010be6c8729d96f4a51ce984232756e520552ea5
|
data/app/models/detour/group.rb
CHANGED
@@ -2,6 +2,7 @@ class Detour::Group < ActiveRecord::Base
|
|
2
2
|
validates :name, presence: true, uniqueness: { scope: :flaggable_type }
|
3
3
|
validates :flaggable_type, presence: true, inclusion: { in: Detour.config.flaggable_types }
|
4
4
|
has_many :memberships, dependent: :destroy
|
5
|
+
has_many :database_group_flags, dependent: :destroy
|
5
6
|
|
6
7
|
accepts_nested_attributes_for :memberships, allow_destroy: true
|
7
8
|
|
@@ -67,6 +67,18 @@ module Detour::ActsAsFlaggable
|
|
67
67
|
as: :flaggable,
|
68
68
|
class_name: "Detour::FlagInFlag"
|
69
69
|
|
70
|
+
has_many :memberships,
|
71
|
+
as: :member,
|
72
|
+
class_name: "Detour::Membership"
|
73
|
+
|
74
|
+
has_many :groups,
|
75
|
+
through: :memberships,
|
76
|
+
class_name: "Detour::Group"
|
77
|
+
|
78
|
+
has_many :database_group_flags,
|
79
|
+
through: :groups,
|
80
|
+
class_name: "Detour::DatabaseGroupFlag"
|
81
|
+
|
70
82
|
has_many :opt_out_flags,
|
71
83
|
as: :flaggable,
|
72
84
|
class_name: "Detour::OptOutFlag"
|
@@ -75,6 +87,16 @@ module Detour::ActsAsFlaggable
|
|
75
87
|
@detour_flaggable_find_by = options[:find_by]
|
76
88
|
end
|
77
89
|
|
90
|
+
def defined_group_flags
|
91
|
+
@defined_group_flags ||= Detour::DefinedGroupFlag.where(flaggable_type: self.class.to_s)
|
92
|
+
end
|
93
|
+
|
94
|
+
def percentage_flags
|
95
|
+
@percentage_flags ||= Detour::PercentageFlag
|
96
|
+
.where(flaggable_type: self.class.to_s)
|
97
|
+
.where("? % 10 < detour_flags.percentage / 10", id)
|
98
|
+
end
|
99
|
+
|
78
100
|
def self.detour_flaggable_find_by
|
79
101
|
@detour_flaggable_find_by
|
80
102
|
end
|
data/lib/detour/flaggable.rb
CHANGED
@@ -16,7 +16,7 @@ module Detour::Flaggable
|
|
16
16
|
defined_group = Detour::DefinedGroup.by_type(self.class)[defined_group_flag.group_name]
|
17
17
|
|
18
18
|
unless defined_group && defined_group.test(self)
|
19
|
-
|
19
|
+
features.delete defined_group_flag.feature
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -47,45 +47,14 @@ module Detour::Flaggable
|
|
47
47
|
private
|
48
48
|
|
49
49
|
def unfiltered_features
|
50
|
-
Detour::Feature.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
-- Get features the record has been flagged into via group membership
|
59
|
-
SELECT feature_id FROM detour_flags
|
60
|
-
INNER JOIN detour_groups
|
61
|
-
ON detour_groups.id = detour_flags.group_id
|
62
|
-
INNER JOIN detour_memberships
|
63
|
-
ON detour_memberships.group_id = detour_groups.id
|
64
|
-
AND detour_memberships.member_id = '#{id}'
|
65
|
-
WHERE detour_flags.type = 'Detour::DatabaseGroupFlag'
|
66
|
-
AND detour_flags.flaggable_type = '#{self.class}'
|
67
|
-
) OR detour_features.id IN (
|
68
|
-
-- Get features the record has been flagged into via defined membership
|
69
|
-
-- We'll test them later
|
70
|
-
SELECT feature_id FROM detour_flags
|
71
|
-
WHERE detour_flags.type = 'Detour::DefinedGroupFlag'
|
72
|
-
AND detour_flags.flaggable_type = '#{self.class}'
|
73
|
-
) OR detour_features.id IN (
|
74
|
-
-- Get features the record has been flagged into via percentage
|
75
|
-
SELECT feature_id FROM detour_flags
|
76
|
-
WHERE detour_flags.type = 'Detour::PercentageFlag'
|
77
|
-
AND detour_flags.flaggable_type = '#{self.class}'
|
78
|
-
AND '#{id}' % 10 < detour_flags.percentage / 10
|
79
|
-
)
|
80
|
-
}).where(%Q{
|
81
|
-
-- Exclude features the record has been opted out of.
|
82
|
-
detour_features.id NOT IN (
|
83
|
-
SELECT feature_id FROM detour_flags
|
84
|
-
WHERE detour_flags.type = 'Detour::OptOutFlag'
|
85
|
-
AND detour_flags.flaggable_type = '#{self.class}'
|
86
|
-
AND detour_flags.flaggable_id = '#{id}'
|
87
|
-
)
|
88
|
-
})
|
50
|
+
table = Detour::Feature.arel_table
|
51
|
+
query = table[:id].in(flag_in_flags.select(:feature_id).arel)
|
52
|
+
.or(table[:id].in(database_group_flags.select(:feature_id).arel))
|
53
|
+
.or(table[:id].in(defined_group_flags.select(:feature_id).arel))
|
54
|
+
.or(table[:id].in(percentage_flags.select(:feature_id).arel))
|
55
|
+
.and(table[:id].not_in(opt_out_flags.select(:feature_id).arel))
|
56
|
+
|
57
|
+
Detour::Feature.where(query)
|
89
58
|
end
|
90
59
|
|
91
60
|
included do
|
data/lib/detour/version.rb
CHANGED
@@ -5,6 +5,31 @@ describe Detour::ActsAsFlaggable do
|
|
5
5
|
|
6
6
|
it { should have_many :flag_in_flags }
|
7
7
|
it { should have_many :opt_out_flags }
|
8
|
+
it { should have_many(:memberships).class_name("Detour::Membership") }
|
9
|
+
it { should have_many(:groups).through(:memberships).class_name("Detour::Group") }
|
10
|
+
it { should have_many(:database_group_flags).through(:groups).class_name("Detour::DatabaseGroupFlag") }
|
11
|
+
|
12
|
+
describe "#defined_group_flags" do
|
13
|
+
it "returns the defined group flags for the class" do
|
14
|
+
Detour::DefinedGroupFlag.should_receive(:where).with(flaggable_type: "User")
|
15
|
+
subject.defined_group_flags
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#percentage_flags" do
|
20
|
+
it "returns the percentage flags for the class" do
|
21
|
+
Detour::PercentageFlag.should_receive(:where).with(flaggable_type: "User").and_call_original
|
22
|
+
subject.percentage_flags
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns the percentage flags with the correct formula" do
|
26
|
+
subject.save!
|
27
|
+
where_double = double(where: true)
|
28
|
+
Detour::PercentageFlag.stub(:where) { where_double }
|
29
|
+
where_double.should_receive(:where).with("? % 10 < detour_flags.percentage / 10", subject.id)
|
30
|
+
subject.percentage_flags
|
31
|
+
end
|
32
|
+
end
|
8
33
|
|
9
34
|
it "includes Detour::Flaggable" do
|
10
35
|
subject.class.ancestors.should include Detour::Flaggable
|
@@ -4,13 +4,14 @@ describe Detour::Flaggable do
|
|
4
4
|
subject { create :user }
|
5
5
|
|
6
6
|
describe "#features" do
|
7
|
-
let(:feature1)
|
8
|
-
let(:feature2)
|
9
|
-
let(:feature3)
|
10
|
-
let(:feature4)
|
11
|
-
let(:feature5)
|
12
|
-
let(:membership)
|
13
|
-
let(:defined_group)
|
7
|
+
let(:feature1) { create :feature }
|
8
|
+
let(:feature2) { create :feature }
|
9
|
+
let(:feature3) { create :feature }
|
10
|
+
let(:feature4) { create :feature }
|
11
|
+
let(:feature5) { create :feature }
|
12
|
+
let(:membership) { create :membership, member: subject }
|
13
|
+
let(:defined_group) { Detour::DefinedGroup.new "foo", ->(user){true} }
|
14
|
+
let(:defined_group2) { Detour::DefinedGroup.new "foo2", ->(user){false} }
|
14
15
|
|
15
16
|
before do
|
16
17
|
Detour.config.defined_groups["User"] = { foo: defined_group }
|
@@ -20,6 +21,7 @@ describe Detour::Flaggable do
|
|
20
21
|
create :database_group_flag, flaggable_type: "User", feature: feature4, group: membership.group
|
21
22
|
create :opt_out_flag, flaggable: subject, feature: feature4
|
22
23
|
create :defined_group_flag, flaggable_type: "User", feature: feature5, group_name: "foo"
|
24
|
+
create :defined_group_flag, flaggable_type: "User", feature: feature5, group_name: "foo2"
|
23
25
|
end
|
24
26
|
|
25
27
|
it "finds every feature for a record" do
|
@@ -9,6 +9,7 @@ describe Detour::Group do
|
|
9
9
|
it { should accept_nested_attributes_for :memberships }
|
10
10
|
|
11
11
|
it { should have_many(:memberships).dependent(:destroy) }
|
12
|
+
it { should have_many(:database_group_flags).dependent(:destroy) }
|
12
13
|
it { should allow_mass_assignment_of :name }
|
13
14
|
it { should allow_mass_assignment_of :flaggable_type }
|
14
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: detour
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Clem
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -363,12 +363,12 @@ files:
|
|
363
363
|
- spec/lib/detour/flag_form_spec.rb
|
364
364
|
- spec/lib/detour/flaggable_spec.rb
|
365
365
|
- spec/models/detour/database_group_flag_spec.rb
|
366
|
-
- spec/models/detour/defined_group_spec.rb
|
367
366
|
- spec/models/detour/feature_spec.rb
|
368
367
|
- spec/models/detour/flag_in_flag_spec.rb
|
369
368
|
- spec/models/detour/flag_spec.rb
|
370
369
|
- spec/models/detour/flaggable_flag_spec.rb
|
371
370
|
- spec/models/detour/group_flag_spec.rb
|
371
|
+
- spec/models/detour/group_spec.rb
|
372
372
|
- spec/models/detour/membership_spec.rb
|
373
373
|
- spec/models/detour/opt_out_flag_spec.rb
|
374
374
|
- spec/models/detour/percentage_flag_spec.rb
|
@@ -471,12 +471,12 @@ test_files:
|
|
471
471
|
- spec/lib/detour/flag_form_spec.rb
|
472
472
|
- spec/lib/detour/flaggable_spec.rb
|
473
473
|
- spec/models/detour/database_group_flag_spec.rb
|
474
|
-
- spec/models/detour/defined_group_spec.rb
|
475
474
|
- spec/models/detour/feature_spec.rb
|
476
475
|
- spec/models/detour/flag_in_flag_spec.rb
|
477
476
|
- spec/models/detour/flag_spec.rb
|
478
477
|
- spec/models/detour/flaggable_flag_spec.rb
|
479
478
|
- spec/models/detour/group_flag_spec.rb
|
479
|
+
- spec/models/detour/group_spec.rb
|
480
480
|
- spec/models/detour/membership_spec.rb
|
481
481
|
- spec/models/detour/opt_out_flag_spec.rb
|
482
482
|
- spec/models/detour/percentage_flag_spec.rb
|