voltaire 0.3.0 → 0.4.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c28cec632b79a5fcac75a29d2fbec19aaa9fd51e
4
- data.tar.gz: 29e6a75c640ac2e8134a54608486a4f2033fe510
3
+ metadata.gz: edd5229303a45a052c3f1711d00a8fa4fed9af37
4
+ data.tar.gz: 49ca0737a96675267c8b45cc33b9ad1ade3ca32c
5
5
  SHA512:
6
- metadata.gz: 93fdff51da9b305f2a5bf4b7c1516c653535b9b8caf0c82c6270cba87d0438a56f36f5bb169a344d04012d339de846c8ac4085973a7eabbf800629c6a45add1d
7
- data.tar.gz: 2049b6be8ac30566be38d6c77e27adb1e7f7c66e2af8bf0277a03a9513fc9b8b74adc90673e5c29427dcd0a56f9a8b267d77ecb2ac900397c805f628c3a30228
6
+ metadata.gz: 2b7d9aae3f1568e5b16153ace6e81b9cbae25e8c8c066e36116e6330eb71d67866984b130a18f1c07c645f8ac083e7292de5f03f84d36ed22614ff46aa453988
7
+ data.tar.gz: 0ff7118f318fb22c6af52bb65afb84445544ac9b537c6864cf6246247f9698256d3ce6cb8789410e3c850c12243c9f0b980bec855f99a2be59446a5c1ee7cbf5
data/README.md CHANGED
@@ -12,7 +12,7 @@ All you have to do is add a column for reputation in your users table and let Vo
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'voltaire', '~> 0.3.0'
15
+ gem 'voltaire', '~> 0.4.5'
16
16
  ```
17
17
 
18
18
  And then execute:
@@ -43,14 +43,28 @@ rails db:migrate
43
43
 
44
44
  ## Implementing Voltaire's Powerful Mechanism
45
45
 
46
- Now you're ready to roll. Voltaire has two methods you can call to increase or decrease the user's reputation score.
47
- It requires 3 arguments: amount (the amount you want to increase or decrease by), reputation (the database column
48
- you want to alter), and user (the user or item whose points will be increased).
46
+ Now you're ready to roll. Voltaire has a couple different ways you can increase or decrease the user's reputation score. One method
47
+ is incremental, and is intended for high-volume actions that increase scores by smaller amounts.
49
48
 
50
- _Note: if you are using any model other than users, you will need to use the_ ```voltaire_up_other``` and ```voltaire_down_other``` _methods_
49
+ _Note that with any of the minus/plus
50
+ methods, the record is updated by 1 for whatever amount you specify, so if you put that you want a user's score to increase by 100 for
51
+ something, the database will be hit 100 times. For larger amounts, use the up/down methods instead._
52
+
53
+ To recap:
54
+ - incrementing and decrementing by 1 or a small number, use the _plus_ or _minus_ methods below; _this way guards against concurrency_
55
+ - increasing and decreasing by larger amounts for rarer actions or if you don't care about concurrency, user _up_ and _down_ methods below
56
+
57
+ Whichever of the two ways you decide to use, the parameters are the same. Each method takes 3 arguments:
58
+ * _amount_ (the amount you want to increase or decrease by)
59
+ * _reputation_ (the database column you want to alter)
60
+ * _user_ (the user or item whose points will be increased)
61
+
62
+ _Note: if you are using any model other than users, you will need to use the_ ```voltaire_plus_other``` , ```voltaire_minus_other```, ```voltaire_up_other``` and ```voltaire_down_other``` _methods_
51
63
  _(instructions farther down)._
52
64
 
53
- The two methods are
65
+
66
+ ## Larger Amounts
67
+ Use ```voltaire_up``` and ```voltaire_down``` methods for larger amounts and if you don't worry about concurrency issues.
54
68
 
55
69
  ```
56
70
  voltaire_up(amount, reputation, user)
@@ -60,6 +74,21 @@ and
60
74
  ```
61
75
  voltaire_down(amount, reputation, user)
62
76
  ```
77
+
78
+ ## Increments/Decrements of 1 or Smaller Amounts
79
+ Use ```voltaire_plus``` and ```voltaire_minus``` methods for smaller amounts or if have concurrency issues. These will hit the database
80
+ multiple times.
81
+
82
+ ```
83
+ voltaire_plus(amount, reputation, user)
84
+ ```
85
+ and
86
+
87
+ ```
88
+ voltaire_minus(amount, reputation, user)
89
+ ```
90
+
91
+ # Implementation and Examples
63
92
  To implement it, simply call the method you want in your controller and pass in the parameters.
64
93
 
65
94
  ## Examples
