mongoid_rateable 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -3,8 +3,6 @@
3
3
  *0.3.0*
4
4
 
5
5
  Removed RATING_RANGE constant.
6
- The RATING_RANGE constant is very inflexible. Much better with a class method, since
7
- it allows for reusability, definint the range in a module that is pulled into
8
- several classes etc.
6
+ The RATING_RANGE constant is very inflexible. Much better with a class method, since it allows for reusability, definint the range in a module that is pulled into several classes etc.
9
7
 
10
8
  Instead this gem now includes several other config methods.
data/README.rdoc CHANGED
@@ -15,45 +15,44 @@ Add to Gemfile:
15
15
 
16
16
  == Getting Started
17
17
 
18
- Include the module in models where you want it.
19
- There are many other configurtion variants available ;)
18
+
19
+ Simply use the `rateable` macro from any class that is a Mongoid Document.
20
+
21
+ This macro will include `Mongoid::Rateable` into the class and configure the rating functionality using the options hash. For any option not present, the default option value will be used.
20
22
 
21
23
  class Post
22
24
  include Mongoid::Document
23
- include Mongoid::Rateable
24
25
 
25
- ...
26
+ rateable range: (-5..7), raters: [User, Admin]
26
27
  end
27
28
 
28
- or
29
+ You can also set the `default_rater`
29
30
 
30
31
  class Post
31
32
  include Mongoid::Document
32
- include Mongoid::Rateable
33
-
34
- # class method
35
- def self.rating_range
36
- (1..5) # should always return the same range
37
- end
38
33
 
39
- # Optional restriction
40
- rateable_by User
34
+ # will simply call the 'owner' method to find the default rater
35
+ # if no rater given when rating
41
36
 
42
- ...
37
+ rateable range: (-5..7), raters: [User, Admin], default_rater: 'owner'
43
38
  end
44
39
 
45
- or
46
-
47
40
  class Post
48
41
  include Mongoid::Document
49
- include Mongoid::Rateable
50
42
 
51
- # macro
52
- set_rating_range (1..5)
53
- rateable_by User, Admin
54
- ...
43
+ # if given a block, this will be used as a dynamic way to find
44
+ # the a rater in case no rater is passed in as the 2nd argument to
45
+ # the rate method
46
+
47
+ rateable range: (-5..7), raters: [User, Admin] do
48
+ # will by default be rated by the last user
49
+ # who made a comment to this post!
50
+ comments.last.user
51
+ end
55
52
  end
56
53
 
54
+ Note: For even more control over the configuration, see the `ClassMethods` module code in `rateable.rb`.
55
+
57
56
  == Cast Rates
58
57
 
59
58
  You can rate by passing an integer and a rater model to the "rate" method:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -1,3 +1,8 @@
1
1
  require 'mongoid_rateable/version'
2
2
  require 'mongoid_rateable/rateable'
3
- require 'mongoid_rateable/rating'
3
+ require 'mongoid_rateable/rating'
4
+
5
+ Mongoid::Document.send :include, Mongoid::Rateable::Ext
6
+
7
+
8
+
@@ -2,6 +2,20 @@ module Mongoid
2
2
  module Rateable
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ module Ext
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def rateable options = {}
10
+ class_eval do
11
+ self.send :include, Mongoid::Rateable
12
+ puts "options: #{options}"
13
+ self.rate_config options
14
+ end
15
+ end
16
+ end
17
+ end
18
+
5
19
  included do
6
20
  field :rates, type: Integer, default: 0
7
21
  field :rating, type: Float, default: nil
@@ -44,6 +58,8 @@ module Mongoid
44
58
  Range.new arr.first, arr.last
45
59
  when Range
46
60
  range
61
+ when nil
62
+ (1..5)
47
63
  else
48
64
  raise ArgumentError, "Must be a range, was: #{range}"
49
65
  end
@@ -55,15 +71,17 @@ module Mongoid
55
71
 
56
72
  def rateable_by *clazzes
57
73
  @rater_classes = []
74
+ return if clazzes.compact.empty?
58
75
  clazzes.each do |clazz|
59
76
  raise ArgumentError, "A rateable must be a class, was: #{clazz}" unless clazz.respond_to?(:new)
60
77
  @rater_classes << clazz
61
78
  end
62
79
  end
63
80
 
64
- def rate_config options = {}
81
+ def rate_config options = {}, &block
65
82
  set_rating_range options[:range]
66
83
  rateable_by options[:raters]
84
+ default_rater options[:default_rater], &block
67
85
  end
68
86
 
69
87
  def default_rater rater=nil, &block
@@ -73,10 +91,12 @@ module Mongoid
73
91
  self.send(rater) # fx to use owner or user relation
74
92
  end
75
93
  when nil
76
- raise ArgumentError, "Must take symbol or block argument" unless block_defined?
94
+ return unless block_given?
77
95
  define_method :default_rater do
78
96
  self.instance_eval(&block)
79
97
  end
98
+ else
99
+ raise ArgumentError, "Must take symbol or block argument"
80
100
  end
81
101
  end
82
102
  end # class methods
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mongoid_rateable"
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter Savichev (proton)"]
12
- s.date = "2012-11-03"
12
+ s.date = "2012-12-07"
13
13
  s.description = "Provides fields and methods for the rating manipulation on Mongoid documents."
14
14
  s.email = "psavichev@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -1,8 +1,7 @@
1
1
  class Comment
2
2
  include Mongoid::Document
3
- include Mongoid::Rateable
4
3
 
5
- set_rating_range (-5..7)
4
+ rateable range: (-5..7)
6
5
 
7
6
  embedded_in :post
8
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_rateable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.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-11-03 00:00:00.000000000 Z
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -168,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
168
  version: '0'
169
169
  segments:
170
170
  - 0
171
- hash: 527634735
171
+ hash: -55772387
172
172
  required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  none: false
174
174
  requirements: