adhonorem 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +6 -0
- data/Rakefile +30 -0
- data/app/assets/javascripts/adhonorem/application.js +13 -0
- data/app/assets/stylesheets/adhonorem/application.css +15 -0
- data/app/controllers/adhonorem/application_controller.rb +5 -0
- data/app/views/layouts/adhonorem/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/lib/adhonorem/engine.rb +5 -0
- data/lib/adhonorem/exceptions.rb +6 -0
- data/lib/adhonorem/generators/adhonorem_generator.rb +21 -0
- data/lib/adhonorem/generators/templates/achievement_migration.rb +14 -0
- data/lib/adhonorem/generators/templates/initializer.rb +3 -0
- data/lib/adhonorem/generators/templates/progress_migration.rb +11 -0
- data/lib/adhonorem/models/achievement.rb +10 -0
- data/lib/adhonorem/models/badge.rb +240 -0
- data/lib/adhonorem/models/configuration.rb +10 -0
- data/lib/adhonorem/models/objective.rb +14 -0
- data/lib/adhonorem/models/progress.rb +48 -0
- data/lib/adhonorem/railtie.rb +12 -0
- data/lib/adhonorem/version.rb +3 -0
- data/lib/adhonorem.rb +32 -0
- data/lib/tasks/adhonorem_tasks.rake +4 -0
- data/spec/adhonorem_spec.rb +5 -0
- data/spec/helpers/badge_instantiator.rb +14 -0
- data/spec/helpers/migration_has_been_run.rb +28 -0
- data/spec/models/badge_spec.rb +63 -0
- data/spec/models/badge_trigger_spec.rb +53 -0
- data/spec/models/meta_badge_spec.rb +44 -0
- data/spec/rails_helper.rb +15 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/test_app/app/controllers/application_controller.rb +5 -0
- data/spec/test_app/app/helpers/application_helper.rb +2 -0
- data/spec/test_app/app/models/badges/conditionnal_badge.rb +20 -0
- data/spec/test_app/app/models/badges/legacy_badge.rb +20 -0
- data/spec/test_app/app/models/badges/meta_badge.rb +19 -0
- data/spec/test_app/app/models/badges/meta_children/sub_badge_one.rb +20 -0
- data/spec/test_app/app/models/badges/meta_children/sub_badge_two.rb +20 -0
- data/spec/test_app/app/models/badges/normal_badge.rb +20 -0
- data/spec/test_app/app/models/badges/parametered_badge.rb +33 -0
- data/spec/test_app/app/models/badges/two_objectives_badge.rb +25 -0
- data/spec/test_app/app/models/user.rb +2 -0
- data/spec/test_app/config/application.rb +16 -0
- data/spec/test_app/config/boot.rb +5 -0
- data/spec/test_app/config/environment.rb +5 -0
- data/spec/test_app/config/environments/development.rb +20 -0
- data/spec/test_app/config/environments/production.rb +25 -0
- data/spec/test_app/config/environments/test.rb +21 -0
- data/spec/test_app/config/initializers/adhonorem.rb +3 -0
- data/spec/test_app/config/initializers/assets.rb +1 -0
- data/spec/test_app/config/initializers/backtrace_silencers.rb +0 -0
- data/spec/test_app/config/initializers/cookies_serializer.rb +3 -0
- data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/test_app/config/initializers/inflections.rb +16 -0
- data/spec/test_app/config/initializers/mime_types.rb +4 -0
- data/spec/test_app/config/initializers/session_store.rb +3 -0
- data/spec/test_app/config/initializers/wrap_parameters.rb +3 -0
- data/spec/test_app/config/routes.rb +3 -0
- data/spec/test_app/db/migrate/20170103065319_create_users.rb +10 -0
- data/spec/test_app/db/migrate/20170103065400_create_adhonorem_progress.rb +10 -0
- data/spec/test_app/db/migrate/20170103065401_create_adhonorem_achievement.rb +12 -0
- data/spec/test_app/db/schema.rb +39 -0
- metadata +271 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9f326114945fed6499e4985c361ccea8375f8116
|
4
|
+
data.tar.gz: c2b90be5a62154750813b7d03fad2406040ad3bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 685fc34c29a634a9685728168c89344d63f6bf88b555f78fcdbea9460183ca1fb190e33096c76fa5800ca79dd209a647f526ce85d8b7b63ca7e8118e6085b079
|
7
|
+
data.tar.gz: d6e57fceb122c8e2557ce35b8c4bc5b32bcfa8897c0102f3b792d2bd252c95e071ed58add556b46cb3211df585612902bedccb64d79be84577e91a6bab080c80
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Hugo Chevalier
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
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 = 'StaticRecord'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path('../spec/test_app/Rakefile', __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake'
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
28
|
+
t.pattern = Dir.glob('../spec/**/*_spec.rb')
|
29
|
+
end
|
30
|
+
task default: :spec
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>AdHonorem</title>
|
5
|
+
<%= stylesheet_link_tag "adhonorem/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "adhonorem/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Adhonorem
|
2
|
+
class MigrationsGenerator < Rails::Generators::Base # :nodoc:
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def copy_files
|
6
|
+
time = Time.zone.now.strftime('%Y%m%d%H%M')
|
7
|
+
template 'progress_migration.rb', "db/migrate/#{time}00_create_adhonorem_progress.rb"
|
8
|
+
template 'achievement_migration.rb', "db/migrate/#{time}01_create_adhonorem_achievement.rb"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class InitializerGenerator < Rails::Generators::Base # :nodoc:
|
13
|
+
source_root File.expand_path('../templates', __FILE__)
|
14
|
+
|
15
|
+
def copy_files
|
16
|
+
@model_name = ask('How is your user model called? [User]')
|
17
|
+
@model_name = 'User' if @model_name.blank?
|
18
|
+
template 'initializer.rb', 'config/initializers/adhonorem.rb', @model_name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# AdHonorem::Achievement will let you know easily wich badges
|
2
|
+
# have been unlocked by a user and when
|
3
|
+
class CreateAdhonoremAchievement < ActiveRecord::Migration
|
4
|
+
def change
|
5
|
+
create_table :adhonorem_achievements do |t|
|
6
|
+
t.integer :user_id
|
7
|
+
t.integer :state, default: 0
|
8
|
+
t.datetime :unlocked_at
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
bind_static_record :adhonorem_achievements, :badge
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# AdHonorem::Progress will save the progress of your users for every badge
|
2
|
+
class CreateAdhonoremProgress < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
create_table :adhonorem_progresses do |t|
|
5
|
+
t.integer :user_id
|
6
|
+
t.string :objective_slug
|
7
|
+
t.integer :numeric_progress, default: 0
|
8
|
+
end
|
9
|
+
bind_static_record :adhonorem_progresses, :badge
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module AdHonorem
|
2
|
+
# Allows to get finished badges per user
|
3
|
+
class Achievement < ActiveRecord::Base
|
4
|
+
self.table_name = :adhonorem_achievements
|
5
|
+
has_static_record :badge, class_name: 'AdHonorem::Badge'
|
6
|
+
belongs_to :user, class_name: AdHonorem.configuration.user_class
|
7
|
+
|
8
|
+
enum state: [:started, :done]
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,240 @@
|
|
1
|
+
module AdHonorem
|
2
|
+
# AdHonorem core class, must be inherited from
|
3
|
+
class Badge < ::StaticRecord::Base
|
4
|
+
table :badges
|
5
|
+
path Rails.root.join('app', 'models', 'badges', '**', '*.rb')
|
6
|
+
primary_key :slug
|
7
|
+
|
8
|
+
attr_reader :objectives, :user
|
9
|
+
|
10
|
+
# After all badges loading, @@hooks will look like
|
11
|
+
# {
|
12
|
+
# master_a_weapon: {
|
13
|
+
# 'ParameteredBadge' => [:checker_one, :checker_two],
|
14
|
+
# 'LegacyBadge' => [:a_checker],
|
15
|
+
# },
|
16
|
+
# other_event: {
|
17
|
+
# 'OtherBadgeResponder' => [:other_checker]
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
@@hooks = {}
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
super
|
24
|
+
|
25
|
+
@sub_badges = []
|
26
|
+
@rewards = []
|
27
|
+
@objectives = {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_context(user)
|
31
|
+
@user = user
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def reload_context!
|
36
|
+
check_context
|
37
|
+
@user = AdHonorem.configuration.user_class.constantize.find(@user.id)
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_reward; end
|
42
|
+
|
43
|
+
def add_objective(slug, name, description, amount_needed = 1)
|
44
|
+
# prevent crash when Badges are initialized before migrations were applied
|
45
|
+
return unless defined?(AdHonorem::Objective)
|
46
|
+
@objectives[slug] = AdHonorem::Objective.new(slug, name, description, amount_needed)
|
47
|
+
end
|
48
|
+
|
49
|
+
def objectives_count
|
50
|
+
objectives.keys.count
|
51
|
+
end
|
52
|
+
|
53
|
+
def progress(progress_type = :step)
|
54
|
+
check_context
|
55
|
+
return progress_meta(progress_type) if meta?
|
56
|
+
|
57
|
+
case progress_type
|
58
|
+
when :step
|
59
|
+
objectives.keys.map do |slug|
|
60
|
+
name = objectives[slug].name
|
61
|
+
"#{name} : #{get_progression_for(slug).progress(:stringified)}"
|
62
|
+
end
|
63
|
+
when :global
|
64
|
+
objectives.keys.map { |slug| get_progression_for(slug).progress(:percentage) }.sum / objectives_count
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def legacy?
|
69
|
+
legacy || false
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.dispatch(user, event, params = nil)
|
73
|
+
params ||= {}
|
74
|
+
result = {}
|
75
|
+
|
76
|
+
@@hooks[event] ||= {}
|
77
|
+
@@hooks[event].each do |responder, registered_checkers|
|
78
|
+
badge = find_by(klass: responder).set_context(user)
|
79
|
+
registered_checkers.each do |checker|
|
80
|
+
res = badge.trigger(checker, params)
|
81
|
+
result[res] ||= []
|
82
|
+
result[res] << "#{responder}##{checker}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
result
|
87
|
+
end
|
88
|
+
|
89
|
+
def trigger(objective_slug, params = nil)
|
90
|
+
return trigger_meta(objective_slug, params || {}) if meta?
|
91
|
+
|
92
|
+
return :already_done if complete?(objective_slug) || complete?
|
93
|
+
return :legacy_badge if legacy?
|
94
|
+
|
95
|
+
params ||= {}
|
96
|
+
proceed(objective_slug, params)
|
97
|
+
end
|
98
|
+
|
99
|
+
def complete?(objective = nil)
|
100
|
+
# Meta badges don't handle per-objective completion
|
101
|
+
return complete_meta? if meta?
|
102
|
+
check_context
|
103
|
+
selection = objectives.keys
|
104
|
+
selection.select! { |slug| slug == objective } if objective
|
105
|
+
selection.each do |slug|
|
106
|
+
objective = objectives[slug]
|
107
|
+
return false unless get_progression_for(objective).done?
|
108
|
+
end
|
109
|
+
true
|
110
|
+
end
|
111
|
+
|
112
|
+
def get_objective(objective_slug)
|
113
|
+
objective = objective_slug
|
114
|
+
unless objective.is_a?(AdHonorem::Objective)
|
115
|
+
objective = objectives[objective_slug]
|
116
|
+
raise ObjectiveNotFound, "Objective #{objective_slug} could not be found" unless objective
|
117
|
+
end
|
118
|
+
|
119
|
+
objective
|
120
|
+
end
|
121
|
+
|
122
|
+
def meta?
|
123
|
+
!@sub_badges.empty?
|
124
|
+
end
|
125
|
+
|
126
|
+
class << self
|
127
|
+
protected
|
128
|
+
|
129
|
+
def hook(event, params = nil)
|
130
|
+
params ||= {}
|
131
|
+
@@hooks[event] ||= {}
|
132
|
+
@@hooks[event][name] ||= []
|
133
|
+
@@hooks[event][name] += [params[:to] || []].flatten.uniq
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
protected
|
138
|
+
|
139
|
+
def add_sub_badge(badge_slug)
|
140
|
+
@sub_badges << badge_slug
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def proceed(objective_slug, params)
|
146
|
+
objective = get_objective(objective_slug)
|
147
|
+
progression = get_progression_for(objective)
|
148
|
+
return :failed_check unless send(objective.slug, user, params[:data] || {})
|
149
|
+
|
150
|
+
progression.advance(params[:amount] || 1)
|
151
|
+
if complete?
|
152
|
+
unlock
|
153
|
+
reward
|
154
|
+
return :completed_badge
|
155
|
+
end
|
156
|
+
|
157
|
+
progression.done? ? :completed_step : :triggered
|
158
|
+
end
|
159
|
+
|
160
|
+
def get_progression_for(objective)
|
161
|
+
objective = get_objective(objective)
|
162
|
+
|
163
|
+
AdHonorem::Progress.find_or_initialize_by(
|
164
|
+
user: @user,
|
165
|
+
badge_static_record_type: self.class.name,
|
166
|
+
objective_slug: objective.slug.to_s
|
167
|
+
)
|
168
|
+
end
|
169
|
+
|
170
|
+
def related_achievement
|
171
|
+
AdHonorem::Achievement.find_or_initialize_by(
|
172
|
+
user: @user,
|
173
|
+
badge_static_record_type: self.class.name
|
174
|
+
)
|
175
|
+
end
|
176
|
+
|
177
|
+
def unlock
|
178
|
+
raise AdHonorem::NotCompleted unless complete?
|
179
|
+
achievement = related_achievement
|
180
|
+
achievement.done!
|
181
|
+
end
|
182
|
+
|
183
|
+
def trigger_meta(objective_slug, params)
|
184
|
+
@sub_badges.each do |sub_slug|
|
185
|
+
sub_badge = AdHonorem::Badge.find(sub_slug).set_context(@user)
|
186
|
+
next if sub_badge.complete? || !sub_badge.respond_to?(objective_slug)
|
187
|
+
sub_badge.trigger(objective_slug, params)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def complete_meta?
|
192
|
+
check_context
|
193
|
+
progress_meta(:global) == 100.0
|
194
|
+
end
|
195
|
+
|
196
|
+
def progress_meta(progress_type = :step)
|
197
|
+
check_context
|
198
|
+
|
199
|
+
case progress_type
|
200
|
+
when :step
|
201
|
+
@sub_badges.map { |sub_slug| AdHonorem::Badge.find(sub_slug).set_context(@user).progress(progress_type) }
|
202
|
+
when :global
|
203
|
+
res = @sub_badges.map do |sub_slug|
|
204
|
+
AdHonorem::Badge.find(sub_slug).set_context(@user).progress(progress_type)
|
205
|
+
end
|
206
|
+
res.sum / @sub_badges.size
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def next_sub_badge
|
211
|
+
@sub_badges.each do |sub_slug|
|
212
|
+
sub = AdHonorem::Badge.find(sub_slug).set_context(@user)
|
213
|
+
return sub unless sub.complete?
|
214
|
+
end
|
215
|
+
last_sub_badge
|
216
|
+
end
|
217
|
+
|
218
|
+
def last_sub_badge
|
219
|
+
AdHonorem::Badge.find(@sub_badges.last).set_context(@user)
|
220
|
+
end
|
221
|
+
|
222
|
+
def reward
|
223
|
+
# @rewards.each do |reward|
|
224
|
+
# end
|
225
|
+
end
|
226
|
+
|
227
|
+
def check_context
|
228
|
+
raise AdHonorem::NoContext, 'No context User has been set' unless @user
|
229
|
+
end
|
230
|
+
|
231
|
+
columns slug: :string,
|
232
|
+
name: :string,
|
233
|
+
description: :string,
|
234
|
+
category: :string,
|
235
|
+
points: :integer,
|
236
|
+
icon_locked: :string,
|
237
|
+
icon_unlocked: :string,
|
238
|
+
legacy: :boolean
|
239
|
+
end
|
240
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module AdHonorem
|
2
|
+
# Represent a badge objective
|
3
|
+
# Re-created on-the-fly at each badge instantiation
|
4
|
+
class Objective
|
5
|
+
attr_reader :slug, :name, :description, :amount_needed
|
6
|
+
|
7
|
+
def initialize(slug, name, description, amount_needed = 1)
|
8
|
+
@slug = slug
|
9
|
+
@name = name
|
10
|
+
@description = description
|
11
|
+
@amount_needed = amount_needed
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module AdHonorem
|
2
|
+
# Store users' progress in database for each badge triggered at least once
|
3
|
+
class Progress < ActiveRecord::Base
|
4
|
+
self.table_name = :adhonorem_progresses
|
5
|
+
has_static_record :badge, class_name: 'AdHonorem::Badge'
|
6
|
+
belongs_to :user, class_name: AdHonorem.configuration.user_class
|
7
|
+
|
8
|
+
PROGRESS_TYPES = {
|
9
|
+
numeric: :capped_numeric_progress,
|
10
|
+
percentage: :percentage_progress,
|
11
|
+
stringified: :stringified_progress
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
def advance(amount = 1)
|
15
|
+
return if done?
|
16
|
+
self.numeric_progress += amount
|
17
|
+
save
|
18
|
+
end
|
19
|
+
|
20
|
+
def objective
|
21
|
+
badge.objectives[objective_slug.to_sym]
|
22
|
+
end
|
23
|
+
|
24
|
+
def done?
|
25
|
+
numeric_progress >= objective.amount_needed
|
26
|
+
end
|
27
|
+
|
28
|
+
def progress(progress_type = :numeric)
|
29
|
+
raise AdHonorem::NotSuchProgressType unless PROGRESS_TYPES.keys.include?(progress_type)
|
30
|
+
send(PROGRESS_TYPES[progress_type])
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def percentage_progress
|
36
|
+
return 100.0 if objective.amount_needed.zero?
|
37
|
+
capped_numeric_progress.to_f / objective.amount_needed * 100.0
|
38
|
+
end
|
39
|
+
|
40
|
+
def stringified_progress
|
41
|
+
"#{capped_numeric_progress}/#{objective.amount_needed}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def capped_numeric_progress
|
45
|
+
[self.numeric_progress, objective.amount_needed].min
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Railtie < Rails::Railtie # :nodoc:
|
2
|
+
initializer 'adhonorem.load_static_record_dependent_files' do |_app|
|
3
|
+
ActiveSupport.on_load :static_record do
|
4
|
+
require 'adhonorem/models/objective'
|
5
|
+
require 'adhonorem/models/achievement'
|
6
|
+
require 'adhonorem/models/progress'
|
7
|
+
require 'adhonorem/models/badge'
|
8
|
+
|
9
|
+
ActiveSupport.run_load_hooks(:adhonorem, AdHonorem::Badge)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/adhonorem.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
require 'adhonorem/engine'
|
4
|
+
require 'adhonorem/models/configuration'
|
5
|
+
|
6
|
+
module AdHonorem # :nodoc:
|
7
|
+
class << self
|
8
|
+
attr_writer :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= AdHonorem::Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.reset
|
16
|
+
@configuration = AdHonorem::Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure
|
20
|
+
yield(configuration)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rails/generators'
|
25
|
+
require 'adhonorem/generators/adhonorem_generator'
|
26
|
+
|
27
|
+
require 'adhonorem/exceptions'
|
28
|
+
|
29
|
+
require 'active_record'
|
30
|
+
require 'static_record'
|
31
|
+
require 'adhonorem/railtie'
|
32
|
+
ActiveSupport.run_load_hooks(:static_record, StaticRecord::Base)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
def instantiate_badges
|
2
|
+
name = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
|
3
|
+
@user = User.create(name: name)
|
4
|
+
|
5
|
+
@normal_badge = AdHonorem::Badge.find('normal_badge').set_context(@user)
|
6
|
+
|
7
|
+
@two_objectives_badge = AdHonorem::Badge.find('two_objectives_badge').set_context(@user)
|
8
|
+
|
9
|
+
@conditionnal_badge = AdHonorem::Badge.find('conditionnal_badge').set_context(@user)
|
10
|
+
|
11
|
+
@legacy_badge = AdHonorem::Badge.find('legacy_badge').set_context(@user)
|
12
|
+
|
13
|
+
@parametered_badge = AdHonorem::Badge.find('parametered_badge').set_context(@user)
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
def migration_has_been_run?(version)
|
2
|
+
table_name = ActiveRecord::Migrator.schema_migrations_table_name
|
3
|
+
query = "SELECT version FROM %s WHERE version = '%s'" % [table_name, version]
|
4
|
+
ActiveRecord::Base.connection.clear_query_cache
|
5
|
+
ActiveRecord::Base.connection.execute(query).any?
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_migrate(migration_class, migration_number, way, reset_classes = nil)
|
9
|
+
reset_classes ||= []
|
10
|
+
|
11
|
+
verbosity = ActiveRecord::Migration.verbose
|
12
|
+
ActiveRecord::Migration.verbose = false
|
13
|
+
|
14
|
+
migration = migration_class.new
|
15
|
+
case way
|
16
|
+
when :up
|
17
|
+
migration.migrate(:up)# unless migration_has_been_run?(migration_number)
|
18
|
+
when :down
|
19
|
+
migration.migrate(:down)# if migration_has_been_run?(migration_number)
|
20
|
+
end
|
21
|
+
|
22
|
+
reset_classes.each do |klass|
|
23
|
+
klass.connection.schema_cache.clear!
|
24
|
+
klass.reset_column_information
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Migration.verbose = verbosity
|
28
|
+
end
|