activerecord-reputation-system 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  ## ActiveRecord Reputation System [![Build Status](https://secure.travis-ci.org/twitter/activerecord-reputation-system.png)](http://travis-ci.org/twitter/activerecord-reputation-system) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/twitter/activerecord-reputation-system)
2
2
 
3
- The Active Record Reputation System helps you discover more about your application and make better decisions. The Reputation System gem makes it easy to integrate reputation systems into Rails applications, decouple the system from the main application and provide guidelines for good design of reputation systems.
3
+ The Active Record Reputation System helps you build the reputation system for your Rails application. It allows Active Record to have reputations and get evaluated by other records. This gem allows you to:
4
+ * define reputations in easily readable way.
5
+ * integrate reputation systems into applications and decouple the system from the main application.
6
+ * discover more about your application and make better decisions.
4
7
 
5
8
  ## Installation
6
9
 
@@ -26,14 +26,14 @@ module FinderMethods
26
26
  reputation_name, srn, find_scope, options = parse_query_args(*args)
27
27
  options[:select] = build_select_statement(table_name, reputation_name, options[:select])
28
28
  options[:joins] = build_join_statement(table_name, name, srn, options[:joins])
29
- options[:conditions] = build_condition_statement(options[:conditions])
29
+ options[:conditions] = build_condition_statement(reputation_name, options[:conditions])
30
30
  find(find_scope, options)
31
31
  end
32
32
 
33
33
  def count_with_reputation(*args)
34
34
  reputation_name, srn, find_scope, options = parse_query_args(*args)
35
35
  options[:joins] = build_join_statement(table_name, name, srn, options[:joins])
36
- options[:conditions] = build_condition_statement(options[:conditions])
36
+ options[:conditions] = build_condition_statement(reputation_name, options[:conditions])
37
37
  options[:conditions][0].gsub!(reputation_name.to_s, "COALESCE(rs_reputations.value, 0)")
38
38
  count(find_scope, options)
39
39
  end
@@ -42,7 +42,7 @@ module FinderMethods
42
42
  reputation_name, srn, find_scope, options = parse_query_args(*args)
43
43
  options[:select] = build_select_statement(table_name, reputation_name, options[:select], srn, true)
44
44
  options[:joins] = build_join_statement(table_name, name, srn, options[:joins])
45
- options[:conditions] = build_condition_statement(options[:conditions])
45
+ options[:conditions] = build_condition_statement(reputation_name, options[:conditions], srn, true)
46
46
  find(find_scope, options)
47
47
  end
48
48
 
@@ -50,7 +50,7 @@ module FinderMethods
50
50
  reputation_name, srn, find_scope, options = parse_query_args(*args)
51
51
  options[:select] = build_select_statement(table_name, reputation_name, options[:select])
52
52
  options[:joins] = build_join_statement(table_name, name, srn, options[:joins])
53
- options[:conditions] = build_condition_statement(options[:conditions])
53
+ options[:conditions] = build_condition_statement(reputation_name, options[:conditions])
54
54
  if respond_to?(:construct_finder_sql, true)
55
55
  construct_finder_sql(options)
56
56
  else
@@ -23,41 +23,33 @@ module ReputationSystem
23
23
  module ClassMethods
24
24
  DELTA = 0.000001
25
25
  REPUTATION_JOIN_STATEMENT = "LEFT JOIN rs_reputations ON %s.id = rs_reputations.target_id AND rs_reputations.target_type = ? AND rs_reputations.reputation_name = ? AND rs_reputations.active = ?"
26
+ REPUTATION_FIELD_STRING = "COALESCE(rs_reputations.value, 0)"
26
27
 
27
28
  def build_select_statement(table_name, reputation_name, select=nil, srn=nil, normalize=false)
28
29
  select = sanitize_sql_array(["%s.*", table_name]) unless select
29
30
  if normalize
