merit 0.9.3 → 0.9.4

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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- merit (0.9.3)
4
+ merit (0.9.4)
5
5
  ambry (~> 0.3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -140,8 +140,6 @@ end
140
140
  # To-do list
141
141
 
142
142
  * add an error handler for inexistent badges.
143
- * rails g merit MODEL_NAME shouldn't create general migrations again.
144
- * Abstract User (rule.rb#51 for instance) into a Merit option.
145
143
  * Should namespace app/models into Merit module.
146
144
  * rescue ActiveRecord::... should depend on ORM used
147
145
  * :value parameter (for star voting for example) should be configurable
@@ -21,7 +21,8 @@ class MeritAction
21
21
  point_rule = actions_to_point[action_name]
22
22
  point_rule[:to].each do |to|
23
23
  if to == :action_user
24
- if !(target = User.find_by_id(user_id))
24
+ target = Merit.user_model.find_by_id(user_id)
25
+ if target.nil?
25
26
  Rails.logger.warn "[merit] no user found to grant points"
26
27
  return
27
28
  end
@@ -33,7 +34,8 @@ class MeritAction
33
34
  return
34
35
  end
35
36
  end
36
- target.update_attribute(:points, target.points + point_rule[:score])
37
+ target.points += point_rule[:score]
38
+ target.save
37
39
  log!("points_granted:#{point_rule[:score]}")
38
40
  end
39
41
  end
@@ -47,14 +49,18 @@ class MeritAction
47
49
  # Grab custom model_name from Rule, or target_model from MeritAction triggered
48
50
  klass = model_name || target_model
49
51
  klass.singularize.camelize.constantize.find_by_id(target_id)
52
+ rescue => e
53
+ Rails.logger.warn "[merit] no target_object found: #{e}"
50
54
  end
51
55
 
52
56
  def log!(str)
53
- self.update_attribute :log, "#{self.log}#{str}|"
57
+ self.log = "#{self.log}#{str}|"
58
+ self.save
54
59
  end
55
60
 
56
61
  # Mark merit_action as processed
57
62
  def processed!
58
- self.update_attribute :processed, true
63
+ self.processed = true
64
+ self.save
59
65
  end
60
66
  end
@@ -5,6 +5,12 @@ Merit.setup do |config|
5
5
 
6
6
  # Define ORM. Could be :active_record (default) and :mongo_mapper and :mongoid
7
7
  # config.orm = :active_record
8
+
9
+ # Define :user_model_name. This model will be used to grand badge if no :to option is given. Default is "User".
10
+ # config.user_model_name = "User"
11
+
12
+ # Define :current_user_method. Similar to previous option. It will be used to retrieve :user_model_name object if no :to option is given. Default is "current_#{user_model_name.downcase}".
13
+ # config.current_user_method = "current_user"
8
14
  end
9
15
 
10
16
  # Create application badges (uses https://github.com/norman/ambry)
@@ -14,6 +14,20 @@ module Merit
14
14
  mattr_accessor :orm
15
15
  @@orm = :active_record
16
16
 
17
+ # Define user_model_name
18
+ mattr_accessor :user_model_name
19
+ @@user_model_name = "User"
20
+ def self.user_model
21
+ @@user_model_name.constantize
22
+ end
23
+
24
+ # Define current_user_method
25
+ mattr_accessor :current_user_method
26
+ def self.current_user_method
27
+ @@current_user_method || "current_#{@@user_model_name.downcase}".to_sym
28
+ end
29
+
30
+
17
31
  # Load configuration from initializer
18
32
  def self.setup
19
33
  yield self
@@ -20,7 +20,7 @@ module Merit
20
20
 
21
21
  # TODO: value should be configurable (now it's params[:value] set in the controller)
22
22
  merit_action_id = MeritAction.create(
23
- :user_id => current_user.try(:id),
23
+ :user_id => send(Merit.current_user_method).try(:id),
24
24
  :action_method => action_name,
25
25
  :action_value => params[:value],
26
26
  :had_errors => target_object.try(:errors).try(:present?),
@@ -48,7 +48,7 @@ module Merit
48
48
  def sash_to_badge(action)
49
49
  target = case to
50
50
  when :action_user
51
- User.find_by_id(action.user_id) # _by_id doens't raise ActiveRecord::RecordNotFound
51
+ Merit.user_model.find_by_id(action.user_id) # _by_id doens't raise ActiveRecord::RecordNotFound
52
52
  when :itself
53
53
  action.target_object(model_name)
54
54
  else
@@ -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.3"
7
+ s.version = "0.9.4"
8
8
  s.authors = ["Tute Costa"]
9
9
  s.email = 'tutecosta@gmail.com'
10
10
  s.add_dependency 'ambry', '~> 0.3.0'
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.3
4
+ version: 0.9.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: