Skip to content

Instantly share code, notes, and snippets.

View joselo's full-sized avatar

Jose Carrion joselo

View GitHub Profile
@joselo
joselo / elements.md
Created November 19, 2011 18:04 — forked from soveran/elements.md
Excerpts from The Elements of Programming Style. The source of this compilation is unknown.

The Elements of Programming Style

The following rules of programming style are excerpted from the book "The Elements of Programming Style" by Kernighan and Plauger, published by McGraw Hill. Here is quote from the book: "To paraphrase an observation in The Elements of Style by Strunk and White, the rules of programming style, like those of English, are sometimes broken, even by the best writers. When a rule is broken, however, you will usually find in the program some compensating merit, attained at the cost of the violation. Unless you are certain of doing as well, you will probably do best to follow the rules."

@joselo
joselo / new.rb
Created January 5, 2012 21:41 — forked from jferris/new.rb
New FactoryGirl definition syntax
FactoryGirl.define do
sequence :email do |n|
"user#{n}@example.com"
end
factory :user do
email
password "test"
aliased_as :author
end
<% @models.each do |model| %>
<% if model.image_models.first %>
<% unless model.image_models.first.document_file_name.blank? %>
<%= image_tag model.image_models.first.permalink %>
<% end %>
<% end %>
<% end %>
#Cambiar en el model.rb
has_many :image_models, :as => :owner, :dependent => :destroy, :order => 'created_at DESC'
# En el model.rb, nota que cambie created_at DESC por ASC
has_many :image_models, :as => :owner, :dependent => :destroy, :order => 'created_at ASC'
# agrega este metodo en le modelo model.rb
def first_image_permalink
image = image_models.first
image.permalink if image && !image.document_file_name.blank?
end
#Finalmente de la vista se llamaria asi
@joselo
joselo / category.rb
Created February 9, 2012 03:29
Category
class Category < ActiveRecord::Base
has_many :children, :class_name => "Category", :dependent => :destroy, :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Category"
end
@joselo
joselo / gist:1942709
Created February 29, 2012 17:25 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@joselo
joselo / gist:1970562
Created March 4, 2012 03:40
Writing readable ruby
# Writing readable ruby
# The bad way
def to_hash
hash = {}
@reports.each do |report|
hash.merge! report.to_hash
end
hash
@joselo
joselo / deploy.rb
Created March 22, 2012 15:44 — forked from brutuscat/deploy.rb
Simple Sunspot Solr Capistrano Tasks
# Simple Start and Stop tasks for your Solr server.
# 1 - Download your Solr from http://www.apache.org/dyn/closer.cgi/lucene/solr/ and extract it somewhere
# 2 - Set the :solr_path with the full path to the Solr "example" dir
#
# NOTES:
# - sunspot_solr gem must be in your Gemfile
# - It will run the "example" Jetty-embed Solr server (start.jar)
# - It will automatically pick your project Solr config from your "project_path/solr" with the Sunspot schema and files.
@joselo
joselo / solr_cap.rb
Created March 22, 2012 15:47 — forked from doitian/solr_cap.rb
sunspot solr in capistrano
namespace :deploy do
task :setup_solr_data_dir do
run "mkdir -p #{shared_path}/solr/data"
end
end
namespace :solr do
desc "start solr"
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec sunspot-solr start --port=8983 --data-directory=#{shared_path}/solr/data --pid-dir=#{shared_path}/pids"
@joselo
joselo / minitest_spec_expectations.md
Created March 22, 2012 19:53 — forked from ordinaryzelig/minitest_spec_expectations.md
How to write MiniTest::Spec expectations

I'm a fan of MiniTest::Spec. It strikes a nice balance between the simplicity of TestUnit and the readable syntax of RSpec. When I first switched from RSpec to MiniTest::Spec, one thing I was worried I would miss was the ability to add matchers. (A note in terminology: "matchers" in MiniTest::Spec refer to something completely different than "matchers" in RSpec. I won't get into it, but from now on, let's use the proper term: "expectations").

Understanding MiniTest::Expectations

Let's take a look in the code (I'm specifically referring to the gem, not the standard library that's built into Ruby 1.9):

# minitest/spec.rb

module MiniTest::Expectations