30
- max = ReputationSystem::Reputation.max(srn, self.name)
31
- min = ReputationSystem::Reputation.min(srn, self.name)
32
- range = max - min
33
- if range < DELTA
34
- sanitize_sql_array(["%s, (0) AS normalized_%s", select, reputation_name])
35
- else
36
- sanitize_sql_array(["%s, ((rs_reputations.value - %s) / %s) AS normalized_%s", select, min, range, reputation_name])
37
- end
31
+ sanitize_sql_array(["%s, %s AS normalized_%s", select, normalized_field_string(srn), reputation_name.to_s])
38
32
  else
39
- sanitize_sql_array(["%s, COALESCE(rs_reputations.value, 0) AS %s", select, reputation_name])
33
+ sanitize_sql_array(["%s, %s AS %s", select, REPUTATION_FIELD_STRING, reputation_name.to_s])
40
34
  end
41
35
  end
42
36
 
43
37
  def build_select_statement_with_reputation_only(table_name, reputation_name, srn=nil, normalize=false)
44
38
  if normalize
45
- max = ReputationSystem::Reputation.max(srn, self.name)
46
- min = ReputationSystem::Reputation.min(srn, self.name)
47
- range = max - min
48
- if range < DELTA
49
- sanitize_sql_array(["(0) AS normalized_%s", reputation_name])
50
- else
51
- sanitize_sql_array(["((rs_reputations.value - %s) / %s) AS normalized_%s", min, range, reputation_name])
52
- end
39
+ sanitize_sql_array(["%s AS normalized_%s", normalized_field_string(srn), reputation_name.to_s])
53
40
  else
54
- sanitize_sql_array(["COALESCE(rs_reputations.value, 0) AS %s", reputation_name])
41
+ sanitize_sql_array(["%s AS %s", REPUTATION_FIELD_STRING, reputation_name.to_s])
55
42
  end
56
43
  end
57
44
 
58
- def build_condition_statement(conditions=nil)
45
+ def build_condition_statement(reputation_name, conditions=nil, srn=nil, normalize=false)
59
46
  conditions ||= [""]
60
47
  conditions = [conditions] unless conditions.is_a? Array
48
+ if normalize
49
+ normalized_reputation_name = sanitize_sql_array(["normalized_%s", reputation_name.to_s])
50
+ conditions[0] = conditions[0].gsub(normalized_reputation_name, normalized_field_string(srn))
51
+ end
52
+ conditions[0] = conditions[0].gsub(reputation_name.to_s, REPUTATION_FIELD_STRING)
61
53
  conditions
62
54
  end
63
55
 
@@ -68,6 +60,19 @@ module ReputationSystem
68
60
  rep_join = sanitize_sql_array([rep_join, table_name])
69
61
  joins << rep_join
70
62
  end
63
+
64
+ protected
65
+
66
+ def normalized_field_string(srn)
67
+ max = ReputationSystem::Reputation.max(srn, self.name)
68
+ min = ReputationSystem::Reputation.min(srn, self.name)
69
+ range = max - min
70
+ if range < DELTA
71
+ "(0)"
72
+ else
73
+ sanitize_sql_array(["((rs_reputations.value - %s) / %s)", min, range])
74
+ end
75
+ end
71
76
  end
72
77
  end
73
78
  end
@@ -22,6 +22,7 @@ module ReputationSystem
22
22
 
23
23
  module ClassMethods
24
24
  def with_reputation(*args)
25
+ warn "[DEPRECATION] `with_reputation` will be deprecated in version 3.0.0. Please use finder methods instead."
25
26
  reputation_name, srn = parse_arel_query_args(args)
26
27
  select = build_select_statement(table_name, reputation_name)
27
28
  joins = build_join_statement(table_name, name, srn)
@@ -29,6 +30,7 @@ module ReputationSystem
29
30
  end
30
31
 
31
32
  def with_reputation_only(*args)
33
+ warn "[DEPRECATION] `with_reputation_only` will be deprecated in version 3.0.0. Please use finder methods instead."
32
34
  reputation_name, srn = parse_arel_query_args(args)
33
35
  select = build_select_statement_with_reputation_only(table_name, reputation_name)
