Constants defined in describe blocks are global, so don’t do this:
Constants defined in describe blocks are global, so don’t do this:
# test/karma.conf.js
...
basePath: '<%= Bundler.root %>',
...
files: [
'<%= resolve_asset('vis.js') %>',
'app/assets/javascripts/app.js',
'test/**/*_spec.js'
],
# Rakefile
namespace :test do
task js: :environment do
with_tmp_karma_config do |config|
sh "./node_modules/karma/bin/karma start #{config} --single-run"
end
end
private
def with_tmp_karma_config
Tempfile.open('karma.js') do |f|
f.write ERB.new(File.read('test/karma.conf.js')).result(binding)
f.flush
yield f.path
end
end
def resolve_asset(file)
Rails.application.assets.find_asset(file).to_a.first.pathname.to_s
end
end
Ran into a few gotschas while implementing this, so I wanted to share 🙂
– buttons need to be disabled via the button method
– e.originalEvent.detail holds progress information
$(document).on("upload:start", "form", function(e) {
$(e.target).prev().prev('img').hide(); // hide old image we are replacing
$(this).find("input[type=submit]").
button({disabled: true}). // do not let user press submit until image is uploaded
after('<img src="/images/loading.gif" />') // indicate we are waiting
});
$(document).on("upload:progress", "form", function(e) {
// http://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable
function humanFileSize(bytes) {
var thresh = 1024;
if(bytes < thresh) return bytes + ' B';
var units = ['kB','MB','GB','TB','PB','EB','ZB','YB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(bytes >= thresh);
return bytes.toFixed(1)+' '+units[u];
};
var detail = e.originalEvent.detail;
var percentage = Math.round((detail.loaded / detail.total) * 100);
$(e.target).next().text(percentage + "% of " + humanFileSize(detail.total))
});
$(document).on("upload:complete", "form", function(e) {
if(!$(this).find("input.uploading").length) {
$(this).find("input[type=submit]").
button({disabled: false}). // all images uploaded, user can submit the form
next().remove(); // remove loading
}
});
A few simple checks to make sure we are not regressing into slow boot times by preloading to many things. Needs to be the last initializer so it catches what all the others are doing -> zz.rb
Code
# config/initializers/zz.rb
if Rails.env.development?
# make sure we do not regress into slow startup time by preloading to much
Rails.configuration.after_initialize do
[
ActiveRecord::Base.send(:descendants).map(&:name),
ActionController::Base.descendants.map(&:name),
(File.basename($0) != "rake" && defined?(Rake) && "rake"),
].compact.flatten.each { |c| raise "#{c} should not be loaded" }
end
end
Result
config/initializers/zz.rb:9:in `block (2 levels) in ': User should not be loaded (RuntimeError)
Requirements
– Keeping secrets in the environment is convenient, but not every sub-process can be trusted.
– Fail fast when configuration is missing
Solution
def ENV.delete!(key)
delete(key) || raise(KeyError, "key not found: #{key.inspect}")
end
secret = ENV.delete!('SECRECT')