Skip to content

Instantly share code, notes, and snippets.

View equivalent's full-sized avatar
:octocat:
I'm IDDQD + IDKFA for Ruby on Rails

Tomas Valent equivalent

:octocat:
I'm IDDQD + IDKFA for Ruby on Rails
View GitHub Profile
@equivalent
equivalent / a-m-opiton.rb
Created August 21, 2012 10:30
dynamically defining STI + fixing Rails STI routing problem
class Option < ActiveRecord::Base
def self.sti_class_names
%w[Alert Foo Bar]
end
#define STIs
sti_class_names.each do |class_name|
new_class = Class.new(Option) do
def self.model_name
@equivalent
equivalent / model_spec.rb
Created August 23, 2012 15:46
FactoryGirl attributes_for giving only accessible attributes
describe SomeModel do
it do
p accessible_attributes_for(:contact)
end
end
@equivalent
equivalent / aes_test.rb
Created September 11, 2012 14:42 — forked from taylor/aes_test.rb
Ruby AES encryption test with lib openssl
#!/usr/bin/env ruby
# encoding: UTF-8
require 'rubygems'
begin
require 'bundler/setup'
rescue LoadError
end
require 'digest/sha1'
require 'openssl'
@equivalent
equivalent / 0-readme.md
Created September 14, 2012 12:45 — forked from burke/0-readme.md
ruby-1.9.3-p194 cumulative performance patch.

Patched ruby 1.9.3-p194 for 30% faster rails boot

Overview

This script installs a patched version of ruby 1.9.3-p194 with boot-time performance improvements (#66 and #68), and runtime performance improvements (#83 and #84). It also includes the new backported GC from ruby-trunk.

Many thanks to funny-falcon for the performance patches.

@equivalent
equivalent / README.md
Last active December 16, 2021 16:34
String "false" to_bool ... or how to convert Rails/SimpleForm radio buttons to boolean

This gist was writen in 2012 and it was solving specific problem in Rails & SimpleForm. Some fellow developers were pointing out this may be out dated concept. That's why I advise everyone to read comment section bellow to have a full grasp of alternative solutions

other sources that may be helpful to understand why this may not be best idea:

@equivalent
equivalent / custom_simple_form_helper.rb
Created December 12, 2012 11:44
simple "monkey patch" way how to generate simple_form form with class representing model name ('document_name') and action ('new') ... I'm sure there is better practice to do this but this is fine for my requirement app/helpers/custom_simple_form_helper.rb
module CustomSimpleFormHelper
def simple_form_for(object, *args, &block)
options = args.extract_options!
action_classes = [object.class.model_name.underscore , (object.persisted? ? 'edit' : 'new')]
options[:html]={} unless options[:html]
if options[:html].empty?
options[:html][:class] = action_classes
# The BootstrapBreadcrumbsBuilder is a Bootstrap compatible breadcrumb builder.
# It provides basic functionalities to render a breadcrumb navigation according to Bootstrap's conventions.
#
# BootstrapBreadcrumbsBuilder accepts a limited set of options:
# * separator: what should be displayed as a separator between elements
#
# You can use it with the :builder option on render_breadcrumbs:
# <%= render_breadcrumbs :builder => ::BootstrapBreadcrumbsBuilder, :separator => "&raquo;" %>
#
# Note: You may need to adjust the autoload_paths in your config/application.rb file for rails to load this class:

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
@equivalent
equivalent / app-model-document.rb
Last active October 29, 2018 07:47
Recursion example in Ruby on Rails model. Recursion will generate array of parents parents
class Document < ActiveRecord::Base
belongs_to :parent, class_name: 'Document'
def self.get_ancestors(who)
@tree ||= []
# @tree is instance variable of Document class object not document instance object
# so: Document.get_instance_variable('@tree')
if who.parent.nil?
@equivalent
equivalent / spec-lib-tasks-notification_rake_spec.rb
Created March 7, 2013 12:16
How to test rake tasks with RSpec
# spec/lib/tasks/notification_rake_spec.rb
require 'spec_helper'
describe 'notifications:weekly' do
include_context "rake"
let(:users){ [double(:user)] }
let(:user_deliveries){ double(:user_deliveries) }
before{ User.stub(:all).and_return( users ) }