This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
spec_helper.rb
206 lines (170 loc) · 5.14 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
##
# Copyright 2012 Twitter, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
require 'active_record'
require 'database_cleaner'
require 'sqlite3'
require 'reputation_system'
DELTA = 0.000001
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :rs_evaluations do |t|
t.string :reputation_name
t.references :source, :polymorphic => true
t.references :target, :polymorphic => true
t.float :value, :default => 0
t.timestamps
end
add_index :rs_evaluations, :reputation_name
add_index :rs_evaluations, [:target_id, :target_type]
add_index :rs_evaluations, [:source_id, :source_type]
create_table :rs_reputations do |t|
t.string :reputation_name
t.float :value, :default => 0
t.string :aggregated_by
t.references :target, :polymorphic => true
t.boolean :active, :default => true
t.timestamps
end
add_index :rs_reputations, :reputation_name
add_index :rs_reputations, [:target_id, :target_type]
create_table :rs_reputation_messages do |t|
t.references :sender, :polymorphic => true
t.integer :receiver_id
t.float :weight, :default => 1
t.timestamps
end
add_index :rs_reputation_messages, [:sender_id, :sender_type]
add_index :rs_reputation_messages, :receiver_id
create_table :users do |t|
t.string :name
t.timestamps
end
create_table :answers do |t|
t.integer :author_id
t.integer :question_id
t.string :text
t.timestamps
end
create_table :questions do |t|
t.integer :author_id
t.string :text
t.timestamps
end
create_table :phrases do |t|
t.string :text
t.timestamps
end
create_table :translations do |t|
t.integer :user_id
t.integer :phrase_id
t.string :text
t.string :locale
t.timestamps
end
create_table :people do |t|
t.string :name
t.string :type
t.timestamps
end
end
class User < ActiveRecord::Base
has_many :answers, :foreign_key => 'author_id', :class_name => 'Answer'
has_many :questions, :foreign_key => 'author_id', :class_name => 'Question'
has_reputation :karma,
:source => [
{ :reputation => :question_karma },
{ :reputation => :answer_karma, :weight => 0.2 }],
:aggregated_by => :product
has_reputation :question_karma,
:source => { :reputation => :total_votes, :of => :questions },
:aggregated_by => :sum
has_reputation :answer_karma,
:source => { :reputation => :weighted_avg_rating, :of => :answers },
:aggregated_by => :average
end
class Question < ActiveRecord::Base
belongs_to :author, :class_name => 'User'
has_many :answers
has_reputation :total_votes,
:source => :user,
:source_of => { :reputation => :question_karma, :of => :author }
has_reputation :difficulty,
:source => :user,
:aggregated_by => :average
end
class Answer < ActiveRecord::Base
belongs_to :author, :class_name => 'User'
belongs_to :question
has_reputation :weighted_avg_rating,
:source => [
{ :reputation => :avg_rating },
{ :reputation => :difficulty, :of => :question }],
:aggregated_by => :product,
:source_of => { :reputation => :answer_karma, :of => :author }
has_reputation :avg_rating,
:source => :user,
:aggregated_by => :average
end
class Phrase < ActiveRecord::Base
has_many :translations do
def for(locale)
self.find_all_by_locale(locale.to_s)
end
end
has_reputation :maturity_all,
:source => [
{ :reputation => :maturity, :of => :self, :scope => :ja },
{ :reputation => :maturity, :of => :self, :scope => :fr }],
:aggregated_by => :sum
has_reputation :maturity,
:source => { :reputation => :votes, :of => lambda {|this, s| this.translations.for(s)} },
:aggregated_by => :sum,
:scopes => [:ja, :fr, :de],
:source_of => { :reputation => :maturity_all, :of => :self, :defined_for_scope => [:ja, :fr] }
has_reputation :difficulty_with_scope,
:source => :user,
:aggregated_by => :average,
:scopes => [:s1, :s2, :s3]
end
class Translation < ActiveRecord::Base
belongs_to :user
belongs_to :phrase
has_reputation :votes,
:source => :user,
:aggregated_by => :sum,
:source_of => { :reputation => :maturity, :of => :phrase, :scope => :locale}
end
class Person < ActiveRecord::Base
has_reputation :leadership,
:source => :person,
:aggregated_by => :sum
end
class Programmer < Person
end
class Designer < Person
end