merit 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -28,6 +28,8 @@ Define rules on +app/models/merit_badge_rules.rb+:
28
28
  * :+to+: method name over target_object which obtains user to badge.
29
29
  * :+temporary+ (boolean): if the condition doesn't hold and the receiver had
30
30
  the badge, it gets removed. +false+ by default (badges are kept forever).
31
+ * :+model_name+ (string): when controller's name differs from the model being
32
+ worked (like RegistrationsController for User model).
31
33
  * &+block+
32
34
  * empty (always grants)
33
35
  * a block which evaluates to boolean (recieves target object as parameter)
@@ -125,6 +127,8 @@ To run the test application inside this gem follow:
125
127
  = To-do list
126
128
 
127
129
  * Test points granting with different options.
130
+ * Test model_name attribute for badge_rules.
131
+ * Add model_name option on rank and point rules.
128
132
  * Ranking should not be badges, so .badges doesn't return them (2-stars shouldn't be badge).
129
133
  * grep -r 'FIXME\|TODO' .
130
134
  * :value parameter (for star voting for example) should be configurable (depends
@@ -38,9 +38,10 @@ class MeritAction < ActiveRecord::Base
38
38
  end
39
39
 
40
40
  # Action's target object
41
- def target_object
42
- klass = target_model.singularize.camelize.constantize
43
- klass.find_by_id(target_id)
41
+ def target_object(model_name = nil)
42
+ # Grab custom model_name from Rule, or target_model from MeritAction triggered
43
+ klass = model_name || target_model.singularize
44
+ klass.camelize.constantize.find_by_id(target_id)
44
45
  end
45
46
 
46
47
  # Mark merit_action as processed
@@ -33,7 +33,7 @@ class MeritBadgeRules
33
33
  # end
34
34
 
35
35
  # Changes his name by one wider than 4 chars (arbitrary ruby code case)
36
- # grant_on 'users#update', :badge => 'autobiographer', :temporary => true do |user|
36
+ # grant_on 'registrations#update', :badge => 'autobiographer', :temporary => true, :model_name => 'User' do |user|
37
37
  # user.name.length > 4
38
38
  # end
39
39
  end
@@ -10,7 +10,7 @@ module Merit
10
10
  point_rules = ::MeritPointRules.new
11
11
  if badge_rules.defined_rules[action].present? || point_rules.actions_to_point[action].present?
12
12
  target_id = params[:id]
13
- # TODO: target_object should be configurable (now it's singularized controller name)
13
+ # TODO: target_object should be configurable (now it's singularized controller name) // Should be using model_name? Only for badges now
14
14
  target_object = instance_variable_get(:"@#{controller_name.singularize}")
15
15
  unless target_id =~ /^[0-9]+$/ # id nil, or string (friendly_id)?
16
16
  target_id = target_object.try(:id)
data/lib/merit/rule.rb CHANGED
@@ -2,7 +2,7 @@ module Merit
2
2
  # Rules has a badge name and level, a target to badge, a conditions block
3
3
  # and a temporary option.
4
4
  class Rule
5
- attr_accessor :badge_name, :level, :to, :temporary, :block
5
+ attr_accessor :badge_name, :level, :to, :temporary, :block, :model_name
6
6
 
7
7
  # Does this rule's condition block apply?
8
8
  def applies?(target_obj = nil)
@@ -38,7 +38,7 @@ module Merit
38
38
  # then remove it.
39
39
  def grant_or_delete_badge(action)
40
40
  if sash = sash_to_badge(action)
41
- if applies? action.target_object
41
+ if applies? action.target_object(model_name)
42
42
  badge.grant_to sash
43
43
  elsif temporary?
44
44
  badge.delete_from sash
@@ -54,10 +54,10 @@ module Merit
54
54
  when :action_user
55
55
  User.find_by_id(action.user_id) # _by_id doens't raise ActiveRecord::RecordNotFound
56
56
  when :itself
57
- action.target_object
57
+ action.target_object(model_name)
58
58
  else
59
59
  begin
60
- action.target_object.send(to)
60
+ action.target_object(model_name).send(to)
61
61
  rescue
62
62
  Rails.logger.warn "[merit] #{action.target_model.singularize}.find(#{action.target_id}) not found, no badges giving today"
63
63
  return
@@ -40,6 +40,7 @@ module Merit
40
40
  rule.level = options[:level]
41
41
  rule.to = options[:to] || :action_user
42
42
  rule.temporary = options[:temporary] || false
43
+ rule.model_name = options[:model_name] || action.split('#')[0]
43
44
  rule.block = block
44
45
 
45
46
  actions = action.kind_of?(String) ? [action] : action
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.2.0
4
+ version: 0.2.1
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-02-06 00:00:00.000000000 Z
12
+ date: 2012-02-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: General reputation Rails engine.
15
15
  email: tutecosta@gmail.com