Skip to content

Instantly share code, notes, and snippets.

View joselo's full-sized avatar

Jose Carrion joselo

View GitHub Profile
@joselo
joselo / gist:2370842
Created April 12, 2012 20:40
Minitest asserts
assert
assert_block
assert_empty
assert_equal
assert_in_delta
assert_in_epsilon
assert_includes
assert_instance_of
assert_kind_of
assert_match
@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
@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 / 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 / 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 / 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 / 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
# 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
<% @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'
@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