minimal_feedback 0.0.2 → 0.0.3
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.
data/README.md
CHANGED
@@ -23,7 +23,18 @@ rake db:migrate
|
|
23
23
|
Let's say we have an Issue ActiveRecord model and we want to be able to give it feedback
|
24
24
|
|
25
25
|
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
has_many :issues
|
28
|
+
end
|
29
|
+
|
26
30
|
class Issue < ActiveRecord::Base
|
31
|
+
include MinimalFeedback
|
32
|
+
|
33
|
+
attr_accessor :condition
|
34
|
+
|
35
|
+
allow_feedback :if => proc { condition }
|
36
|
+
|
37
|
+
belongs_to :user
|
27
38
|
end
|
28
39
|
```
|
29
40
|
|
@@ -31,6 +42,7 @@ What would happen with this configuration:
|
|
31
42
|
|
32
43
|
```ruby
|
33
44
|
issue = Issue.create
|
45
|
+
issue.condition = proc { true }
|
34
46
|
issue.give_feedback(:positive)
|
35
47
|
issue.feedbacks.first.type
|
36
48
|
=> :positive
|
@@ -40,6 +52,8 @@ issue.feedbacks.last.type
|
|
40
52
|
=> :negative
|
41
53
|
```
|
42
54
|
|
55
|
+
Else if the condition proc returns false when the feedback validation is performed an exception is raised
|
56
|
+
|
43
57
|
## Contributing
|
44
58
|
|
45
59
|
1. Fork it
|
@@ -5,9 +5,16 @@ module MinimalFeedback
|
|
5
5
|
self.table_name = 'mf_feedbacks'
|
6
6
|
|
7
7
|
belongs_to :rateable, :polymorphic => true
|
8
|
+
belongs_to :user
|
8
9
|
|
9
10
|
validates :rating, :inclusion => { :in => [1, -1] }
|
10
11
|
|
12
|
+
validate do
|
13
|
+
if rateable.class.feedback_condition && !rateable.instance_eval(&rateable.class.feedback_condition).call
|
14
|
+
errors.add(:base, 'feedback is not allowed')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
11
18
|
def type
|
12
19
|
case rating
|
13
20
|
when 1
|
data/lib/minimal_feedback.rb
CHANGED
@@ -10,16 +10,31 @@ module MinimalFeedback
|
|
10
10
|
|
11
11
|
class InvalidFeedbackError < StandardError; end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
cattr_accessor :feedback_condition
|
14
|
+
|
15
|
+
def self.allow_feedback(options)
|
16
|
+
self.feedback_condition = options[:if]
|
17
|
+
end
|
18
|
+
|
19
|
+
def give_feedback(*args)
|
20
|
+
options = args.extract_options!
|
21
|
+
type = args.first.to_sym
|
22
|
+
|
23
|
+
feedback = Feedback.new do |f|
|
24
|
+
f.rateable = self
|
25
|
+
f.user = options[:user]
|
26
|
+
end
|
27
|
+
|
28
|
+
case type
|
16
29
|
when :positive
|
17
|
-
|
30
|
+
feedback.rating = 1
|
18
31
|
when :negative
|
19
|
-
|
32
|
+
feedback.rating = -1
|
20
33
|
else
|
21
34
|
raise InvalidFeedbackError
|
22
35
|
end
|
36
|
+
|
37
|
+
feedback.save!
|
23
38
|
end
|
24
39
|
end
|
25
40
|
end
|
@@ -1,32 +1,61 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'mf_feedbacks'")
|
4
|
+
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'users'")
|
4
5
|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'issues'")
|
5
6
|
ActiveRecord::Base.connection.create_table(:mf_feedbacks) do |t|
|
6
7
|
t.string :type
|
7
8
|
t.integer :rating
|
9
|
+
t.references :user
|
8
10
|
t.references :rateable, :polymorphic => true
|
9
11
|
t.timestamps
|
10
12
|
end
|
11
13
|
|
12
|
-
ActiveRecord::Base.connection.create_table(:
|
14
|
+
ActiveRecord::Base.connection.create_table(:users)
|
15
|
+
ActiveRecord::Base.connection.create_table(:issues) do |t|
|
16
|
+
t.references :user
|
17
|
+
end
|
18
|
+
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
has_many :issues
|
21
|
+
end
|
13
22
|
|
14
23
|
class Issue < ActiveRecord::Base
|
15
24
|
include MinimalFeedback
|
25
|
+
|
26
|
+
attr_accessor :condition
|
27
|
+
|
28
|
+
allow_feedback :if => proc { condition }
|
29
|
+
|
30
|
+
belongs_to :user
|
16
31
|
end
|
17
32
|
|
18
33
|
describe Issue do
|
19
34
|
before(:each) do
|
20
|
-
@
|
35
|
+
@user = User.create
|
36
|
+
@issue = Issue.create do |i|
|
37
|
+
i.user = @user
|
38
|
+
end
|
21
39
|
end
|
22
40
|
|
23
41
|
it 'assigns positive feedback' do
|
24
|
-
|
42
|
+
user = User.create
|
43
|
+
@issue.condition = proc { true }
|
44
|
+
@issue.give_feedback(:positive, :user => user)
|
45
|
+
|
25
46
|
@issue.feedbacks.first.type.should == :positive
|
26
47
|
end
|
27
48
|
|
28
49
|
it 'assigns negative feedback' do
|
29
|
-
|
50
|
+
user = User.create
|
51
|
+
@issue.condition = proc { true }
|
52
|
+
@issue.give_feedback(:negative, :user => user)
|
53
|
+
|
30
54
|
@issue.feedbacks.first.type.should == :negative
|
31
55
|
end
|
56
|
+
|
57
|
+
it 'cannot assign feedback if the condition is false' do
|
58
|
+
@issue.condition = proc { false }
|
59
|
+
expect { @issue.give_feedback(:positive, :user => @user) }.to raise_error
|
60
|
+
end
|
32
61
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimal_feedback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
143
|
version: '0'
|
144
144
|
segments:
|
145
145
|
- 0
|
146
|
-
hash:
|
146
|
+
hash: -2381872170530731746
|
147
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
148
|
none: false
|
149
149
|
requirements:
|
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
152
|
version: '0'
|
153
153
|
segments:
|
154
154
|
- 0
|
155
|
-
hash:
|
155
|
+
hash: -2381872170530731746
|
156
156
|
requirements: []
|
157
157
|
rubyforge_project:
|
158
158
|
rubygems_version: 1.8.24
|