@@ -67,20 +96,21 @@ To implement it, simply call the method you want in your controller and pass in
67
96
  Here is an implementation of the [acts_as_votable](https://github.com/ryanto/acts_as_votable) gem, which allows users to
68
97
  upvote or downvote comments. In the comments_controller.rb file, we pass in our method where we want Voltaire to go to
69
98
  town. In the example below, when a user upvotes a comment, the user who made the comment will have their _karma_ increase
70
- by 1, as karma is the database column in this example.
99
+ by 1, as karma is the database column in this example. Because it is incrementing and decrementing by 1, and because lots of
100
+ users can potentially upvote and downvote simultaneously, we are using the concurrent safe versions:
71
101
 
72
102
  _comments_controller.rb_
73
103
 
74
104
  ```ruby
75
105
  def upvote
76
106
  @comment.upvote_by current_user
77
- voltaire_up(1, :karma, @comment.user_id)
107
+ voltaire_plus(1, :karma, @comment.user_id)
78
108
  redirect_to :back
79
109
  end
80
110
 
81
111
  def downvote
82
112
  @comment.downvote_by current_user
83
- voltaire_down(1, :karma, @comment.user_id)
113
+ voltaire_minus(1, :karma, @comment.user_id)
84
114
  redirect_to :back
85
115
  end
86
116
  ```
@@ -102,7 +132,8 @@ _index.html.erb_
102
132
 
103
133
  ## More Examples
104
134
  Here we have set up an easy way to toggle an article and make it featured. Any time a user's article gets featured, we have
105
- Voltaire increase their _reputation_ by 20 points.
135
+ Voltaire increase their _reputation_ by 250 points. Because an article being featured is a rarer occasion, and because we don't
136
+ feel like hitting our database 250 times for this, we use the up/down method.
106
137
 
107
138
  _articles_controller.rb_
108
139
 
@@ -139,7 +170,7 @@ def create
139
170
  end
140
171
  ```
141
172
 
142
- ## Something Besides Users
173
+ # Something Besides Users
143
174
  If you want to implement a score system on some other than users, you will need to pass that in to the method as a fourth parameter.
144
175
  In the example below, there is a World class for an app that helps writers create new worlds. If we implement a scoring
145
176
  system on the world, we can easily see which ones are more fleshed out. The methods in this case will look like this:
@@ -148,6 +179,10 @@ system on the world, we can easily see which ones are more fleshed out. The meth
148
179
  voltaire_up_other(amount, reputation, user, other)
149
180
 
150
181
  voltaire_down_other(amount, reputation, user, other)
182
+
183
+ voltaire_plus_other(amount, reputation, user, other)
184
+
185
+ voltaire_minus_other(amount, reputation, user, other)
151
186
  ```
152
187
 
153
188
  The fourth parameter, other, indicates the class. In this case, it would be ```World```. _This parameter needs to be uppercase._
@@ -159,7 +194,7 @@ The fourth parameter, other, indicates the class. In this case, it would be ```W
159
194
 
160
195
  respond_to do |format|
161
196
  if @city.save
162
- voltaire_up_other(1, :score, @city.world_id, World)
197
+ voltaire_plus_other(1, :score, @city.world_id, World)
163
198
  format.html { redirect_to @city, notice: 'City was successfully created.' }
164
199
  else
165
200
  format.html { render :new }
@@ -1,25 +1,51 @@
1
1
  require "voltaire/version"
2
2
 
3
3
  module Voltaire
4
+ # FOR MORE THAN INCREMENTING BY 1
4
5
  def voltaire_up(amount, reputation, user)
6
+ get_user = User.find_by(id: user)
7
+ get_user.reputation += amount
8
+ get_user.save
9
+ end
10
+
11
+ def voltaire_down(amount, reputation, user)
12
+ get_user = User.find_by(id: user)
13
+ get_user.reputation -= amount
14
+ get_user.save
15
+ end
16
+
17
+ def voltaire_up_other(amount, reputation, user, other)
18
+ get_other = other.find_by(id: user)
19
+ get_other.reputation += amount
20
+ get_other.save
21
+ end
22
+
23
+ def voltaire_down_other(amount, reputation, user, other)
24
+ get_other = other.find_by(id: user)
25
+ get_other.reputation -= amount
26
+ get_other.save
27
+ end
28
+
29
+ # FOR INCREMENTING
30
+ def voltaire_plus(amount, reputation, user)
5
31
  amount.times.collect do
6
32
  User.increment_counter(reputation, user)
7
33
  end
8
34
  end
9
35
 
10
- def voltaire_down(amount, reputation, user)
36
+ def voltaire_minus(amount, reputation, user)
11
37
  amount.times.collect do
12
38
  User.decrement_counter(reputation, user)
13
39
  end
14
40
  end
15
41
 
16
- def voltaire_up_other(amount, reputation, user, other)
42
+ def voltaire_plus_other(amount, reputation, user, other)
17
43
  amount.times.collect do
18
44
  other.increment_counter(reputation, user)
19
45
  end
20
46
  end
21
47
 
22
- def voltaire_down_other(amount, reputation, user, other)
48
+ def voltaire_minus_other(amount, reputation, user, other)
23
49
  amount.times.collect do
24
50
  other.decrement_counter(reputation, user)
25
51
  end
@@ -1,3 +1,3 @@
1
1
  module Voltaire
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voltaire
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Donche
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-26 00:00:00.000000000 Z
11
+ date: 2017-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler