Skip to content

Instantly share code, notes, and snippets.

View Fitzsimmons's full-sized avatar

Justin Fitzsimmons Fitzsimmons

View GitHub Profile
@Fitzsimmons
Fitzsimmons / gist:3548815
Created August 31, 2012 03:41
Passing context in javascript
var that = this;
this.player.on("audio-ended", function() {
that.next();
});
//-------VERSUS-----------
this.player.on("audio-ended", function() {
this.next();
}, this);
@Fitzsimmons
Fitzsimmons / rich_domain_models2.md
Created September 24, 2012 17:05 — forked from vsavkin/rich_domain_models2.md
Building Rich Domain Models in Rails (revision 2)

Building Rich Domain Models in Rails

Part 1. Decoupling Persistence

Abstract

Domain model is an effective tool for software development. It can be used to express really complex business logic, and to verify and validate the understanding of the domain among stakeholders. Building rich domain models in Rails is hard. Primarily, because of Active Record, which doesn't play well with the domain model approach.

One way to deal with this problem is to use an ORM implementing the data mapper pattern. Unfortunately, there is no production ready ORM doing that for Ruby. DataMapper 2 is going to be the first one.

Another way is to use Active Record just as a persistence mechanism and build a rich domain model on top of it. That's what I'm going to talk about here.

@Fitzsimmons
Fitzsimmons / arel.rb
Created October 31, 2012 19:19
Modify an existing arel query to use the window function for the non_paged_count
results = arel.
select("COUNT(#{table_name}.id) OVER () AS non_paged_count, #{table_name}.*").
offset(offset).
limit(per_page).all
@Fitzsimmons
Fitzsimmons / pager.rb
Created October 31, 2012 19:20
Framework our quick hack to avoid the extra queries from Kaminari
class Pager
attr_reader :arel, :page, :per_page
def initialize(arel, options={})
@arel = arel
@page = Integer(options[:page]) rescue 1
@per_page = Integer(options[:per_page]) rescue arel.klass.default_per_page
end
def paged_results
non_paged_count | id [extra columns omitted]
-----------------+--------
130 | 239184
130 | 239183
[98 additional records omitted]
def index
arel = DocumentFilter.find(params)
@documents = Pager.new(arel, :page => params[:page]).paged_results
end
@Fitzsimmons
Fitzsimmons / example_migration.rb
Created November 23, 2012 22:06
A quick and dirty example of how to import mongo documents into a postgres hybrid document store
require 'pg'
require 'mongo'
require 'nokogiri'
mongo_conn = Mongo::Connection.new.db("qcloud_dev")
pg_conn = PG.connect(:dbname => "poc")
standards_collection = mongo_conn["standards"]
sheets_collection = mongo_conn["sheets"]
WITH unified_materials AS(
-- Find the base materials (used to calculate the 'base price' for an item?)
SELECT typename, quantity FROM invtypematerials AS mats
INNER JOIN invtypes ON invtypes.typeid = mats.materialtypeid
WHERE mats.typeid = 11993
UNION
-- Find the complex materials (Never present in T1 stuff)
SELECT invtypes.typename,
@Fitzsimmons
Fitzsimmons / Clojure Confusion
Created November 20, 2013 17:04
Clojure Confusion
Sometimes it works...
Digest: [18 8c ab df 4a 78 3a 31 2e 9a 57 94 a8 b 76 42 6c 12 3d 83]
Offset: 3
Excerpt: [df 4a 78 3a]
Unsigned: [5f 4a 78 3a]
Squashed: 5f4a783a
------------------
@Fitzsimmons
Fitzsimmons / next_break_return.txt
Last active December 29, 2015 13:49
Showing the various ways of returning a value from a block (or failing to do so).
irb(main):001:0> A = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> def test_next
irb(main):003:1> puts A.map{|x| next x}.inspect
irb(main):004:1> puts "Finished nexting!"
irb(main):005:1> end
=> nil
irb(main):006:0> test_next
[1, 2, 3]
Finished nexting!