mongoid_flaggable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +190 -0
- data/lib/mongoid_flaggable/instance_methods.rb +4 -2
- data/lib/mongoid_flaggable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac3936d7fd6a4958b693caf93869747a2d927496
|
4
|
+
data.tar.gz: e8df29c37f1cfd3aeadbddb4f4ce4fad1c8f586c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f26b1caad5c340a21d98bf55e12c715c8f55f46a3fa7fe52eda63403753f8ceaf7c77de4befaa44fa9fac7585a4005de0ac9c3d2cd5b61940e745a1aac032cec
|
7
|
+
data.tar.gz: 8aa8dbfffd91ee8c5674b723d4ed88e9b7d4f8a63c2df89634c3129b9b2cf059deb1d6b5a149d3d1a3010bf366edcdfec63ebfb4d583fbb1a29771907ce6e84c
|
data/README.md
CHANGED
@@ -0,0 +1,190 @@
|
|
1
|
+
mongoid_flaggable [![Gem Version](https://badge.fury.io/rb/mongoid_flaggable.png)](http://badge.fury.io/rb/mongoid_flaggable) [![Code Climate](https://codeclimate.com/github/mchail/mongoid_flaggable.png)](https://codeclimate.com/github/mchail/mongoid_flaggable) [![Build Status](https://travis-ci.org/mchail/mongoid_flaggable.png?branch=master)](https://travis-ci.org/mchail/mongoid_flaggable)
|
2
|
+
==============
|
3
|
+
|
4
|
+
mongoid_flaggable is a lightweight and powerful way to add flags/tags (boolean values) to your mongoid models.
|
5
|
+
|
6
|
+
# Installation
|
7
|
+
|
8
|
+
Install directly from rubygems:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem install mongoid_flaggable
|
12
|
+
```
|
13
|
+
|
14
|
+
Or if you are using a Gemfile, place this in your Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'mongoid_flaggable'
|
18
|
+
```
|
19
|
+
|
20
|
+
# Use cases
|
21
|
+
|
22
|
+
This gem was created to give developers an easy way to flag mongoid models with boolean values. Flags are well-suited for temporary boolean values that do not merit the overhead of an additional field on the model and in the database. This includes gradual feature rollouts (whitelisting certain users for experimental features) and temporary metadata useful for analytics. Flags are *not* well-suited for data you intend to persist on your documents long-term.
|
23
|
+
|
24
|
+
# Configure a model to be flaggable
|
25
|
+
|
26
|
+
One line of code is needed to set up a model with mongoid_flaggable.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Book
|
30
|
+
include Mongoid::Document
|
31
|
+
include Mongoid::Flaggable # it's this one
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Be sure to run `rake db:mongoid:create_indexes` after adding mongoid_flaggable to a model. This will create an index on the `flag_array` field it makes use of.
|
36
|
+
|
37
|
+
# Basic usage
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
book = Book.create
|
41
|
+
book.flags #=> []
|
42
|
+
book.add_flag!(:out_of_print)
|
43
|
+
book.flags #=> ["out_of_print"]
|
44
|
+
book.flag?(:out_of_print) #=> true
|
45
|
+
book.flag?(:florinese_translation) #=> false
|
46
|
+
book.remove_flag!(:out_of_print)
|
47
|
+
book.flags #=> []
|
48
|
+
```
|
49
|
+
|
50
|
+
# API
|
51
|
+
|
52
|
+
- All methods accept `"strings"` or `:symbols`. The arguments are cast to strings before being used.
|
53
|
+
- Methods that end in a `!` will immediately write to the database. Methods without the bang will make updates in memory (if the intention of the method is to update data), but will not persist the changes (i.e. after calling `book.add_flag(:out_of_print)`, you must call `book.save` to persist the new flag).
|
54
|
+
|
55
|
+
### Instance Methods
|
56
|
+
|
57
|
+
Add a flag to a specific model **without saving**
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
book.add_flag(:out_of_print)
|
61
|
+
```
|
62
|
+
|
63
|
+
Add a flag to a specific model **and save immediately**
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
book.add_flag!(:out_of_print)
|
67
|
+
```
|
68
|
+
|
69
|
+
Remove a flag from a specific model **without saving**
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
book.remove_flag(:out_of_print)
|
73
|
+
```
|
74
|
+
|
75
|
+
Remove a flag from a specific model **and save immediately**
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
book.remove_flag!(:out_of_print)
|
79
|
+
```
|
80
|
+
|
81
|
+
Clear all flags from a specific model **without saving**
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
book.clear_flags
|
85
|
+
```
|
86
|
+
|
87
|
+
Clear all flags from a specific model **and save immediately**
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
book.clear_flags!
|
91
|
+
```
|
92
|
+
|
93
|
+
Get array of flags from a model - guaranteed to return an array or zero or more strings
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
book.flags # => ["out_of_print"]
|
97
|
+
```
|
98
|
+
|
99
|
+
Test for the presence of a flag
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
book.flag?(:out_of_print)
|
103
|
+
```
|
104
|
+
|
105
|
+
Test for the presence of **any** of multiple flags
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
book.any_flags?(:out_of_print, :florinese_translation)
|
109
|
+
```
|
110
|
+
|
111
|
+
Test for the presence of **all** of multiple flags
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
book.all_flags?(:out_of_print, :florinese_translation)
|
115
|
+
```
|
116
|
+
|
117
|
+
### Class Methods
|
118
|
+
|
119
|
+
- All finders return a `Mongoid::Criteria` that may be chained with additional clauses (e.g. `where`, `limit`, `skip`, `order_by`, etc.)
|
120
|
+
|
121
|
+
Retrieve documents with a given flag
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
Book.by_flag(:out_of_print)
|
125
|
+
```
|
126
|
+
|
127
|
+
Retrieve documents with **any** of multiple flags
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
Book.by_any_flags(:out_of_print, :florinese_translation)
|
131
|
+
```
|
132
|
+
|
133
|
+
Retrieve documents with **all** of multiple flags
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
Book.by_all_flags(:out_of_print, :florinese_translation)
|
137
|
+
```
|
138
|
+
|
139
|
+
Get the number of documents with a given flag
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
Book.flag_count(:out_of_print)
|
143
|
+
```
|
144
|
+
|
145
|
+
Get the number of documents with **all** of multiple flags
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
Book.flag_count(:out_of_print, :florinese_translation)
|
149
|
+
```
|
150
|
+
|
151
|
+
Get an array of all the distinct flags in use on the collection
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
Book.distinct_flags
|
155
|
+
```
|
156
|
+
|
157
|
+
Add a flag to multiple documents matching given criteria
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
Book.bulk_add_flag!(:out_of_print, {:last_printed_at.lt => 20.years.ago})
|
161
|
+
```
|
162
|
+
|
163
|
+
Remove a flag from multiple documents matching given criteria
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
Book.bulk_remove_flag!(:out_of_print, {author_name: "William Goldman"})
|
167
|
+
```
|
168
|
+
|
169
|
+
Remove a flag from all documents
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
Book.bulk_remove_flag!(:out_of_print)
|
173
|
+
```
|
174
|
+
|
175
|
+
Get a sorted frequency hash of all flags used on a collection
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
Book.flag_frequency #=> {"out_of_print" => 20, "florinese_translation" => 5}
|
179
|
+
```
|
180
|
+
|
181
|
+
# Contributing
|
182
|
+
|
183
|
+
1. Fork it
|
184
|
+
2. Make your changes
|
185
|
+
3. Write/update tests. Run with `rake`.
|
186
|
+
4. Issue a Pull Request
|
187
|
+
|
188
|
+
# License
|
189
|
+
|
190
|
+
MIT. Go nuts.
|
@@ -8,7 +8,8 @@ module Mongoid
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def add_flag!(flag)
|
11
|
-
|
11
|
+
add_flag(flag)
|
12
|
+
save
|
12
13
|
end
|
13
14
|
|
14
15
|
def remove_flag(flag)
|
@@ -17,7 +18,8 @@ module Mongoid
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def remove_flag!(flag)
|
20
|
-
|
21
|
+
remove_flag(flag)
|
22
|
+
save
|
21
23
|
end
|
22
24
|
|
23
25
|
def clear_flags
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_flaggable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve McHail
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|