has-activity 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/has-activity.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "has-activity"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cary Dunn"]
12
- s.date = "2012-04-19"
12
+ s.date = "2012-05-02"
13
13
  s.description = "A simple way to grab recent activity on a given model grouped by hour, day, week or month (time series). Supports \"padding\" for intervals without activity."
14
14
  s.email = "cary.dunn@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -30,12 +30,18 @@ module HasActivity
30
30
  # :hour, :day, :week
31
31
  # * :order
32
32
  # :asc, :desc
33
+ # * :count_on
34
+ # default is "*". optionally put a custom aggregator ex "table.field".
35
+ # * :count_distinct
36
+ # default is false. optionally set to true for distinct counts on :count_on ex. "COUNT(DISTINCT table.field)".
33
37
  #
34
38
  def calculate_activity(between_start, between_end, options={})
35
39
  options[:padding] ||= true
36
40
  options[:order] ||= :asc
37
41
  options[:by] ||= :hour
38
42
  options[:on] ||= has_activity_options[:on]
43
+ options[:count_on] ||= "*"
44
+ options[:count_distinct] = false unless options[:count_distinct]
39
45
 
40
46
  # TODO: check for index on :on column
41
47
 
@@ -43,16 +49,9 @@ module HasActivity
43
49
  # TODO: review other possibilities for people to screw up the query...
44
50
  raise "You cannot use custom #group filters with #activity_since" if self.group_values.present?
45
51
 
46
- if options[:on].is_a?(String)
47
- # insert raw...
48
- activity_column = options[:on].split(".").collect { |v|
49
- connection.quote_column_name(v)
50
- }.join(".")
51
- else
52
- activity_table_name = connection.quote_table_name(self.table.name)
53
- activity_column_name = connection.quote_column_name(options[:on])
54
- activity_column = "#{activity_table_name}.#{activity_column_name}"
55
- end
52
+ activity_column = parse_activity_column(options[:on])
53
+ count_column = parse_activity_column(options[:count_on])
54
+ count_column = "DISTINCT(#{count_column})" if options[:count_distinct]
56
55
 
57
56
  activity_end_time = "'#{between_end.to_s(:db)}'"
58
57
 
@@ -60,7 +59,7 @@ module HasActivity
60
59
  when "hour"
61
60
  relation = self.select("
62
61
  #{activity_column} AS has_activity_timestamp,
63
- COUNT(*) AS has_activity_count,
62
+ COUNT(#{count_column}) AS has_activity_count,
64
63
  ((((YEAR(#{activity_end_time}) - YEAR(#{activity_column}))*365)+(DAYOFYEAR(#{activity_end_time})-DAYOFYEAR(#{activity_column})))*24)+(HOUR(#{activity_end_time})-HOUR(#{activity_column})) AS has_activity_hours_ago,
65
64
  CONCAT(YEAR(#{activity_column}), CONCAT(DAYOFYEAR(#{activity_column}), HOUR(#{activity_column}))) AS has_activity_uniqueness
66
65
  ")
@@ -69,7 +68,7 @@ module HasActivity
69
68
  when "day"
70
69
  relation = self.select("
71
70
  #{activity_column} AS has_activity_timestamp,
72
- COUNT(*) AS has_activity_count,
71
+ COUNT(#{count_column}) AS has_activity_count,
73
72
  DATEDIFF(#{activity_end_time}, #{activity_column}) AS has_activity_days_ago,
74
73
  CONCAT(YEAR(#{activity_column}), CONCAT(DAYOFYEAR(#{activity_column}))) AS has_activity_uniqueness
75
74
  ")
@@ -78,7 +77,7 @@ module HasActivity
78
77
  when "week"
79
78
  relation = self.select("
80
79
  #{activity_column} AS has_activity_timestamp,
81
- COUNT(*) AS has_activity_count,
80
+ COUNT(#{count_column}) AS has_activity_count,
82
81
  ((YEAR(#{activity_end_time}) - YEAR(#{activity_column}))*52)+(WEEK(#{activity_end_time})-WEEK(#{activity_column})) AS has_activity_weeks_ago,
83
82
  YEARWEEK(#{activity_column}) AS has_activity_uniqueness
84
83
  ")
@@ -87,7 +86,7 @@ module HasActivity
87
86
  when "month"
88
87
  relation = self.select("
89
88
  #{activity_column} AS has_activity_timestamp,
90
- COUNT(*) AS has_activity_count,
89
+ COUNT(#{count_column}) AS has_activity_count,
91
90
  ((YEAR(#{activity_end_time}) - YEAR(#{activity_column}))*12)+(MONTH(#{activity_end_time})-MONTH(#{activity_column})) AS has_activity_months_ago,
92
91
  CONCAT(YEAR(#{activity_column}), CONCAT(MONTH(#{activity_column}))) AS has_activity_uniqueness
93
92
  ")
@@ -159,6 +158,21 @@ module HasActivity
159
158
  padded_results
160
159
  end # pad_activity_results
161
160
 
161
+ def parse_activity_column(col)
162
+ if col.is_a?(String)
163
+ return "*" if col == "*" # Do not quote '*'
164
+ # insert raw...
165
+ activity_column = col.split(".").collect { |v|
166
+ connection.quote_column_name(v)
167
+ }.join(".")
168
+ else
169
+ activity_table_name = connection.quote_table_name(self.table.name)
170
+ activity_column_name = connection.quote_column_name(col)
171
+ activity_column = "#{activity_table_name}.#{activity_column_name}"
172
+ end
173
+ activity_column
174
+ end # parse_activity_column
175
+
162
176
  def round_activity_timestamp(t, round_to)
163
177
  #Time.at((t.to_f / 1.send(round_to)).floor * 1.send(round_to))
164
178
  if round_to == "weeks"
@@ -168,7 +182,7 @@ module HasActivity
168
182
  end
169
183
 
170
184
  Time.new(t.year, t.month, (%w(weeks days hours).include?(round_to) ? day : 1), (round_to == "hours" ? t.hour : 0), 0, 0, Time.zone.utc_offset)
171
- end
185
+ end # round_activity_timestamp
172
186
 
173
187
  end # OverallMethods
174
188
  end # ActivitizeCalculations
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has-activity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-19 00:00:00.000000000 Z
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
16
- requirement: &70135035018580 !ruby/object:Gem::Requirement
16
+ requirement: &70254072540360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70135035018580
24
+ version_requirements: *70254072540360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &70135035017920 !ruby/object:Gem::Requirement
27
+ requirement: &70254072539680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.12'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70135035017920
35
+ version_requirements: *70254072539680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &70135035017160 !ruby/object:Gem::Requirement
38
+ requirement: &70254072539000 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70135035017160
46
+ version_requirements: *70254072539000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &70135035016280 !ruby/object:Gem::Requirement
49
+ requirement: &70254069044600 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.8.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70135035016280
57
+ version_requirements: *70254069044600
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &70135034768360 !ruby/object:Gem::Requirement
60
+ requirement: &70254069043860 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70135034768360
68
+ version_requirements: *70254069043860
69
69
  description: A simple way to grab recent activity on a given model grouped by hour,
70
70
  day, week or month (time series). Supports "padding" for intervals without activity.
71
71
  email: cary.dunn@gmail.com
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  segments:
106
106
  - 0
107
- hash: 2987300255728733323
107
+ hash: -4197150481822050296
108
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  none: false
110
110
  requirements: