Skip to content

Instantly share code, notes, and snippets.

@bf4
bf4 / ruby_learning.md
Last active July 17, 2021 08:06
Some Ruby Learning Resources
@bf4
bf4 / sti_mti.rb
Created January 29, 2013 20:05
Multiple table inheritance with Rails
# from http://web.archive.org/web/20120406010350/http://mediumexposure.com/multiple-table-inheritance-active-record
# the below code creates a macro 'acts_as_product' that you can include in an AR model
# including 'acts_as_product' adds the attributes from the ProductProperties model/table
# so that, e.g. a Tee can have attributes that are a combination
# of the tees tables and the product_properties table
class ActiveRecord::Base
def self.acts_as_product
include Sellable
end
end
@bf4
bf4 / mti.rb
Last active December 11, 2015 22:08
Multiple table inheritance in Rails
# Multiple table inheritance in Rails
# from http://web.archive.org/web/20120406010350/http://mediumexposure.com/multiple-table-inheritance-active-record
# the below code creates a macro 'acts_as_title' that you can include in an AR model
# including 'acts_as_title' adds the attributes from the TitleProperties model/table
# so that, e.g. a TvTitle can have attributes that are a combination
# of the tv_titles table and the title_properties table
# and behaves more or less as a normal AR object.
#
# yeah this looks a little nuts and needs refactoring
@bf4
bf4 / polymorphic_many_to_many_in_rails.md
Last active February 6, 2024 21:13
a polymorphic many-to-many association in Rails

This was my solution for a polymorphic many-to-many association

class ItemCountry < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
  belongs_to :country
  # fields are :locatable_id, :locatable_type, :country_id
end

class Title < ActiveRecord::Base

has_many :countries, :through => :item_countries, :as => :locatable

@bf4
bf4 / better_rails_patterns.rb
Last active December 13, 2015 17:48
Limiting ActiveRecord database methods to certain objects as part of events or workflows
# http://engineering.nulogy.com/posts/hexagonal-architecture-for-rails-developers
class OrdersController < ApplicationController
#...
def create
create_order = CreateOrder.new(OrderRepository, self)
create_order.create current_user, params[:order]
end
def order_creation_succeeded order
@bf4
bf4 / YamlCookbook.md
Created March 17, 2013 21:04
Yaml Configuration

Yaml Recipes

@bf4
bf4 / ruby_data_uri.haml
Created March 18, 2013 17:20
Image to Data URI in ruby
- img_url = "http://example.com/image.jpg"
- img_type = img_url.split('.')[-1]
- img_binary = open(img_url).read
- img_data = ActiveSupport::Base64.encode64(img_binary).gsub("\n", '')
%img{src: "data:image/#{img_type};base64,#{img_data}"}
@bf4
bf4 / ruby_object_model.txt
Last active December 15, 2015 07:40
Ruby Object Model
I've found understanding the ruby object model to be * extremely* valuable
module ModMax; end
module ModMen
module OldFashioned; end
class Moddy < Waters; end
end
class ImYourDaddy; end
class KlassAct < ImYourDaddy
include ModMen
@bf4
bf4 / README.md
Last active December 15, 2015 18:19
RubyTapas non-video files Downloader