Skip to content

Instantly share code, notes, and snippets.

@mgates
mgates / unvalidated.rake
Created June 15, 2011 18:26
A rake task to show validations
desc "show unvalidated attributes"
task :unvalidated => [:environment] do
tables = ActiveRecord::Base.connection.tables.sort
classes = Array.new
tables.each do |t|
begin
classes << (Kernel.const_get t.classify)
rescue NameError
end
end
@mgates
mgates / gist:6772055
Last active December 24, 2015 08:39
read.rb
require 'serialport'
require 'tempfile'
port_str = "/dev/rfcomm0"
sp = SerialPort.new(port_str)
sp.puts 'AT+CMGF=1'
loop do
@mgates
mgates / gist:8442629
Created January 15, 2014 19:22
If you need pg_dump on heroku....
It's super simple. You don't have to statically link it or anything. Just fire up an Ubuntu 10 LTS, build postgresql-9.1.11 and copy the pg_dump program into your app. All the libs it needs are in your container.
There is probably better ways to do this, but I wasn playing around, and here is what I came up with:
```ruby
module ErrorConverter
def with_exception(exception, method)
define_method "#{method}_with_fetch_exception" do |*args|
begin
send "#{method}_without_fetch_exception", *args
rescue KeyError
raise exception
@mgates
mgates / gist:11048927
Created April 18, 2014 15:06
How to find which gem is providing an asset
Rails.application.assets.paths.map {|d| Dir[d + '*/*']}.flatten.grep /block/
@mgates
mgates / gist:11264056
Last active August 29, 2015 14:00
Debugging callbacks in rails 4

When you have a misbehaving callback, you can add a warn in active_support/callback.rb in make_lambda(filter).

Someday maybe I'll make a gem to do this, but I just changed it to

        when Symbol
          lambda { |target, _, &blk| warn "Running symbol callback #{filter}"; target.send filter, &blk }
        when String
          l = eval "lambda { |value| warn \"Running eval callback #{filter}\";  #{filter} }"
@mgates
mgates / gist:f81ce99fbbf6762eed17
Created May 9, 2014 15:17
To get styling when you save_and_open_page
Just set
````
config.action_controller.asset_host = 'http://localhost:3000'
````
In config/enviroments/test.rb, and run your dev server

Partage API

The Partage API provides a simple and limited interface to the Partage brokerage system.

The latest version of this document can always be reached at http://gopartage.com/api.

Authentication

For now, authentication is done with an API token that is connected to your company.

@mgates
mgates / gist:a72a0a01915f122de3c8
Created October 14, 2014 15:03
If you need to change the backtrace silencers...
You can put this in an initializer
bc = Rails.backtrace_cleaner
bc.remove_silencers!
bc. add_silencer { |line| (line !~ bc.class::APP_DIRS_PATTERN && line !~ %r[^/?(vendor/gems/internal_gem)])}
@mgates
mgates / gist:eee93a87a8394d93bba7
Last active August 29, 2015 14:10
To add tempfile method to Rack::Test::UploadedFile to make it like ActionDispatch::Http::UploadedFile

The CSV.foreach method cexpects a string of a file to open. ActionDispatch::Http::UploadedFile has a #tempfile method which is good enough to pass into File.open like CSV.foreach does. Rack::Test::UploadedFile doesn't (and probably won't: rails/rails#799, rack/rack-test#30).

I added it like this:

# in spec/support/add_tempfile_to_test_uploaded_file.rb

class Rack::Test::UploadedFile
  def tempfile
    @tempfile
 end