Skip to content

Instantly share code, notes, and snippets.

View remi's full-sized avatar
👋

Rémi Prévost remi

👋
View GitHub Profile
@remi
remi / migrate.rake
Created September 11, 2013 18:18
Migrate data from two ActiveRecord databases. Works with ActiveRecord 3 and above.
# Migrate data from two ActiveRecord databases.
# Works with ActiveRecord 3 and above.
#
# Written by Rémi Prévost
# Based on the work of Rama McIntosh & Mike Castleman
#
# This task is released under this BSD license:
#
# Copyright (c) 2008, Matson Systems, Inc.
# Copyright (c) 2010, Roasted Vermicelli LLC.
@remi
remi / README.md
Last active July 1, 2018 21:11
ActiveRecord and multiple `belongs_to` associations

ActiveRecord and multiple belongs_to associations

When an ActiveRecord model has more than one belongs_to association, calling delete_all on an ActiveRecord::Associations::CollectionProxy will not delete the records but unassociate them. That might be a problem in a table with a NOT NULL clause.

Client.create
# => #<Client id: 1, created_at: "2013-08-14 17:43:42", updated_at: "2013-08-14 17:43:42">
 
Team.create
# => #<Team id: 1, created_at: "2013-08-14 17:43:55", updated_at: "2013-08-14 17:43:55">
@remi
remi / date_validator.rb
Created August 9, 2013 18:26
An ActiveModel validator to make sure we pass a parseable string to a date field.
class DateValidator < ActiveModel::EachValidator
# We have to use our own `allow_blank_before_type_cast` option instead of the
# standard `allow_blank`, because Rails calls `#blank?` on the cast value and
# invalid cast dates are always blank (`nil`).
def initialize(options)
options.reverse_merge!(message: :invalid_date, allow_blank_before_type_cast: false)
super
end
def validate_each(record, attribute, value)
h = Hashie::Mash.new(options: { flatten: false, annotated: false })
h.options.annotated # => false
h.options.flatten # => ["flatten", false, "annotated", false]
!!h.options.annotated # => false
!!h.options.flatten # => true
!!h.options[:annotated] # => false
!!h.options[:flatten] # => false
@remi
remi / citizen.rb
Last active December 19, 2015 06:09
class Citizen
attr_reader :name
def initialize(opts = {})
@persisted = opts[:persisted]
end
def persisted?
!!@persisted
end
describe :scope do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.adapter :test do |stub|
stub.get("/users?enabled=true") { |env| ok! [] }
end
end
module Namespace
@remi
remi / reports_controller_spec.rb
Last active December 14, 2015 03:19
let, let, let, let, let, let, let.
describe ReportsController do
subject { last_response }
describe :create do
let(:payload) { { :serial => serial, :version => "1.0.0" } }
before { post reports_path, payload.to_json }
context "with present serial" do
let(:serial) { "123abc" }
@remi
remi / challenge.md
Last active December 10, 2015 14:08 — forked from rafbm/challenge.md

CSS Challenge #2

You start with this:

And must end up with this:

@remi
remi / challenge.md
Last active December 10, 2015 01:08 — forked from rafbm/challenge.md

CSS Challenge

You start with this:

<style>
  div {
    display: inline-block;
    width: 250px;
 padding: 20px;
@remi
remi / her-multipart.rb
Created December 12, 2012 02:35
Multipart HTTP Requests with Her
Her::API.setup :url => 'http://api.example.com' do |connection|
connection.use Faraday::Request::Multipart
connection.use Her::Middleware::DefaultParseJSON
connection.use Faraday::Adapter::NetHttp
end
class User
include Her::Model
end