badginator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/.observr +7 -0
- data/.rspec +1 -0
- data/.ruby-gemset.example +1 -0
- data/.ruby-version.example +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +146 -0
- data/Rakefile +26 -0
- data/badginator.gemspec +29 -0
- data/lib/badginator/badge.rb +27 -0
- data/lib/badginator/nominee.rb +33 -0
- data/lib/badginator/status.rb +11 -0
- data/lib/badginator/version.rb +3 -0
- data/lib/badginator.rb +59 -0
- data/lib/generators/badginator/install_generator.rb +18 -0
- data/lib/generators/templates/app/models/awarded_badge.rb +8 -0
- data/lib/generators/templates/db/migrate/create_table_awarded_badges.rb +12 -0
- data/spec/badge_spec.rb +54 -0
- data/spec/badginator_spec.rb +125 -0
- data/spec/dummy/.gitignore +1 -0
- data/spec/dummy/.rspec +1 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/awarded_badge.rb +8 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/nominee.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20130911063801_create_table_awarded_badges.rb +12 -0
- data/spec/dummy/db/migrate/20130911102417_create_table_nominees.rb +8 -0
- data/spec/dummy/db/schema.rb +32 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/log/development.log +0 -0
- data/spec/dummy/log/test.log +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/spec_helper.rb +50 -0
- metadata +273 -0
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
.ruby-version
|
7
|
+
.ruby-gemset
|
8
|
+
Gemfile.lock
|
9
|
+
InstalledFiles
|
10
|
+
_yardoc
|
11
|
+
coverage
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/reports
|
17
|
+
test/tmp
|
18
|
+
test/version_tmp
|
19
|
+
spec/dummy/log/
|
20
|
+
tmp
|
21
|
+
.idea
|
22
|
+
badginator.iml
|
data/.observr
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1 @@
|
|
1
|
+
badginator
|
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Steven Hilton
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# Badginator
|
2
|
+
|
3
|
+
Badginator is a gem to add "badges" (or "trophies" or "achievements") to any model
|
4
|
+
of a Rails application, like User or Player. Useful for game-oriented applications.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'badginator'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install badginator
|
20
|
+
|
21
|
+
Once installed run the install generator:
|
22
|
+
|
23
|
+
$ rails g badginator:install
|
24
|
+
|
25
|
+
This generates a model and migration for AwardedBadges. Be sure the run the migration:
|
26
|
+
|
27
|
+
$ rake db:migrate
|
28
|
+
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Define your badges in `config/initializers/badginator.rb`, using the Badginator.define_badge() method. All badges
|
33
|
+
are awarded to a *nominee* based on *condition* in a given *context*.
|
34
|
+
|
35
|
+
## Properties
|
36
|
+
Badges have a few properties:
|
37
|
+
#### code
|
38
|
+
A ruby symbol. Each defined badge must be unique across your badge set
|
39
|
+
|
40
|
+
#### disabled
|
41
|
+
A boolean. You may want to disable a badge if it no longer applies, A disabled badge will never be awarded and will
|
42
|
+
not show up in a global badge list. Awardees will still be able to see their own disabled badges if they have them.
|
43
|
+
|
44
|
+
#### description
|
45
|
+
A human readable string.
|
46
|
+
|
47
|
+
#### condition
|
48
|
+
A lamda that accepts a `nominee` and a `context`. Should evaluate to a boolean.
|
49
|
+
This determines whether or not the nominee won the award. This lambda should have no effects.
|
50
|
+
|
51
|
+
#### level
|
52
|
+
Optional declaration of the difficulty level of the badge for use by the application.
|
53
|
+
Can be anything, a numeric value (1,2,3,5,8) or string value (bronze, silver, gold, platinum).
|
54
|
+
|
55
|
+
It is useful to assign a numeric value here, so you can sum the level of all a players badge's for an
|
56
|
+
overall score or reputation.
|
57
|
+
|
58
|
+
#### image
|
59
|
+
Optional url of image or code used for a graphic of the badge
|
60
|
+
|
61
|
+
#### reward
|
62
|
+
An optional lambda that gets called and returned as a part of a status object when the badge is awarded.
|
63
|
+
Also takes the two `nominee` and `context` params. This lambda should have no effects. Use the return value in
|
64
|
+
application code to actually assign rewards.
|
65
|
+
|
66
|
+
This could be a numeric value like bonus points or gold. Or and in-game object to add to a Player's inventory.
|
67
|
+
|
68
|
+
|
69
|
+
### Examples
|
70
|
+
|
71
|
+
Badginator.define_badge do
|
72
|
+
code :first_blood
|
73
|
+
name First Blood
|
74
|
+
description "Made your first kill"
|
75
|
+
condition ->(nominee, context) {
|
76
|
+
nominee.kills > 1
|
77
|
+
}
|
78
|
+
reward 1
|
79
|
+
image "http:://example.org/images/first_blood_badge.gif"
|
80
|
+
level :bronze
|
81
|
+
end
|
82
|
+
|
83
|
+
Badginator.define_badge do
|
84
|
+
code :hoarder
|
85
|
+
name "Gold Hoarder"
|
86
|
+
description "You have collected over 100,000 gold!"
|
87
|
+
condition ->(nominee, context) {
|
88
|
+
nominee.gold > 100000
|
89
|
+
}
|
90
|
+
reward 10
|
91
|
+
image "http://example.org/images/hoarder_badge.gif"
|
92
|
+
end
|
93
|
+
|
94
|
+
A class becomes eligible to be nominated when you include the Badginator::Nominee module:
|
95
|
+
|
96
|
+
class Player
|
97
|
+
include Badginator::Nominee
|
98
|
+
|
99
|
+
[...]
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
After that setup, you are ready to go!
|
104
|
+
|
105
|
+
Try to award a player an award:
|
106
|
+
|
107
|
+
status = player.try_award_badge(:hoarder, context) # context is optional
|
108
|
+
|
109
|
+
The result will have one of four states:
|
110
|
+
|
111
|
+
1. Did not win (no action taken)
|
112
|
+
2. Already won (player already has badge, no action taken)
|
113
|
+
3. Won (badge added)
|
114
|
+
4. Error (something went wrong)
|
115
|
+
|
116
|
+
If the badges was awarded, the return status has the badge attached:
|
117
|
+
|
118
|
+
if status.code == Badginator::WON
|
119
|
+
awarded_badge = status.awarded_bade
|
120
|
+
end
|
121
|
+
|
122
|
+
The `awarded_badge` is an ActiveRecord object, and has the defined badge attached to it:
|
123
|
+
|
124
|
+
$ awarded_badge.badge.code #=> :hoarder
|
125
|
+
|
126
|
+
$ awarded_badge.reward #=> 10
|
127
|
+
|
128
|
+
|
129
|
+
You can get a list of badges the player has won:
|
130
|
+
|
131
|
+
$ player.badges
|
132
|
+
|
133
|
+
|
134
|
+
Lastly, you can get a list of all available badges (minus the disabled ones):
|
135
|
+
|
136
|
+
$ Badginator.badges
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
## Contributing
|
141
|
+
|
142
|
+
1. Fork it
|
143
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
144
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
145
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
146
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Badginator'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Bundler::GemHelper.install_tasks
|
18
|
+
|
19
|
+
require 'rspec'
|
20
|
+
require "rspec/core/rake_task"
|
21
|
+
RSpec::Core::RakeTask.new('spec')
|
22
|
+
|
23
|
+
|
24
|
+
task :default => :spec
|
25
|
+
|
26
|
+
|
data/badginator.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'badginator/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "badginator"
|
7
|
+
spec.version = Badginator::VERSION
|
8
|
+
spec.authors = ["Steven Hilton"]
|
9
|
+
spec.email = ["mshiltonj@gmail.com"]
|
10
|
+
spec.description = %q{Add "badges" to any Rails model.}
|
11
|
+
spec.summary = %q{Badginator is a gem to add "badges" (or "trophies" or "achievements") to any model of a Rails application, like User or Player. Useful for game-oriented applications.}
|
12
|
+
spec.homepage = "https://github.com/mshiltonj/badginator"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rails", "~> 4.0.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency "sqlite3"
|
23
|
+
spec.add_development_dependency "awesome_print"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "rspec-rails"
|
26
|
+
spec.add_development_dependency "observr"
|
27
|
+
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Badginator
|
2
|
+
class Badge
|
3
|
+
|
4
|
+
def self.setters(*method_names)
|
5
|
+
method_names.each do |name|
|
6
|
+
send :define_method, name do |*data|
|
7
|
+
if data.length > 0
|
8
|
+
instance_variable_set "@#{name}", data.first
|
9
|
+
else
|
10
|
+
instance_variable_get "@#{name}"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
setters :code, :name, :description, :condition, :disabled, :level, :image, :reward
|
18
|
+
|
19
|
+
|
20
|
+
def build_badge(&block)
|
21
|
+
instance_eval &block
|
22
|
+
@code = @code.to_sym if @code
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Badginator
|
2
|
+
module Nominee
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval {
|
6
|
+
has_many :badges, class_name: "AwardedBadge", as: :awardee
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def try_award_badge(badge_name, context = {})
|
11
|
+
badge = Badginator.get_badge(badge_name)
|
12
|
+
|
13
|
+
success = badge.condition.call(self, context)
|
14
|
+
|
15
|
+
if success
|
16
|
+
if (self.has_badge?(badge_name))
|
17
|
+
status = Badginator::Status(Badginator::ALREADY_WON)
|
18
|
+
else
|
19
|
+
awarded_badge = AwardedBadge.create! awardee: self, badge_code: badge.code
|
20
|
+
status = Badginator::Status(Badginator::WON, awarded_badge)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
status = Badginator::Status(Badginator::DID_NOT_WIN)
|
24
|
+
end
|
25
|
+
|
26
|
+
status
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_badge?(badge_code)
|
30
|
+
AwardedBadge.where(badge_code: badge_code, awardee: self).first
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/badginator.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require "badginator/version"
|
3
|
+
require "badginator/badge"
|
4
|
+
require "badginator/status"
|
5
|
+
require "badginator/nominee"
|
6
|
+
|
7
|
+
class Badginator
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
DID_NOT_WIN = 1
|
11
|
+
WON = 2
|
12
|
+
ALREADY_WON = 3
|
13
|
+
ERROR = 4
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@badges = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_badge(badge_code)
|
20
|
+
@badges.fetch(badge_code)
|
21
|
+
end
|
22
|
+
|
23
|
+
def badges
|
24
|
+
@badges.values.select { |badge| ! badge.disabled }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.badges
|
28
|
+
self.instance.badges
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_badge(*args, &block)
|
32
|
+
badge = Badge.new
|
33
|
+
badge.build_badge &block
|
34
|
+
badge.freeze
|
35
|
+
|
36
|
+
if @badges.key?(badge.code)
|
37
|
+
raise "badge code '#{badge.code}' already defined."
|
38
|
+
end
|
39
|
+
|
40
|
+
@badges[badge.code] = badge
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.define_badge(*args, &block)
|
44
|
+
self.instance.define_badge(*args, &block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.get_badge(badge_code)
|
48
|
+
self.instance.get_badge(badge_code)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.Status(status_code, badge = nil)
|
52
|
+
case status_code
|
53
|
+
when DID_NOT_WIN, WON, ALREADY_WON, ERROR
|
54
|
+
Badginator::Status.new code: status_code, badge: badge
|
55
|
+
else
|
56
|
+
rails TypeError, "Cannot convert #{status_code} to Status"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
class Badginator
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
source_root File.expand_path("../../templates", __FILE__)
|
6
|
+
|
7
|
+
def create_awarded_badges_model
|
8
|
+
copy_file "app/models/awarded_badge.rb", "app/models/awarded_badge.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_awarded_badges_migration
|
12
|
+
time_str = Time.now.strftime("%Y%m%d%H%M%S")
|
13
|
+
copy_file "db/migrate/create_table_awarded_badges.rb", "db/migrate/#{time_str}_create_table_awarded_badges.rb"
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateTableAwardedBadges < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :awarded_badges do |t|
|
4
|
+
t.integer :awardee_id
|
5
|
+
t.string :awardee_type
|
6
|
+
t.string :badge_code
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :awarded_badges, [:awardee_id, :awardee_type]
|
11
|
+
end
|
12
|
+
end
|
data/spec/badge_spec.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe "Badginator::Badge" do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
reset_badginator
|
7
|
+
Badginator.define_badge do
|
8
|
+
code :one
|
9
|
+
description "ONE"
|
10
|
+
condition ->(nominee, context) {
|
11
|
+
true
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
Badginator.define_badge do
|
16
|
+
code :two
|
17
|
+
description "TWO"
|
18
|
+
condition ->(nominee, context) {
|
19
|
+
true
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
Badginator.define_badge do
|
24
|
+
code :three
|
25
|
+
description "THREE"
|
26
|
+
condition ->(nominee, context) {
|
27
|
+
true
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
Badginator.define_badge do
|
32
|
+
code :four
|
33
|
+
description "FOUR"
|
34
|
+
condition ->(nominee, context) {
|
35
|
+
true
|
36
|
+
}
|
37
|
+
disabled true
|
38
|
+
end
|
39
|
+
|
40
|
+
Badginator.define_badge do
|
41
|
+
code :five
|
42
|
+
description "FIVE"
|
43
|
+
condition ->(nominee, context) {
|
44
|
+
true
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it "will show a list of all available badges" do
|
50
|
+
expect(Badginator.badges.size).to eql(4)
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe "Badginator" do
|
4
|
+
|
5
|
+
it "can reset the singleton for tests" do
|
6
|
+
badginator1 = Badginator.instance
|
7
|
+
badginator2 = Badginator.instance
|
8
|
+
|
9
|
+
expect(badginator1.object_id).to eql(badginator2.object_id)
|
10
|
+
|
11
|
+
reset_badginator
|
12
|
+
badginator3 = Badginator.instance
|
13
|
+
|
14
|
+
expect(badginator3.object_id).to_not eql(badginator2.object_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "when defining a badge" do
|
18
|
+
before(:each) do
|
19
|
+
reset_badginator
|
20
|
+
@thecode = thecode = :foo_badge
|
21
|
+
@thename = thename = "Foo"
|
22
|
+
|
23
|
+
@badge1 = Badginator.define_badge do
|
24
|
+
code thecode
|
25
|
+
name thename
|
26
|
+
description "Foo'd something"
|
27
|
+
image :s3
|
28
|
+
level 2
|
29
|
+
condition ->(nominee, context) {
|
30
|
+
return true
|
31
|
+
}
|
32
|
+
reward ->(nominee, context){
|
33
|
+
1
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "return a badge definition" do
|
39
|
+
expect(@badge1.name).to eql( @thename)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can call the condition" do
|
43
|
+
expect(@badge1.condition.call(nil, nil)).to be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "can not define the same badge code twice" do
|
47
|
+
|
48
|
+
expect {
|
49
|
+
thecode = @thecode
|
50
|
+
thename = @thename
|
51
|
+
other_badge = Badginator.define_badge do
|
52
|
+
code thecode
|
53
|
+
name thename
|
54
|
+
description "Foo'd something"
|
55
|
+
condition ->(nominee, context) {
|
56
|
+
return true
|
57
|
+
}
|
58
|
+
end
|
59
|
+
}.to raise_error
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "when awarding a badge" do
|
63
|
+
before(:each) do
|
64
|
+
|
65
|
+
@badge_to_win = badge_to_win = :contest_winner
|
66
|
+
|
67
|
+
@badge2 = Badginator.define_badge do
|
68
|
+
code badge_to_win
|
69
|
+
name "Won A Contest"
|
70
|
+
description "We have a winner over here"
|
71
|
+
condition ->(nominee, context) {
|
72
|
+
nominee.winner?
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
@nominee = double('nominee')
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should check for a winning condition" do
|
81
|
+
@nominee = Nominee.new
|
82
|
+
@nominee.should_receive(:winner?).and_return(true)
|
83
|
+
|
84
|
+
awarded_badge = AwardedBadge.new(awardee: @nominee, badge_code: @badge_to_win)
|
85
|
+
@nominee.should_receive(:has_badge?).and_return(false)
|
86
|
+
|
87
|
+
status = @nominee.try_award_badge(@badge_to_win)
|
88
|
+
|
89
|
+
expect(status.code).to eql(Badginator::WON)
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should return a did not win status if the conditions is not met" do
|
94
|
+
@nominee = Nominee.new
|
95
|
+
@nominee.should_receive(:winner?).and_return(false)
|
96
|
+
|
97
|
+
status = @nominee.try_award_badge(@badge_to_win)
|
98
|
+
expect(status.code).to eql(Badginator::DID_NOT_WIN)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should check for already won condition" do
|
102
|
+
@nominee = Nominee.new
|
103
|
+
@nominee.should_receive(:winner?).and_return(true)
|
104
|
+
|
105
|
+
awarded_badge = AwardedBadge.new(awardee: @nominee, badge_code: @badge_to_win)
|
106
|
+
@nominee.should_receive(:has_badge?).and_return(true)
|
107
|
+
|
108
|
+
status = @nominee.try_award_badge(@badge_to_win)
|
109
|
+
|
110
|
+
expect(status.code).to eql(Badginator::ALREADY_WON)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should return the badge if won" do
|
114
|
+
@nominee = Nominee.new
|
115
|
+
@nominee.should_receive(:winner?).and_return(true)
|
116
|
+
|
117
|
+
awarded_badge = AwardedBadge.new(awardee: @nominee, badge_code: @badge_to_win)
|
118
|
+
@nominee.should_receive(:has_badge?).and_return(false)
|
119
|
+
|
120
|
+
status = @nominee.try_award_badge(@badge_to_win)
|
121
|
+
expect(status.awarded_badge.badge.code).to eql(awarded_badge.badge.code)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
log/*
|
data/spec/dummy/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
File without changes
|