badgeable 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/features/award_badges.feature +9 -0
- data/features/step_definitions/badging_steps.rb +29 -0
- data/features/support/test_models/user.rb +1 -0
- data/lib/badgeable/adapters/mongoid_adapter.rb +4 -2
- data/lib/badgeable/adapters/mongoid_adapter/badge.rb +15 -3
- data/lib/badgeable/award.rb +2 -1
- data/lib/badgeable/config.rb +19 -0
- data/lib/badgeable/subject.rb +4 -2
- data/lib/badgeable/version.rb +1 -1
- data/lib/generators/templates/badge.rb +22 -0
- data/lib/generators/templates/badging.rb +29 -0
- metadata +6 -4
data/Gemfile.lock
CHANGED
@@ -17,4 +17,13 @@ Feature: Easily award badges to any Ruby class (given enough effort on your part
|
|
17
17
|
Given a User gets the "Guppy" badge when they have written 10 reviews
|
18
18
|
When the user has written 10 reviews
|
19
19
|
Then the user should have the "Guppy" badge
|
20
|
+
|
21
|
+
Scenario: If I supply some extra attributes, the badge gets created with them
|
22
|
+
Given A user gets the "Awesome" badge with a custom description "This badge is awesome" and icon "awesome-badge.jpg" for being awesome
|
23
|
+
When they are awesome
|
24
|
+
Then the user should have the "Awesome" badge
|
25
|
+
And the badge description should be "This badge is awesome"
|
26
|
+
And the badge icon should be "awesome-badge.jpg"
|
27
|
+
|
28
|
+
|
20
29
|
|
@@ -29,6 +29,19 @@ Given /^a User gets the "([^\"]*)" badge when they have written (\d+) reviews$/
|
|
29
29
|
}
|
30
30
|
end
|
31
31
|
|
32
|
+
Given /^A user gets the "([^"]*)" badge with a custom description "([^"]*)" and icon "([^"]*)" for being awesome$/ do |badge_name, description, icon|
|
33
|
+
User.class_eval %Q{
|
34
|
+
badge "#{badge_name}", :after => :save do
|
35
|
+
thing User
|
36
|
+
conditions do |user|
|
37
|
+
user.awesome?
|
38
|
+
end
|
39
|
+
description "#{description}"
|
40
|
+
icon "#{icon}"
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
32
45
|
When /^a diner creates (\d+) meals$/ do |num|
|
33
46
|
@diner = Diner.create
|
34
47
|
num.to_i.times do
|
@@ -49,6 +62,12 @@ When /^the user has written (\d+) reviews$/ do |num|
|
|
49
62
|
end
|
50
63
|
end
|
51
64
|
|
65
|
+
When /^they are awesome$/ do
|
66
|
+
Given %Q{I am a user}
|
67
|
+
@user.awesome = true
|
68
|
+
@user.save
|
69
|
+
end
|
70
|
+
|
52
71
|
Given /^I am a user$/ do
|
53
72
|
@user = User.create
|
54
73
|
end
|
@@ -89,3 +108,13 @@ When /^my unseen badges are marked as seen$/ do
|
|
89
108
|
badging.mark_as_seen
|
90
109
|
end
|
91
110
|
end
|
111
|
+
|
112
|
+
Then /^the badge description should be "([^"]*)"$/ do |description|
|
113
|
+
@badge = @user.badges.first
|
114
|
+
@badge.description.should eql(description)
|
115
|
+
end
|
116
|
+
|
117
|
+
Then /^the badge icon should be "([^"]*)"$/ do |icon|
|
118
|
+
@badge = @user.badges.first
|
119
|
+
@badge.icon.should eql(icon)
|
120
|
+
end
|
@@ -23,8 +23,10 @@ module Badgeable
|
|
23
23
|
end
|
24
24
|
|
25
25
|
module InstanceMethods
|
26
|
-
def award_badge(
|
27
|
-
|
26
|
+
def award_badge(*args)
|
27
|
+
options = args.extract_options!
|
28
|
+
name = args[0]
|
29
|
+
badge = Badge.find_or_create_by_name(name, options)
|
28
30
|
badgings.create(:badge_id => badge.id) unless has_badge?(badge)
|
29
31
|
end
|
30
32
|
|
@@ -3,13 +3,25 @@ class Badge
|
|
3
3
|
include Mongoid::Timestamps
|
4
4
|
|
5
5
|
field :name
|
6
|
+
field :description
|
7
|
+
field :icon
|
6
8
|
index :name
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
+
before_create :assign_default_icon_name, :unless => lambda{icon}
|
11
|
+
|
12
|
+
def self.find_or_create_by_name(name, options)
|
13
|
+
attrs = options.merge(:name => name)
|
14
|
+
criteria.where(:name => name).first || create(attrs)
|
10
15
|
end
|
11
16
|
|
12
|
-
|
17
|
+
private
|
18
|
+
|
19
|
+
def assign_default_icon_name
|
20
|
+
self.icon = default_icon_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_icon_name
|
13
24
|
"#{name.parameterize}.png"
|
14
25
|
end
|
26
|
+
|
15
27
|
end
|
data/lib/badgeable/award.rb
CHANGED
@@ -31,12 +31,13 @@ module Badgeable
|
|
31
31
|
after_callback = options[:after] || :create
|
32
32
|
config = Badgeable::Config.new
|
33
33
|
config.instance_eval(&block)
|
34
|
+
attrs = [:description, :icon].inject({}) {|hash, key| hash.merge(key => config.send(key)) }
|
34
35
|
method_name = "award_#{name.titleize.gsub(/\s/, '').underscore}_badge".to_sym
|
35
36
|
config.klass.class_eval do
|
36
37
|
set_callback after_callback, :after, method_name
|
37
38
|
define_method method_name, Proc.new {
|
38
39
|
if config.conditions_array.all? {|p| p.call(self) }
|
39
|
-
config.subject_proc.call(self).award_badge(name)
|
40
|
+
config.subject_proc.call(self).award_badge(name, attrs)
|
40
41
|
end
|
41
42
|
}
|
42
43
|
end
|
data/lib/badgeable/config.rb
CHANGED
@@ -53,5 +53,24 @@ module Badgeable
|
|
53
53
|
block.call(instance)
|
54
54
|
}
|
55
55
|
end
|
56
|
+
|
57
|
+
def icon(*args)
|
58
|
+
raise ArgumentError unless args.length <= 1
|
59
|
+
if args.length == 1
|
60
|
+
@icon = args[0]
|
61
|
+
else
|
62
|
+
@icon
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def description(*args)
|
67
|
+
raise ArgumentError unless args.length <= 1
|
68
|
+
if args.length == 1
|
69
|
+
@description = args[0]
|
70
|
+
else
|
71
|
+
@description
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
56
75
|
end
|
57
76
|
end
|
data/lib/badgeable/subject.rb
CHANGED
@@ -2,8 +2,10 @@ module Badgeable
|
|
2
2
|
module Subject
|
3
3
|
# Award a named badge to this object. If the badge doesn't exist
|
4
4
|
# in the database already, it's created by name.
|
5
|
-
def award_badge(
|
6
|
-
|
5
|
+
def award_badge(*args)
|
6
|
+
options = args.extract_options!
|
7
|
+
name = args[0]
|
8
|
+
badge = Badge.find_or_create_by_name(name, options)
|
7
9
|
badges << badge unless has_badge?(badge)
|
8
10
|
end
|
9
11
|
|
data/lib/badgeable/version.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
# class BadgeCreate<%= table_name.camelize %> < ActiveRecord::Migration
|
2
|
+
# def self.up
|
3
|
+
# create_table(:<%= table_name %>) do |t|
|
4
|
+
# t.name :string
|
5
|
+
#
|
6
|
+
# <% for attribute in attributes -%>
|
7
|
+
# t.<%= attribute.type %> :<%= attribute.name %>
|
8
|
+
# <% end -%>
|
9
|
+
#
|
10
|
+
# t.timestamps
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# add_index :<%= table_name %>, :email, :unique => true
|
14
|
+
# add_index :<%= table_name %>, :reset_password_token, :unique => true
|
15
|
+
# # add_index :<%= table_name %>, :confirmation_token, :unique => true
|
16
|
+
# # add_index :<%= table_name %>, :unlock_token, :unique => true
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# def self.down
|
20
|
+
# drop_table :<%= table_name %>
|
21
|
+
# end
|
22
|
+
# end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
|
2
|
+
# def self.up
|
3
|
+
# create_table(:<%= table_name %>) do |t|
|
4
|
+
# t.database_authenticatable :null => false
|
5
|
+
# t.recoverable
|
6
|
+
# t.rememberable
|
7
|
+
# t.trackable
|
8
|
+
#
|
9
|
+
# # t.confirmable
|
10
|
+
# # t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %>
|
11
|
+
# # t.token_authenticatable
|
12
|
+
#
|
13
|
+
# <% for attribute in attributes -%>
|
14
|
+
# t.<%= attribute.type %> :<%= attribute.name %>
|
15
|
+
# <% end -%>
|
16
|
+
#
|
17
|
+
# t.timestamps
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# add_index :<%= table_name %>, :email, :unique => true
|
21
|
+
# add_index :<%= table_name %>, :reset_password_token, :unique => true
|
22
|
+
# # add_index :<%= table_name %>, :confirmation_token, :unique => true
|
23
|
+
# # add_index :<%= table_name %>, :unlock_token, :unique => true
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# def self.down
|
27
|
+
# drop_table :<%= table_name %>
|
28
|
+
# end
|
29
|
+
# end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Scott Burton
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-12-17 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -113,6 +113,8 @@ files:
|
|
113
113
|
- lib/badgeable/railtie.rb
|
114
114
|
- lib/badgeable/subject.rb
|
115
115
|
- lib/badgeable/version.rb
|
116
|
+
- lib/generators/templates/badge.rb
|
117
|
+
- lib/generators/templates/badging.rb
|
116
118
|
- spec/spec_helper.rb
|
117
119
|
has_rdoc: true
|
118
120
|
homepage: http://rubygems.org/gems/badgeable
|