merit 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- merit (0.9.4)
4
+ merit (0.9.5)
5
5
  ambry (~> 0.3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -4,11 +4,6 @@
4
4
 
5
5
  [![Build Status](https://secure.travis-ci.org/tute/merit.png?branch=master)](http://travis-ci.org/tute/merit)
6
6
 
7
-
8
- # Requirements
9
-
10
- * ActiveRecord or Mongoid
11
-
12
7
  # Installation
13
8
 
14
9
  1. Add `gem 'merit'` to your `Gemfile`
@@ -79,9 +74,7 @@ method(s) defined in the `:to` option. Define rules on
79
74
  ## Examples
80
75
 
81
76
  ```ruby
82
- score 10, :on => [
83
- 'users#update'
84
- ]
77
+ score 10, :to => :post_creator, :on => 'comments#create'
85
78
 
86
79
  score 20, :on => [
87
80
  'comments#create',
@@ -145,6 +138,7 @@ end
145
138
  * :value parameter (for star voting for example) should be configurable
146
139
  (depends on params[:value] on the controller).
147
140
  * Make fixtures for integration testing (now creating objects on test file!).
141
+ * Rules should be cached? Calling *Rules.new more than once
148
142
 
149
143
  ---
150
144
 
@@ -4,44 +4,48 @@ class MeritAction
4
4
  attr_accessible :user_id, :action_method, :action_value, :had_errors, :target_model, :target_id, :processed, :log
5
5
 
6
6
  # Check rules defined for a merit_action
7
- def check_rules(defined_rules)
8
- action_name = "#{target_model}\##{action_method}"
9
-
7
+ def check_rules
10
8
  unless had_errors
11
- # Check Badge rules
12
- if defined_rules[action_name].present?
13
- defined_rules[action_name].each do |rule|
14
- rule.grant_or_delete_badge(self)
15
- end
16
- end
9
+ check_badge_rules
10
+ check_point_rules
11
+ end
12
+ processed!
13
+ end
14
+
15
+ def check_badge_rules
16
+ defined_rules = Merit::BadgeRules.new.defined_rules["#{target_model}\##{action_method}"]
17
+ return if defined_rules.nil?
17
18
 
18
- # Check Point rules
19
- actions_to_point = Merit::PointRules.new.actions_to_point
20
- if actions_to_point[action_name].present?
21
- point_rule = actions_to_point[action_name]
22
- point_rule[:to].each do |to|
23
- if to == :action_user
24
- target = Merit.user_model.find_by_id(user_id)
25
- if target.nil?
26
- Rails.logger.warn "[merit] no user found to grant points"
27
- return
28
- end
29
- else
30
- begin
31
- target = target_object.send(to)
32
- rescue NoMethodError
33
- Rails.logger.warn "[merit] No target_object found on check_rules."
34
- return
35
- end
19
+ defined_rules.each do |rule|
20
+ rule.grant_or_delete_badge(self)
21
+ end
22
+ end
23
+
24
+ def check_point_rules
25
+ actions_to_point = Merit::PointRules.new.actions_to_point["#{target_model}\##{action_method}"]
26
+ return if actions_to_point.nil?
27
+
28
+ actions_to_point.each do |point_rule|
29
+ point_rule[:to].each do |to|
30
+ if to == :action_user
31
+ target = Merit.user_model.find_by_id(user_id)
32
+ if target.nil?
33
+ Rails.logger.warn "[merit] no user found to grant points"
34
+ return
35
+ end
36
+ else
37
+ begin
38
+ target = target_object.send(to)
39
+ rescue NoMethodError
40
+ Rails.logger.warn "[merit] No target_object found on check_rules."
41
+ return
36
42
  end
37
- target.points += point_rule[:score]
38
- target.save
39
- log!("points_granted:#{point_rule[:score]}")
40
43
  end
44
+ target.points += point_rule[:score]
45
+ target.save
46
+ log!("points_granted:#{point_rule[:score]}")
41
47
  end
42
48
  end
43
-
44
- processed!
45
49
  end
46
50
 
47
51
  # Action's target object
@@ -54,7 +54,7 @@ module Merit
54
54
  # Check non processed actions and grant badges if applies
55
55
  def check_new_actions
56
56
  MeritAction.where(:processed => false).each do |merit_action|
57
- merit_action.check_rules(defined_rules)
57
+ merit_action.check_rules
58
58
  end
59
59
  end
60
60
 
@@ -6,14 +6,14 @@ module Merit
6
6
  # Define rules on certaing actions for giving points
7
7
  def score(points, *args, &block)
8
8
  options = args.extract_options!
9
-
10
- actions = options[:on].kind_of?(Array) ? options[:on] : [options[:on]]
11
9
  options[:to] ||= [:action_user]
12
- targets = options[:to].kind_of?(Array) ? options[:to] : [options[:to]]
10
+
11
+ actions = Array.wrap(options[:on])
13
12
  actions.each do |action|
14
- actions_to_point[action] = {
15
- :to => targets,
16
- :score => points
13
+ actions_to_point[action] ||= []
14
+ actions_to_point[action] << {
15
+ :score => points,
16
+ :to => Array.wrap(options[:to])
17
17
  }
18
18
  end
19
19
  end
@@ -23,4 +23,4 @@ module Merit
23
23
  @actions_to_point ||= {}
24
24
  end
25
25
  end
26
- end
26
+ end
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.description = "Manage badges, points and rankings (reputation) of resources in a Rails application."
5
5
  s.homepage = "http://github.com/tute/merit"
6
6
  s.files = `git ls-files`.split("\n").reject{|f| f =~ /^\./ }
7
- s.version = "0.9.4"
7
+ s.version = "0.9.5"
8
8
  s.authors = ["Tute Costa"]
9
9
  s.email = 'tutecosta@gmail.com'
10
10
  s.add_dependency 'ambry', '~> 0.3.0'
@@ -7,9 +7,10 @@ module Merit
7
7
  include Merit::PointRulesMethods
8
8
 
9
9
  def initialize
10
- score 5, :to => :user, :on => [
11
- 'comments#vote'
12
- ]
10
+ # Thanks for voting point
11
+ score 1, :on => 'comments#vote'
12
+ # Points to voted user
13
+ score 5, :to => :user, :on => 'comments#vote'
13
14
 
14
15
  score 20, :on => [
15
16
  'comments#create',
@@ -104,7 +104,7 @@ class NavigationTest < ActiveSupport::IntegrationCase
104
104
 
105
105
  visit "/comments/#{Comment.last.id}/vote/4"
106
106
  user = User.first
107
- assert_equal 45, user.points, 'Voting comments should grant 5 points'
107
+ assert_equal 46, user.points, 'Voting comments should grant 5 points for voted, and 1 point for voting'
108
108
  end
109
109
 
110
110
  test 'user workflow should grant levels at some times' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-01 00:00:00.000000000 Z
12
+ date: 2012-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ambry