Skip to content

Instantly share code, notes, and snippets.

@dlt
dlt / gist:1191511
Created September 3, 2011 17:40
nested_attributes
class Product < ActiveRecord::Base
has_many :fields, :as => :attributable, :dependent => :destroy
accepts_nested_attributes_for :fields
end
class Field < ActiveRecord::Base
belongs_to :attributable, :polymorphic => true
belongs_to :column
scope :with_column, select("fields.*, columns.column_name AS 'name'").joins(:column)
def file_copy(src, dest)
File.open(dest, 'wb') { |f| f.puts(File.read(src)) }
end
def word_wrap(word, len = 22)
if word.size < len
word
else
new_word = []
parts = word.split(/\s/).map { |p| p.strip }
while part = parts.shift and (new_word.join(" ").size + part.size) < len
new_word << part
end
function Car () {
}
Car.prototype.extDirectOptions = function(options) {
this.extDirectOptions = options;
}
Generator.prototype.controllerMetadata = function() {
// find controller in config.controllersDir
class Product
include Mongoid::Document
referenced_in :group
end
class Group
include Mongoid::Document
references_many :products, :stored_as => :array, :inverse_of => :group
end
# Module Frequency
# A small dsl written in ruby to work with frequency events (never, sometimes, always..)
#
#
# Alteracoes:
# * removi "if block_given?" e passei &block como parametro para #execute_with_probability
# para remover duplicacao
# * mudei #correct_if_string
module Frequency
NORMALLY = 0.75