forked from pilotcreative/vote_fu
/
test_helper.rb
178 lines (149 loc) · 5.05 KB
/
test_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
require 'simplecov'
SimpleCov.start
require 'test/unit'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'active_record'
config = {
:database => 'thumbs_up_test',
:username => 'test'
}
case ENV['DB']
when 'mysql'
config = {
:adapter => 'mysql2',
:database => 'thumbs_up_test',
:username => 'test',
:password => 'test',
:socket => '/tmp/mysql.sock'
}
if ENV['TRAVIS']
config = {
:adapter => 'mysql2',
:database => 'thumbs_up_test',
:username => 'root'
}
end
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.drop_database(config[:database]) rescue nil
ActiveRecord::Base.connection.create_database(config[:database])
when 'postgres'
config = {
:adapter => 'postgresql',
:database => 'thumbs_up_test',
:username => 'test',
}
if ENV['TRAVIS']
config = {
:adapter => 'postgresql',
:database => 'thumbs_up_test',
:username => 'postgres',
}
end
ActiveRecord::Base.establish_connection(config.merge({ :database => 'postgres' }))
ActiveRecord::Base.connection.drop_database(config[:database])
ActiveRecord::Base.connection.create_database(config[:database])
when 'sqlite3'
config = {
:adapter => 'sqlite3',
:database => 'test.sqlite3',
:username => 'test',
}
end
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :votes, :force => true do |t|
t.boolean :vote, :default => false
t.references :voteable, :polymorphic => true, :null => false
t.references :voter, :polymorphic => true
t.timestamps
end
add_index :votes, [:voter_id, :voter_type]
add_index :votes, [:voteable_id, :voteable_type]
# Comment out the line below to allow multiple votes per voter on a single entity.
add_index :votes, [:voter_id, :voter_type, :voteable_id, :voteable_type], :unique => true, :name => 'fk_one_vote_per_user_per_entity'
create_table :users, :force => true do |t|
t.string :name
t.timestamps
end
create_table :items, :force => true do |t|
t.integer :user_id
t.string :name
t.string :description
end
create_table :other_items, :force => true do |t|
t.integer :user_id
t.string :name
t.string :description
end
create_table :user_customs, :force => true do |t|
t.string :name
t.timestamps
end
create_table :item_customs, :force => true do |t|
t.integer :user_id
t.string :name
t.string :description
end
create_table :other_item_customs, :force => true do |t|
t.integer :user_id
t.string :name
t.string :description
end
end
require 'thumbs_up'
class Vote < ActiveRecord::Base
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.name]) }
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.name]) }
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
scope :descending, order("created_at DESC")
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
attr_accessible :vote, :voter, :voteable
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
end
class Item < ActiveRecord::Base
# This is default, however because the setting is app-wide, and changed elsewhere, we need to be explicit
ThumbsUp.configuration.voteable_relationship_name = :votes
ThumbsUp.configuration.voter_relationship_name = :votes
acts_as_voteable
belongs_to :user
end
class OtherItem < ActiveRecord::Base
# This is default, however because the setting is app-wide, and changed elsewhere, we need to be explicit
ThumbsUp.configuration.voteable_relationship_name = :votes
ThumbsUp.configuration.voter_relationship_name = :votes
acts_as_voteable
belongs_to :user
end
class User < ActiveRecord::Base
# This is default, however because the setting is app-wide, and changed elsewhere, we need to be explicit
ThumbsUp.configuration.voteable_relationship_name = :votes
ThumbsUp.configuration.voter_relationship_name = :votes
acts_as_voter
has_many :items
has_karma(:items)
end
class ItemCustom < ActiveRecord::Base
ThumbsUp.configuration.voteable_relationship_name = :votes_on
ThumbsUp.configuration.voter_relationship_name = :votes_by
acts_as_voteable
belongs_to :user
end
class OtherItemCustom < ActiveRecord::Base
ThumbsUp.configuration.voteable_relationship_name = :votes_on
ThumbsUp.configuration.voter_relationship_name = :votes_by
acts_as_voteable
belongs_to :user
end
class UserCustom < ActiveRecord::Base
ThumbsUp.configuration.voteable_relationship_name = :votes_on
ThumbsUp.configuration.voter_relationship_name = :votes_by
acts_as_voter
has_many :items
has_karma(:items)
end
class Test::Unit::TestCase
end