active_flags 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37e82dc8c41afadca68b93d26556c7306fe4cd85ea13dc1ec0186ea616d3df7d
4
- data.tar.gz: 54afcdf0ee74fce3c33b7b87fd5321f35e1e0e5d6a2c2a85945fc1a86a764585
3
+ metadata.gz: 04d4f132fab19c0376a2a86331ebc79c309dae8e8e6a3de21a5f73295af63646
4
+ data.tar.gz: b1f08e89de858e78adb8713a2fdee131801db75aeb5abc265918a0f2b5d78d07
5
5
  SHA512:
6
- metadata.gz: 8576103bbda00fadd4a9e3a3c0a0c6c251008e5ba5d04500d83385dd824602a3cf2db16f8f53102d21aab958f7b5231835a2162d1a33f334fec885ad865a8ee7
7
- data.tar.gz: d68b0707f7c9b53546333dcd4e25dd6bd3f8dfc478e815c0fba3cca49495414afcb57d3c9afa6f5724fac3618a0a140afc0150e86c6a579499cad84fe1df3657
6
+ metadata.gz: 0adf7d4c13094ba72e1b168e1398310c46c1e33346ffc9aa49e82366ed811cd18861099b7346d2bc42a3772b1577fc16b6533a3a51a6422d0308b6d99b62640c
7
+ data.tar.gz: 44b07703f575acebcbd4af7c17f2b5101cdc118293e6c425cfd1298b5d18f5b265c8cd84c27b191958a376917f36ed01b0ffd4bcec9abc871110c497f5d513c1
data/README.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # ActiveFlags
2
+
3
+ [![Build Status](https://travis-ci.org/FidMe/active_flags.svg?branch=master)](https://travis-ci.org/FidMe/active_flags)
4
+ [![Gem Version](https://badge.fury.io/rb/active_flags.svg)](https://badge.fury.io/rb/active_flags)
5
+
6
+
2
7
  Active Flags allows you to use automatic flags on models. You won't have to create useless booleans with view logic in your core tables. They should not have been there in the first place, let Active Flags handle them for you!
3
8
 
4
9
  ## Installation
@@ -49,7 +54,7 @@ And that's it!
49
54
  You can now add flags on a user like that:
50
55
 
51
56
  ```ruby
52
- user.flags = { visible: 'true', active: 'true' }
57
+ user.flags = { visible: true, active: true }
53
58
  user.save!
54
59
  ```
55
60
 
@@ -65,12 +70,50 @@ end
65
70
 
66
71
  And then you can declare as much flags as you want with no restriction:
67
72
  ```ruby
68
- user.update!(flags: { visible: 'true', active: 'true', diet: 'vegan', power: 'super saiyan' })
73
+ user.update!(flags: { visible: true, active: true, diet: 'vegan', power: 'super saiyan' })
69
74
  ```
70
75
 
71
76
  To access your flags, you now have 2 ways.
72
77
  Either as a hash, with the `flags` method or as an ActiveFlag::Flags collection with the `flags_as_collection` method.
73
78
 
79
+ ## Flags as scopes
80
+
81
+ When you develop an app without active_flags, you will generally query the equivalent of flags as simple booleans.
82
+
83
+ ActiveFlags gives you a clean and simple way to query your model based on defined flags.
84
+
85
+ Any flag can be queried as a scope using the `flagged_as` method
86
+
87
+ ```ruby
88
+ user = User.create!(flags: { visible: true })
89
+
90
+ User.flagged_as_visible
91
+ # #<ActiveRecord::Relation [#<User id: 1>]>
92
+
93
+ User.flagged_as_visible(false)
94
+ # #<ActiveRecord::Relation []>
95
+
96
+ User.flagged_as_intelligent
97
+ # #<ActiveRecord::Relation []>
98
+
99
+ user.update!(flags: { intelligent: true })
100
+ User.flagged_as_intelligent
101
+ # #<ActiveRecord::Relation [#<User id: 1>]>
102
+
103
+ user.update!(flags: { intelligent: 'a bit' })
104
+ User.flagged_as_intelligent('a_bit')
105
+ # #<ActiveRecord::Relation [#<User id: 1>]>
106
+ ```
107
+
108
+ To query flags the other way around you can use the `not_flagged_as` method
109
+
110
+ ```ruby
111
+ User.not_flagged_as_intelligent
112
+ # or with value
113
+ User.not_flagged_as_intelligent('a bit')
114
+ ```
115
+
116
+
74
117
  ## Contributing
75
118
  https://github.com/FidMe/active_flags
76
119
 
data/lib/active_flags.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "active_flags/engine"
2
2
  require_relative 'active_flags/handler/flag_mapper'
3
3
  require_relative 'active_flags/handler/flag_builder'
4
+ require_relative 'utils/value_stringifier'
4
5
 
5
6
  module ActiveFlags
6
7
  extend ActiveSupport::Concern
@@ -23,5 +24,17 @@ module ActiveFlags
23
24
  hash_of_flags.with_indifferent_access
24
25
  end
25
26
  end
27
+
28
+ def method_missing(method_name, *args)
29
+ super(method_name, *args) unless method_name.to_s.include?(prefix = 'flagged_as_')
30
+ different_from = method_name.to_s.starts_with?('not')
31
+ flag = method_name.to_s.gsub(different_from ? "not_#{prefix}" : prefix, '')
32
+ condition = { active_flags_flags: { value: stringify(args[0]) } }
33
+
34
+ joins(:flags_as_collection)
35
+ .where(active_flags_flags: { key: flag })
36
+ .send(different_from ? 'where' : 'all')
37
+ .send(different_from ? 'not' : 'where', condition)
38
+ end
26
39
  end
27
- end
40
+ end
@@ -7,9 +7,9 @@ module ActiveFlags
7
7
  end
8
8
 
9
9
  def save
10
- @resource.flags_as_collection << ActiveFlags::Flag.find_or_initialize_by(subject: @resource, key: @flag_attributes[:key]) do |flag_to_create|
11
- flag_to_create.value = @flag_attributes[:value]
12
- end
10
+ ActiveFlags::Flag.find_or_initialize_by(subject: @resource, key: @flag_attributes[:key])
11
+ .update!(value: @flag_attributes[:value])
12
+ @resource.reload
13
13
  end
14
14
  end
15
15
  end
@@ -2,7 +2,6 @@ module ActiveFlags
2
2
  module Handler
3
3
  module FlagMappers
4
4
  class KeyValueMapper
5
-
6
5
  def initialize(hash_to_map)
7
6
  @hash_to_map = hash_to_map
8
7
  end
@@ -19,4 +18,4 @@ module ActiveFlags
19
18
  end
20
19
  end
21
20
  end
22
- end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveFlags
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -0,0 +1,9 @@
1
+ def stringify(value)
2
+ if value.nil? || value == true
3
+ 't'
4
+ elsif value == false
5
+ 'f'
6
+ else
7
+ value
8
+ end
9
+ 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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Huberty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2019-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -68,6 +68,7 @@ files:
68
68
  - lib/active_flags/handler/flag_mappers/key_value_mapper.rb
69
69
  - lib/active_flags/version.rb
70
70
  - lib/tasks/active_flags_tasks.rake
71
+ - lib/utils/value_stringifier.rb
71
72
  homepage: https://github.com/FidMe/active_flags
72
73
  licenses:
73
74
  - MIT