popularable 1.2.0 → 1.4.1

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
- SHA1:
3
- metadata.gz: 7e979b1fa4cea6c06ffe15b203b29a79457c68f2
4
- data.tar.gz: 240ec42fce12117ddaa1d22d42958013b735a336
2
+ SHA256:
3
+ metadata.gz: '064497b0e513cb12f5576493808768c138dc33ad745b23c5b7091c45ac078987'
4
+ data.tar.gz: ea65be967f9c675cdff2bea3f0ecbf6364e9642cc0311728d87c56a0ec1e41f6
5
5
  SHA512:
6
- metadata.gz: 271bb74de44fb2bd37196347347aae4a6fb13e7d3697ff0fb433488a5b424f61e70ee4004fbb0885d3fa8888eaff7119a838e591b464993587ac76e7c9b2104c
7
- data.tar.gz: 713c2e4266b42a12133d87ad966f90b4a430e1abff297172e94826711740e5b6eed6361eb71297e522617582e48addb71b0e3570a6bd8d4dcfb7e6f68ecdca8a
6
+ metadata.gz: 2c911cac3ce70dea9760a520296c2c538655371047028dff2094cb4710eb0362790366549f419633a57b33fe89d4737a577b43911882cb9c802956ba032dca5b
7
+ data.tar.gz: 043eb3b48552d0e2d70fc4af001e3570e002419158915a2bfe9a87f94a6ae0d427238bb0f0edcec7acc8df22ceea834f026c5e6519b1e98fa207325b111180ea
@@ -5,28 +5,29 @@ module Popularable
5
5
  included do
6
6
  # scope :order_by_recent_popularity, -> { .joins( "LEFT OUTER JOIN popularable_popularity_events ON (" + self.to_s.pluralize.underscore + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "')").where( ["popularable_popularity_events.popularity_event_date >= ?", 1.month.ago] ).group( self.to_s.pluralize.underscore + ".id" ).order( "popularity DESC" ) }
7
7
 
8
- scope :popular_today, -> {
8
+ scope :popular_today, -> {
9
9
  popular_since( Time.now.beginning_of_day )
10
10
  }
11
11
 
12
- scope :popular_this_week, -> {
12
+ scope :popular_this_week, -> {
13
13
  popular_since( Time.now.beginning_of_week )
14
14
  }
15
-
16
- scope :popular_this_month, -> {
15
+
16
+ scope :popular_this_month, -> {
17
17
  popular_since( Time.now.beginning_of_month )
18
18
  }
19
-
20
- scope :popular_this_year, -> {
19
+
20
+ scope :popular_this_year, -> {
21
21
  popular_since( Time.now.beginning_of_year )
22
22
  }
23
23
 
24
- scope :popular_all_time, -> {
25
- popular_since( Time.now - 100.years )
24
+ scope :popular_all_time, -> {
25
+ # This uses an inner join and ditches the unnecessary where clause for performance.
26
+ select( "#{self.table_name}.*, 0 + SUM(popularable_popularity_events.popularity) AS popularity").joins( "INNER JOIN popularable_popularity_events ON (#{self.table_name}.id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '#{self.name}')").group( "#{self.table_name}.id" ).order( "popularity DESC" )
26
27
  }
27
-
28
+
28
29
  scope :popular_since, -> (since){
29
- select( "#{self.table_name}.*, 0 + SUM(popularable_popularity_events.popularity) AS popularity").joins( "LEFT OUTER JOIN popularable_popularity_events ON (#{self.table_name}.id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '#{self.to_s}')").where( "popularable_popularity_events.popularity_event_date >= ?", since.to_date ).group( "#{MediaItem.table_name}.id" ).order( "popularity DESC" )
30
+ select( "#{self.table_name}.*, 0 + SUM(popularable_popularity_events.popularity) AS popularity").joins( "LEFT OUTER JOIN popularable_popularity_events ON (#{self.table_name}.id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '#{self.name}')").where( "popularable_popularity_events.popularity_event_date >= ?", since.to_date ).group( "#{self.table_name}.id" ).order( "popularity DESC" )
30
31
  }
31
32
 
32
33
  has_many :popularable_popularity_events, as: :popularable
@@ -35,12 +36,37 @@ module Popularable
35
36
  true
36
37
  end
37
38
 
39
+ # Count methods
40
+ def popular_count_today
41
+ popular_count_since(Time.now.beginning_of_day)
42
+ end
43
+
44
+ def popular_count_this_week
45
+ popular_count_since(Time.now.beginning_of_week)
46
+ end
47
+
48
+ def popular_count_this_month
49
+ popular_count_since(Time.now.beginning_of_month)
50
+ end
51
+
52
+ def popular_count_this_year
53
+ popular_count_since(Time.now.beginning_of_year)
54
+ end
55
+
56
+ def popular_count_all_time
57
+ popular_count_since(Time.now - 100.years)
58
+ end
59
+
60
+ def popular_count_since(since = Time.now)
61
+ popularable_popularity_events.where( "popularable_popularity_events.popularity_event_date >= ?", since.to_date ).sum( :popularity )
62
+ end
63
+
38
64
  end
39
65
 
40
66
  def bump_popularity!( popularity_add_value = 1, popularity_event_time = Time.now )
41
67
  popularable_popularity_event = self.popularable_popularity_events.find_or_create_by( popularity_event_date: popularity_event_time.to_date )
42
68
 
43
- popularable_popularity_event.update_attributes( popularity: popularable_popularity_event.popularity.to_i + popularity_add_value )
69
+ popularable_popularity_event.update( popularity: popularable_popularity_event.popularity.to_i + popularity_add_value )
44
70
  end
45
71
  end
46
- end
72
+ end
@@ -1,5 +1,5 @@
1
1
  module Popularable
2
2
  module Rails
3
- VERSION = "1.2.0"
3
+ VERSION = "1.4.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: popularable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff McFadden
@@ -76,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubyforge_project:
80
- rubygems_version: 2.5.1
79
+ rubygems_version: 3.0.3
81
80
  signing_key:
82
81
  specification_version: 4
83
82
  summary: Organize your models by a historical popularity value