List moved
Please use https://github.com/bf4/learning to fork and pull changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Yaml Recipes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - 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}"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |