merit 1.0.1 → 1.1.0

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 (1.0.1)
4
+ merit (1.1.0)
5
5
  ambry (~> 0.3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,21 +2,17 @@
2
2
 
3
3
  ![Merit](http://i567.photobucket.com/albums/ss118/DeuceBigglebags/th_nspot26_300.jpg)
4
4
 
5
- [![Build Status](https://secure.travis-ci.org/tute/merit.png?branch=master)](http://travis-ci.org/tute/merit)
5
+ [![Build Status](https://travis-ci.org/tute/merit.png?branch=master)](http://travis-ci.org/tute/merit)
6
6
 
7
7
  # Installation
8
8
 
9
9
  1. Add `gem 'merit'` to your `Gemfile`
10
10
  2. Run `rails g merit:install`
11
11
  3. Run `rails g merit MODEL_NAME` (e.g. `user`)
12
- 4. Depending on your ORM
13
- * ActiveRecord: Run `rake db:migrate`
14
- * Mongoid: Set `config.orm = :mongoid` in `config/initializers/merit.rb`
12
+ 4. Run `rake db:migrate`
15
13
  5. Define badges you will use in `config/initializers/merit.rb`
16
14
  6. Configure reputation rules for your application in `app/models/merit/*`
17
15
 
18
- NOTE: Mongoid support is experimental.
19
-
20
16
  ---
21
17
 
22
18
  # Defining badge rules
@@ -152,9 +148,11 @@ end
152
148
 
153
149
  # To-do list
154
150
 
151
+ * `Merit::BadgeRules.new.defined_rules` should be cached on initialization,
152
+ instead of initialized per controllers `after_filter` and
153
+ `merit_action.check_rules`.
155
154
  * target_object should be configurable (now it's singularized controller name)
156
155
  * Translate comments from spanish in `rules_badge.rb`.
157
156
  * Should namespace app/models into Merit module.
158
157
  * :value parameter (for star voting for example) should be configurable
159
158
  (depends on params[:value] on the controller).
160
- * Make fixtures for integration testing (now creating objects on test file!).
@@ -1,8 +1,13 @@
1
1
  # Upgrading
2
2
 
3
+ ## to 1.1.0
4
+
5
+ Code refactorings. Support for Ruby 1.8.7 has been dropped.
6
+
3
7
  ## to 1.0.1
4
8
 
5
- Adds Merit::Point#created_at attribute.
9
+ Adds `Merit::Point#created_at` (`merit_score_points` table) attribute.
10
+ May already be added if upgrading from merit < 1).
6
11
 
7
12
  ## to 1.0.0
8
13
 
@@ -27,6 +32,7 @@ Run the following migration to have the new DB tables:
27
32
  t.references :score
28
33
  t.integer :num_points, :default => 0
29
34
  t.string :log
35
+ t.datetime :created_at
30
36
  end
31
37
  end
32
38
 
@@ -38,16 +44,27 @@ Run the following migration to have the new DB tables:
38
44
 
39
45
  # This will create a single point entry log, with previous points granted
40
46
  # to each meritable resource. Code example for a User class.
47
+
41
48
  class UpgradeMeritableResources < ActiveRecord::Migration
42
49
  def up
43
50
  User.find_each do |user|
51
+ unless user.sash
52
+ user.sash = Sash.create!
53
+ user.save
54
+ end
55
+
44
56
  user.sash.scores << Merit::Score.create
45
- user.add_points(user.points, 'Initial merit points import.')
57
+ user.add_points(user.read_attribute(:points), 'Initial merit points import.')
46
58
  end
47
59
  remove_column :users, :points
48
60
  end
49
61
  end
50
62
 
63
+ If you get an `ActiveRecord::DangerousAttributeError: points` exception, you
64
+ may need to temporarily tweak your meritable model, as explained in
65
+ http://stackoverflow.com/a/1515571/356060.
66
+
67
+
51
68
  ## to 0.10.0
52
69
 
53
70
  `badges_sashes` table gets a primary key `id` column. Run the following migration:
@@ -14,21 +14,15 @@ class MeritAction
14
14
  end
15
15
 
16
16
  def check_badge_rules
17
- return if badge_rules.nil?
17
+ badge_rules = Merit::BadgeRules.new.defined_rules[action_str] || []
18
18
  badge_rules.each { |rule| rule.grant_or_delete_badge(self) }
19
19
  end
20
20
 
21
21
  def check_point_rules
22
- return if point_rules.nil?
22
+ point_rules = Merit::PointRules.new.defined_rules[action_str] || []
23
23
  point_rules.each { |rule| rule.grant_points(self) }
24
24
  end
25
25
 
26
- def badge_rules
27
- @badge_rules ||= Merit::BadgeRules.new.defined_rules[action_str] || []
28
- end
29
- def point_rules
30
- @point_rules ||= Merit::PointRules.new.actions_to_point[action_str] || []
31
- end
32
26
  def action_str
33
27
  "#{target_model}\##{action_method}"
34
28
  end
@@ -8,7 +8,7 @@ module Merit
8
8
  action = "#{controller_name}\##{action_name}"
9
9
  badge_rules = BadgeRules.new
10
10
  point_rules = PointRules.new
11
- if badge_rules.defined_rules[action].present? || point_rules.actions_to_point[action].present?
11
+ if badge_rules.defined_rules[action].present? || point_rules.defined_rules[action].present?
12
12
  merit_action_id = MeritAction.create(
13
13
  :user_id => send(Merit.current_user_method).try(:id),
14
14
  :action_method => action_name,
@@ -21,34 +21,37 @@ module Merit
21
21
  where(:_id => id).first
22
22
  end
23
23
  end
24
- end
25
- end
26
24
 
27
- # Delegate relationship methods from meritable models to their sash
28
- %w(badge_ids badges points).each do |method|
29
- define_method(method) do
30
- _sash = sash || create_sash_and_scores
31
- _sash.send method
32
- end
33
- end
25
+ # Add instance methods to meritable models
26
+ # Using define_method on meritable classes to not pollute every model
34
27
 
35
- def add_points(num_points, log = 'Manually granted through `add_points`', category = 'default')
36
- _sash = sash || create_sash_and_scores
37
- _sash.add_points num_points, log, category
38
- end
39
- def substract_points(num_points, log = 'Manually granted through `add_points`', category = 'default')
40
- _sash = sash || create_sash_and_scores
41
- _sash.substract_points num_points, log, category
42
- end
28
+ # Delegate relationship methods from meritable models to their sash
29
+ %w(badge_ids badges points).each do |method|
30
+ define_method(method) do
31
+ _sash = sash || create_sash_and_scores
32
+ _sash.send method
33
+ end
34
+ end
35
+
36
+ define_method(:add_points) do |num_points, log = 'Manually through `add_points`', category = 'default'|
37
+ _sash = sash || create_sash_and_scores
38
+ _sash.add_points num_points, log, category
39
+ end
40
+ define_method(:substract_points) do |num_points, log = 'Manually through `substract_points`', category = 'default'|
41
+ _sash = sash || create_sash_and_scores
42
+ _sash.substract_points num_points, log, category
43
+ end
43
44
 
44
- # Create sash if doesn't have
45
- def create_sash_and_scores
46
- if self.sash.blank?
47
- self.sash = Sash.create
48
- self.sash.scores << Merit::Score.create
49
- self.save(:validate => false)
45
+ # Create sash if doesn't have
46
+ define_method(:create_sash_and_scores) do
47
+ if self.sash.blank?
48
+ self.sash = Sash.create
49
+ self.sash.scores << Merit::Score.create
50
+ self.save(:validate => false)
51
+ end
52
+ self.sash
53
+ end
50
54
  end
51
- self.sash
52
55
  end
53
56
  end
54
57
 
@@ -53,9 +53,7 @@ module Merit
53
53
 
54
54
  # Check non processed actions and grant badges if applies
55
55
  def check_new_actions
56
- MeritAction.where(:processed => false).each do |merit_action|
57
- merit_action.check_rules
58
- end
56
+ MeritAction.where(:processed => false).map &:check_rules
59
57
  end
60
58
 
61
59
  # Currently defined rules
@@ -17,15 +17,15 @@ module Merit
17
17
  rule.block = block
18
18
 
19
19
  actions.each do |action|
20
- actions_to_point[action] ||= []
21
- actions_to_point[action] << rule
20
+ defined_rules[action] ||= []
21
+ defined_rules[action] << rule
22
22
  end
23
23
  end
24
24
  end
25
25
 
26
26
  # Currently defined rules
27
- def actions_to_point
28
- @actions_to_point ||= {}
27
+ def defined_rules
28
+ @defined_rules ||= {}
29
29
  end
30
30
  end
31
31
  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 = '1.0.1'
7
+ s.version = '1.1.0'
8
8
  s.authors = ["Tute Costa"]
9
9
  s.email = 'tutecosta@gmail.com'
10
10
  s.add_dependency 'ambry', '~> 0.3.0'
@@ -28,6 +28,23 @@ class MeritUnitTest < ActiveSupport::TestCase
28
28
  assert_equal Badge.find(98), rule.badge
29
29
  end
30
30
 
31
+ test "Extending only certain ActiveRecord models" do
32
+ class MeritableModel < ActiveRecord::Base
33
+ def self.columns; @columns ||= []; end
34
+ has_merit
35
+ end
36
+ assert MeritableModel.method_defined?(:points), 'Meritable model should respond to merit methods'
37
+ assert !ActiveRecord::Base.method_defined?(:points), 'ActiveRecord::Base shouldn\'t respond to merit methods'
38
+ end
39
+
40
+ # Do we need this non-documented attribute?
41
+ test "BadgesSash#set_notified! sets boolean attribute" do
42
+ badge_sash = BadgesSash.new
43
+ assert !badge_sash.notified_user
44
+ badge_sash.set_notified!
45
+ assert badge_sash.notified_user
46
+ end
47
+
31
48
  # TODO: Test and refactor:
32
49
  # Rule: grant_or_delete_badge(action), sash_to_badge
33
50
  # Badge: delete_from
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: 1.0.1
4
+ version: 1.1.0
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-10-26 00:00:00.000000000 Z
12
+ date: 2012-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ambry
@@ -325,3 +325,4 @@ signing_key:
325
325
  specification_version: 3
326
326
  summary: General reputation Rails engine.
327
327
  test_files: []
328
+ has_rdoc: