has_streak 0.0.1 → 0.0.2

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: 5bc92c9b28f09aebafeed5d5777934687b466eea
4
- data.tar.gz: 2b053c10a0644c46e3ced087631f91f3ee11705d
3
+ metadata.gz: 3addd5db918bed4cc4714e6d64767c6a3ea3d84f
4
+ data.tar.gz: e1949a658bef8b49596d4565ab5405fa84173c42
5
5
  SHA512:
6
- metadata.gz: 42453ff1e357a2ab4a802739284f753a41104174535a24a8818167d75ef4a9ff1a7589d8d110057925aafb620ac575782a66417d4e5ade4b8d4531abf375eb0d
7
- data.tar.gz: 2a78ab6fe71310a9e22a415581925d6719f16af35478f8804b28bd0d2f1b34fc37aaa6d65c2c79279e4e80c63f133db9a3eda45c35971199862ac62d099f973d
6
+ metadata.gz: 73dedc299b1b06899914fae3db6aa5ed95e8dde0daa9fdc33c1eee8610e9078cdc3bbb325393acf6c9e38018c220c629652f10d2814ee69177b8c9b56852e3e3
7
+ data.tar.gz: fed002fc0506f3c96cd21ed76af1a3ed57a2e1564dbb72942a66e6715cd05aa595f8723ed49d78c1b5d2ac384fe183775a00e8dc3ebeeaa131e5c2e8fb64545a
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # HasStreak
2
2
 
3
- TODO: Write a gem description
3
+ Track streaks on your ActiveRecord models.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,37 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Let's say I have a <code>User</code> that <code>has_many</code> posts:
22
+
23
+ ```ruby
24
+ class User < ActiveRecord::Base
25
+ has_many :posts
26
+ end
27
+ ```
28
+
29
+ I want to track how many days in a row that each user wrote a post. I just have to add <code>has_streak</code> to the model:
30
+
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ # ...
34
+
35
+ has_streak
36
+ end
37
+ ```
38
+
39
+ Now I can display the user's streak:
40
+
41
+ ```ruby
42
+ user.streak(:posts) # => number of days in a row that this user wrote a post.
43
+ ```
44
+
45
+ The <code>streak</code> instance method can be called with any association:
46
+
47
+ ```ruby
48
+ user.streak(:other_association)
49
+ ```
50
+
51
+ This gem assumes that the model you want to track has a <code>created_at</code> timestamp.
22
52
 
23
53
  ## Contributing
24
54
 
@@ -15,12 +15,12 @@ module HasStreak
15
15
  private
16
16
 
17
17
  def get_days_in_descending_order(association)
18
- self.send(association).order("created_at DESC").pluck(:created_at).map(&:day)
18
+ self.send(association).order(:created_at).pluck(:created_at).map(&:to_date).uniq
19
19
  end
20
20
 
21
21
  def determine_consecutive_days(days)
22
- days.each_with_index.inject(1) do |streak, (day, index)|
23
- streak += 1 if day - 1 == days[index + 1]
22
+ days.each_with_index.inject(0) do |streak, (day, index)|
23
+ streak += 1 if day + 1 == day.tomorrow
24
24
  streak
25
25
  end
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module HasStreak
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -5,11 +5,20 @@ describe HasStreak::Streakable do
5
5
  let(:user) { User.create(name: "garrett") }
6
6
 
7
7
  it "returns a streak of 3" do
8
- oldest = Post.create(content: "hello", user_id: user.id, created_at: 2.days.ago)
9
- old = Post.create(content: "hello", user_id: user.id, created_at: 1.day.ago)
10
- new = Post.create(content: "hello", user_id: user.id)
8
+ user.posts.create(content: "hello", created_at: 2.days.ago)
9
+ user.posts.create(content: "hello", created_at: 1.day.ago)
10
+ user.posts.create(content: "hello")
11
11
 
12
12
  expect(user.streak(:posts)).to eq(3)
13
13
  end
14
+
15
+ context "spanning two months" do
16
+ it "returns a streak of 2" do
17
+ user.posts.create(content: "hello", created_at: Date.new.end_of_month)
18
+ user.posts.create(content: "hello", created_at: Date.new.end_of_month.advance(days: 1))
19
+
20
+ expect(user.streak(:posts)).to eq(2)
21
+ end
22
+ end
14
23
  end
15
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_streak
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garrett Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-12 00:00:00.000000000 Z
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord