This file contains 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
################## | |
# Mongoid # | |
################## | |
League.org_id(1) | |
#=> #<Mongoid::Criteria | |
# selector: {:org=>{"$in"=>[1]}}, | |
# options: {}, | |
# class: League, | |
# embedded: false> |
This file contains 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
# Tried on Rails 3.2.{11,12} so far | |
class Venue < ActiveRecord::Base | |
scope :org_id, ->(ids) { where org_id: Array(ids) } | |
end | |
Venue.org_id(1).org_id(2) | |
# => Venue Load (0.3ms) SELECT `venues`.* FROM `venues` WHERE `venues`.`org_id` IN (2) | |
Venue.where(org_id: [1]).where(org_id: [2]) | |
# => Venue Load (0.5ms) SELECT `venues`.* FROM `venues` WHERE `venues`.`org_id` IN (1) AND `venues`.`org_id` IN (2) |
This file contains 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
#!/usr/bin/env zsh | |
test_loc="./test/**/$1_test.rb" | |
eval "ruby -I'lib:test' $test_loc" | |
# Now you can: | |
# rtest user | |
# rtest user_controller |
This file contains 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
2.0.0p247 :021 > h = Hash.new([]) | |
# => {} | |
2.0.0p247 :022 > h[:test] | |
# => [] | |
2.0.0p247 :023 > h[:test] << "h" | |
# => ["h"] | |
2.0.0p247 :024 > h | |
# => {} | |
2.0.0p247 :025 > h.values | |
# => [] |
This file contains 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
module ApplicationHelper | |
def fetch_param(*key_names) | |
result = key_names.reduce(params) { |fetchable, param| | |
fetchable.fetch(param, {}) | |
} | |
result.is_a?(Hash) && result.empty? ? nil : result | |
end | |
end |
This file contains 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
User.find_by_sql( | |
%q{ | |
SELECT table_schema 'table', sum( data_length + index_length ) / 1024 / 1024 'size' | |
FROM information_schema.TABLES GROUP BY table_schema; | |
} | |
).each { |u| puts "#{u.table}: #{u.size} MB" } |
This file contains 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
# This option has the most clarity, but I generally hate local variables in a | |
# method, preferring to use tap. | |
def calculated_foo | |
available_foos = [] | |
available_foos << bar | |
available_foos << baz.qux if baz | |
available_foos.max | |
end | |
# It's a little less clear, unless you know what tap is, and `end.max` is |
This file contains 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
execute(<<-STR) | |
SELECT * | |
FROM * | |
STR | |
execute(<<-STR.gsub(/*/, ";DROP *;"), now: true) | |
SELECT * | |
FROM * | |
STR |
This file contains 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 Persona < Foundation::ServiceResource::Base | |
route "/users/:user_id/personas", :by_user | |
end | |
id = 1 | |
Persona.by_user id | |
Persona.by_user user_id: id |
This file contains 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 ConfigFile | |
attr_reader :config_name | |
attr_reader :context | |
def self.read_with_env(*args) | |
new(*args).read_with_env | |
end | |
def self.read(*args) | |
new(*args).read |