Skip to content

Instantly share code, notes, and snippets.

@joakimk
joakimk / README.md
Last active Dec 4, 2020
CircleCI elixir build example
View README.md

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 Aug 12, 2014
Starting a redis server when running elixir tests
View test_helper.exs
Code.require_file "support/test_redis.exs", __DIR__
ExUnit.start
TestRedis.start
View 00_blog.md

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 Feb 18, 2014
Using the app/records organization method with minimapper.
View app_mappers_application_mapper.rb
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 Aug 29, 2015
Support unit testing AR models without loading all of rails
View gist:8703877
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 Dec 25, 2015
Using rails constant missing features (ActiveSupport::Dependencies) outside of rails
View doc.md

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 Dec 19, 2015
The functional core, imperative shell pattern as I see it. I use this quite a bit. Makes the code simpler to understand and test.
View gist:6029651
# 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 Jul 15, 2013
How to find schenker status for a hobbyking netherlands package via GLS.
View gist:5999653
http://privpakportal.schenker.nu/TrackAndTrace/packageinfo.aspx?packagesurrid=69682901&inpPackageId=<SSCC NO>
@joakimk
joakimk / README.md
Last active Dec 17, 2015
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).
View README.md

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
@joakimk
joakimk / code.rb
Created Mar 21, 2013
How to handle serialized associations with minimapper that can be validated.
View code.rb
class Address
include Minimapper::Entity
attribute :street
attribute :postalcode
attribute :city
attribute :country
validates :street, presence: true
end