34
36
  joins = build_join_statement(table_name, name, srn)
@@ -36,6 +38,7 @@ module ReputationSystem
36
38
  end
37
39
 
38
40
  def with_normalized_reputation(*args)
41
+ warn "[DEPRECATION] `with_normalized_reputation` will be deprecated in version 3.0.0. Please use finder methods instead."
39
42
  reputation_name, srn = parse_arel_query_args(args)
40
43
  select = build_select_statement(table_name, reputation_name, nil, srn, true)
41
44
  joins = build_join_statement(table_name, name, srn)
@@ -43,6 +46,7 @@ module ReputationSystem
43
46
  end
44
47
 
45
48
  def with_normalized_reputation_only(*args)
49
+ warn "[DEPRECATION] `with_normalized_reputation_only` will be deprecated in version 3.0.0. Please use finder methods instead."
46
50
  reputation_name, srn = parse_arel_query_args(args)
47
51
  select = build_select_statement_with_reputation_only(table_name, reputation_name, srn, true)
48
52
  joins = build_join_statement(table_name, name, srn)
@@ -15,5 +15,5 @@
15
15
  ##
16
16
 
17
17
  module ReputationSystem
18
- VERSION = "2.0.0"
18
+ VERSION = "2.0.1"
19
19
  end
@@ -16,7 +16,7 @@
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe ActiveRecord::Base do
19
+ describe ReputationSystem::Base do
20
20
 
21
21
  before(:each) do
22
22
  @user = User.create!(:name => 'jack')
@@ -16,7 +16,7 @@
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe ActiveRecord::Base do
19
+ describe ReputationSystem::EvaluationMethods do
20
20
 
21
21
  before(:each) do
22
22
  @user = User.create!(:name => 'jack')
@@ -16,7 +16,7 @@
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe ActiveRecord::Base do
19
+ describe ReputationSystem::FinderMethods do
20
20
 
21
21
  before(:each) do
22
22
  @user = User.create!(:name => 'jack')
@@ -170,13 +170,13 @@ describe ActiveRecord::Base do
170
170
  sql = Question.find_with_reputation_sql(:total_votes, :all, {
171
171
  :select => "questions.*, users.name AS user_name",
172
172
  :joins => "JOIN users ON questions.author_id = users.id",
173
- :conditions => "total_votes > 0.6",
173
+ :conditions => "COALESCE(rs_reputations.value, 0) > 0.6",
174
174
  :order => "total_votes"})
175
175
  sql.should ==
176
176
  "SELECT questions.*, users.name AS user_name, COALESCE(rs_reputations.value, 0) AS total_votes "\
177
177
  "FROM \"questions\" JOIN users ON questions.author_id = users.id "\
178
178
  "LEFT JOIN rs_reputations ON questions.id = rs_reputations.target_id AND rs_reputations.target_type = 'Question' AND rs_reputations.reputation_name = 'total_votes' AND rs_reputations.active = 't' "\
179
- "WHERE (total_votes > 0.6) "\
179
+ "WHERE (COALESCE(rs_reputations.value, 0) > 0.6) "\
180
180
  "ORDER BY total_votes"
181
181
  end
182
182
  end
@@ -16,7 +16,7 @@
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe ActiveRecord::Base do
19
+ describe ReputationSystem::QueryMethods do
20
20
 
21
21
  before(:each) do
22
22
  @user = User.create!(:name => 'jack')
@@ -16,7 +16,7 @@
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe ActiveRecord::Base do
19
+ describe ReputationSystem::ReputationMethods do
20
20
 
21
21
  before(:each) do
22
22
  @user = User.create!(:name => 'jack')
@@ -16,7 +16,7 @@
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe ActiveRecord::Base do
19
+ describe ReputationSystem::ScopeMethods do
20
20
 
21
21
  before(:each) do
22
22
  @user = User.create!(:name => 'jack')
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: activerecord-reputation-system
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.0
5
+ version: 2.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Katsuya Noguchi