This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
/
helper.rb
158 lines (129 loc) · 3.66 KB
/
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
require 'rubygems'
require 'tempfile'
require 'test/unit'
require 'shoulda'
require 'mocha'
case ENV['RAILS_VERSION']
when '2.1' then
gem 'activerecord', '~>2.1.0'
gem 'activesupport', '~>2.1.0'
when '3.0' then
gem 'activerecord', '~>3.0.0'
gem 'activesupport', '~>3.0.0'
else
gem 'activerecord', '~>2.3.0'
gem 'activesupport', '~>2.3.0'
end
require 'active_record'
require 'active_record/version'
require 'active_support'
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
`ruby -e 'exit 0'` # Prime $? with a value.
begin
require 'ruby-debug'
rescue LoadError => e
puts "debugger disabled"
end
ROOT = File.join(File.dirname(__FILE__), '..')
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
class Test::Unit::TestCase
def setup
silence_warnings do
Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
end
end
end
$LOAD_PATH << File.join(ROOT, 'lib')
$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
require File.join(ROOT, 'lib', 'paperclip.rb')
require 'shoulda_macros/paperclip'
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['test'])
def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip)
Object.send(:remove_const, class_name) rescue nil
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.class_eval{ include Paperclip }
klass
end
def reset_table table_name, &block
block ||= lambda { |table| true }
ActiveRecord::Base.connection.create_table :dummies, {:force => true}, &block
end
def modify_table table_name, &block
ActiveRecord::Base.connection.change_table :dummies, &block
end
def rebuild_model options = {}
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
table.column :other, :string
table.column :avatar_file_name, :string
table.column :avatar_content_type, :string
table.column :avatar_file_size, :integer
table.column :avatar_updated_at, :datetime
table.column :avatar_fingerprint, :string
end
rebuild_class options
end
def rebuild_class options = {}
ActiveRecord::Base.send(:include, Paperclip)
Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Dummy.class_eval do
include Paperclip
has_attached_file :avatar, options
end
end
class FakeModel
attr_accessor :avatar_file_name,
:avatar_file_size,
:avatar_last_updated,
:avatar_content_type,
:avatar_fingerprint,
:id
def errors
@errors ||= []
end
def run_paperclip_callbacks name, *args
end
end
def attachment options
Paperclip::Attachment.new(:avatar, FakeModel.new, options)
end
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
def should_accept_dummy_class
should "accept the class" do
assert_accepts @matcher, @dummy_class
end
should "accept an instance of that class" do
assert_accepts @matcher, @dummy_class.new
end
end
def should_reject_dummy_class
should "reject the class" do
assert_rejects @matcher, @dummy_class
end
should "reject an instance of that class" do
assert_rejects @matcher, @dummy_class.new
end
end
def with_exitstatus_returning(code)
saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
begin
`ruby -e 'exit #{code.to_i}'`
yield
ensure
`ruby -e 'exit #{saved_exitstatus.to_i}'`
end
end