Skip to content

Instantly share code, notes, and snippets.

@joakimk
joakimk / gist:5efe8d04e526d521e8c6
Last active August 29, 2015 14:17
Alternate heroku setup for circle
echo -e "machine api.heroku.com\n login $HEROKU_USER\n password $HEROKU_API_KEY\nmachine code.heroku.com\n login $HEROKU_USER\n password $HEROKU_API_KEY" > ~/.netrc && chmod 0600 /home/ubuntu/.netrc # We want to simply configure the API key, this seems easier than using the regular UI as that requires you to log in as a github user and assign yourself, etc.
@joakimk
joakimk / README.md
Last active January 1, 2022 23:21
CircleCI elixir build example

This runs a build for a small elixir (phoenix) project in about 40 seconds by caching as much of the compiled files as possible.

We've been using this for months in multiple projects without any issues. Please ping be if there is any issues with this script and I'll update it.

It should be generic enough to work on any elixir app using mix.

If you have a elixir_buildpack.config, then enable that section in the build script to keep versions in sync!

2016-08-09: Updated to newer Erlang and Elixir and fixed curl command.

@joakimk
joakimk / test_helper.exs
Created August 12, 2014 23:18
Starting a redis server when running elixir tests
Code.require_file "support/test_redis.exs", __DIR__
ExUnit.start
TestRedis.start

I just read http://re-factor.com/blog/2013/09/27/slow-tests-are-the-symptom-not-the-cause/.

I fully agree that the testing speed can often be a result of improving the code design.

This is probably how I'd implement a similar feature in barsoom.se's apps.

I'm mostly of the opinion that you should use the right tool for each job. Integrated tests to test integration. Unit tests to test logic and message passing.

I prefer to keep things simple and avoid dependency injection until there is a need for it outside of tests.

@joakimk
joakimk / app_mappers_application_mapper.rb
Created February 18, 2014 12:32
Using the app/records organization method with minimapper.
require "minimapper/mapper"
class ApplicationMapper < Minimapper::Mapper
def record_class
record_name = self.class.name.gsub(/Mapper/, "Record")
record_name.constantize
end
end
@joakimk
joakimk / gist:8703877
Last active August 29, 2015 13:55
Support unit testing AR models without loading all of rails
require "active_record"
# Also highly recommended:
# require "active_support/dependencies"
# ActiveSupport::Dependencies.autoload_paths += [ "app/models" ]
# ..so you don't need to do manual requires, found it to be just as fast.
# Support unit testing AR models without loading all of rails.
connection_info = YAML.load(ERB.new(File.read("config/database.yml")).result)["test"]
ActiveRecord::Base.establish_connection(connection_info)
@joakimk
joakimk / doc.md
Last active December 25, 2015 03:59
Using rails constant missing features (ActiveSupport::Dependencies) outside of rails

Using ActiveSupport::Dependencies outside of rails seems just as fast as doing manual requires. I've found this out by replacing hundreds of requires in a unit tested code base with ActiveSupport::Dependencies without any change to the time it takes to run the test suite.

Using ActiveSupport::Dependencies also allows you to have circular depedencies like class Foo::Bar in foo/bar.rb and class Foo in foo.rb that points to Foo::Bar in it's class definition (like in a rails validation).

The classical way of solving this without ActiveSupport::Dependencies is to do:

# foo/bar.rb
class Foo
 class Bar
@joakimk
joakimk / gist:6029651
Last active December 19, 2015 22:48
The functional core, imperative shell pattern as I see it. I use this quite a bit. Makes the code simpler to understand and test.
# Functional core
# Tested with unit tests. Mostly simple state based tests (like: invoice.gross_amount.should == 200).
class BuildInvoice
def build
# build invoice in memory..
end
end
# Imperative shell
# Usually just tested with a single test to ensure it stores data in the database
@joakimk
joakimk / gist:5999653
Created July 15, 2013 12:43
How to find schenker status for a hobbyking netherlands package via GLS.
http://privpakportal.schenker.nu/TrackAndTrace/packageinfo.aspx?packagesurrid=69682901&inpPackageId=<SSCC NO>
@joakimk
joakimk / README.md
Last active December 17, 2015 18:59
Extensions to minimapper's ActiveRecord mappers that haven't yet been included in minimapper or minimapper-extras (they are only tested as part of the project that uses them).

Association loading

# Load data from the DB
user = user_mapper.find(params[:id], include: { profile: :group })

# Use data in memory (you can't do lazy-loads with minimapper, which is probably
# a very good thing. Lets you avoid N+1 by default).
user.profile.group.name