active_flags 0.3.3 → 0.3.10
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/README.md +10 -9
- data/app/models/active_flags/flag.rb +28 -0
- data/lib/active_flags.rb +4 -7
- data/lib/active_flags/handler/flag_builder.rb +1 -1
- data/lib/active_flags/version.rb +1 -1
- data/lib/utils/value_stringifier.rb +12 -2
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc2b9964dd268bb5482ee832a4d1cbab56ab00c4a37ff13bf92cd56b03dbab39
|
4
|
+
data.tar.gz: 157e67e3132902f707ba8a849ab56807be2a989b64918658df1998884dcc9bcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07d09aa9564267e89000018f120d72ab80ffee79d466b99fa7197bd96bb2193a0f53da9279bfcb09bc5b8a1931a39a00e3518007815bceefe79f6d9461449063
|
7
|
+
data.tar.gz: e1f6e8ed611eab07b4af1bea6c30650d1a3403fcc74a1e52a9dcd874b621d143392744e91454f0d787a37f2423311507466a3e6f24ec22e6a2cbb775840e5cc5
|
data/README.md
CHANGED
@@ -74,7 +74,9 @@ user.update!(flags: { visible: true, active: true, diet: 'vegan', power: 'super
|
|
74
74
|
```
|
75
75
|
|
76
76
|
To access your flags, you now have 2 ways.
|
77
|
-
Either as a hash, with the `flags` method or as an ActiveFlag::
|
77
|
+
Either as a hash, with the `flags` method or as an ActiveFlag::Flag collection with the `flags_as_collection` method.
|
78
|
+
|
79
|
+
Note: You can call `converted_value` on an `ActiveFlag::Flag` instance returned by flags_as_collection, to retrieved your 'true' or 'false' value as a boolean.
|
78
80
|
|
79
81
|
## Flags as scopes
|
80
82
|
|
@@ -85,35 +87,34 @@ ActiveFlags gives you a clean and simple way to query your model based on define
|
|
85
87
|
Any flag can be queried as a scope using the `flagged_as` method
|
86
88
|
|
87
89
|
```ruby
|
88
|
-
user = User.create!(flags:
|
90
|
+
user = User.create!(flags: { visible: true })
|
89
91
|
|
90
92
|
User.flagged_as_visible
|
91
|
-
#
|
93
|
+
# #<ActiveRecord::Relation [#<User id: 1>]>
|
92
94
|
|
93
95
|
User.flagged_as_visible(false)
|
94
|
-
#
|
96
|
+
# #<ActiveRecord::Relation []>
|
95
97
|
|
96
98
|
User.flagged_as_intelligent
|
97
|
-
#
|
99
|
+
# #<ActiveRecord::Relation []>
|
98
100
|
|
99
101
|
user.update!(flags: { intelligent: true })
|
100
102
|
User.flagged_as_intelligent
|
101
|
-
#
|
103
|
+
# #<ActiveRecord::Relation [#<User id: 1>]>
|
102
104
|
|
103
105
|
user.update!(flags: { intelligent: 'a bit' })
|
104
106
|
User.flagged_as_intelligent('a_bit')
|
105
|
-
#
|
107
|
+
# #<ActiveRecord::Relation [#<User id: 1>]>
|
106
108
|
```
|
107
109
|
|
108
110
|
To query flags the other way around you can use the `not_flagged_as` method
|
109
111
|
|
110
112
|
```ruby
|
111
113
|
User.not_flagged_as_intelligent
|
112
|
-
#
|
114
|
+
# or with value
|
113
115
|
User.not_flagged_as_intelligent('a bit')
|
114
116
|
```
|
115
117
|
|
116
|
-
|
117
118
|
## Contributing
|
118
119
|
https://github.com/FidMe/active_flags
|
119
120
|
|
@@ -1,8 +1,36 @@
|
|
1
|
+
require 'utils/value_stringifier'
|
2
|
+
|
1
3
|
module ActiveFlags
|
2
4
|
class Flag < ApplicationRecord
|
3
5
|
belongs_to :subject, polymorphic: true
|
4
6
|
|
5
7
|
validates :subject, :key, :value, presence: true
|
6
8
|
validates :key, uniqueness: { scope: :subject }
|
9
|
+
|
10
|
+
after_save :notify_subject_flag_has_changed, if: proc { |flag| flag.saved_changes.key?('value') }
|
11
|
+
def notify_subject_flag_has_changed
|
12
|
+
subject.flag_has_changed(key, value) if subject&.respond_to?(:flag_has_changed)
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def removing_duplicated_needed?
|
17
|
+
ActiveFlags::Flag.where(subject: subject, key: key).any?
|
18
|
+
end
|
19
|
+
|
20
|
+
def removing_duplicated_flags!
|
21
|
+
return false unless removing_duplicated_needed?
|
22
|
+
grouped = ActiveFlags::Flag
|
23
|
+
.where(subject_id: subject_id, subject_type: subject_type, key: key)
|
24
|
+
.group_by { |model| [model.key, model.subject_id, model.subject_type] }
|
25
|
+
grouped.values.each do |duplicates|
|
26
|
+
duplicates.shift
|
27
|
+
duplicates.each(&:destroy)
|
28
|
+
end
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
def converted_value
|
33
|
+
self.value = unstringify(value)
|
34
|
+
end
|
7
35
|
end
|
8
36
|
end
|
data/lib/active_flags.rb
CHANGED
@@ -21,7 +21,7 @@ module ActiveFlags
|
|
21
21
|
define_method(:flags) do
|
22
22
|
hash_of_flags = {}
|
23
23
|
flags_as_collection.each do |flag|
|
24
|
-
hash_of_flags[flag.key.to_sym] = flag.
|
24
|
+
hash_of_flags[flag.key.to_sym] = flag.converted_value
|
25
25
|
end
|
26
26
|
hash_of_flags.with_indifferent_access
|
27
27
|
end
|
@@ -33,15 +33,12 @@ module ActiveFlags
|
|
33
33
|
|
34
34
|
def method_missing(method_name, *args, &block)
|
35
35
|
return super unless method_name.to_s.include?(prefix = ACTIVE_FLAGS_PREFIX)
|
36
|
-
|
37
36
|
different_from = method_name.to_s.starts_with?('not')
|
38
37
|
flag = method_name.to_s.gsub(different_from ? "not_#{prefix}" : prefix, '')
|
39
|
-
|
38
|
+
result = joins(:flags_as_collection)
|
39
|
+
.where(active_flags_flags: { key: flag, value: stringify(args[0]) })
|
40
40
|
|
41
|
-
|
42
|
-
.where(active_flags_flags: { key: flag })
|
43
|
-
.send(different_from ? 'where' : 'all')
|
44
|
-
.send(different_from ? 'not' : 'where', condition)
|
41
|
+
different_from ? where.not(id: result.pluck(:id)) : result
|
45
42
|
end
|
46
43
|
end
|
47
44
|
end
|
@@ -8,8 +8,8 @@ module ActiveFlags
|
|
8
8
|
|
9
9
|
def save
|
10
10
|
flag = ActiveFlags::Flag.find_or_initialize_by(subject: @resource, key: @flag_attributes[:key])
|
11
|
+
flag.removing_duplicated_flags!
|
11
12
|
flag.update!(value: @flag_attributes[:value])
|
12
|
-
|
13
13
|
@resource.flags_as_collection << flag
|
14
14
|
end
|
15
15
|
end
|
data/lib/active_flags/version.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
def stringify(value)
|
2
|
-
if value.nil? || value == true
|
2
|
+
if value.nil? || value == true || value == 'true'
|
3
3
|
't'
|
4
|
-
elsif value == false
|
4
|
+
elsif value == false || value == 'false'
|
5
5
|
'f'
|
6
6
|
else
|
7
7
|
value
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
def unstringify(value)
|
12
|
+
if value == 't' || value == 'true'
|
13
|
+
true
|
14
|
+
elsif value == 'f' || value == 'false' || value == nil
|
15
|
+
false
|
16
|
+
else
|
17
|
+
value
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_flags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Huberty
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '0'
|
41
41
|
description: Easily declare flags for your models.
|
42
42
|
email:
|
43
43
|
- nathan.huberty@orange.fr
|
@@ -73,7 +73,7 @@ homepage: https://github.com/FidMe/active_flags
|
|
73
73
|
licenses:
|
74
74
|
- MIT
|
75
75
|
metadata: {}
|
76
|
-
post_install_message:
|
76
|
+
post_install_message:
|
77
77
|
rdoc_options: []
|
78
78
|
require_paths:
|
79
79
|
- lib
|
@@ -88,8 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
|
-
rubygems_version: 3.
|
92
|
-
signing_key:
|
91
|
+
rubygems_version: 3.1.2
|
92
|
+
signing_key:
|
93
93
|
specification_version: 4
|
94
94
|
summary: Easily declare flags for your models.
|
95
95
|
test_files: []
|