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."
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
FactoryGirl.define do | |
sequence :email do |n| | |
"user#{n}@example.com" | |
end | |
factory :user do | |
password "test" | |
aliased_as :author | |
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
<% @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' |
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
# 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 |
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
class Category < ActiveRecord::Base | |
has_many :children, :class_name => "Category", :dependent => :destroy, :foreign_key => "parent_id" | |
belongs_to :parent, :class_name => "Category" | |
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
# Writing readable ruby | |
# The bad way | |
def to_hash | |
hash = {} | |
@reports.each do |report| | |
hash.merge! report.to_hash | |
end | |
hash |
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
# 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. | |
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
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" |
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").
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