Skip to content

Instantly share code, notes, and snippets.

View barelyknown's full-sized avatar

Sean Devine barelyknown

View GitHub Profile
@barelyknown
barelyknown / gist:1369399
Created November 16, 2011 06:09
How do you check if a parent SObject has any children Sobjects in a VisualForce page without a wrapper class?
When looping through a set of Salesforce.com SObjects from a VisualForce page, you may want to test whether the SObject has any children without wrapping the SObjects in a wrapper class. Here's how:
Controller:
Create a property in the controller that is a map of the parent IDs and Boolean flag.
public Map<Id, Boolean> hasChildren {
get {
if (this.hasChildren == null) {
for (Parent__c parent : [select id, (select id from children__r) from parent__c]) {
if (parent.children__r.size() > 0) {
@barelyknown
barelyknown / gist:1522089
Created December 26, 2011 20:44
Textmate Snippet for Sobject Maps by Id
Map<Id, ${1/./\u$0/:enumerate}__c> $1Map = new Map<Id, ${1/./\u$0/}__c>();
for (${1/./\u$0/}__c $1: $1s) {
$1Map.put($1.id, $1);
}
@barelyknown
barelyknown / gist:2936281
Created June 15, 2012 12:41
Standardize Company Name for Searching
def standardize(query)
query.upcase! # IbM => IBM
query.gsub!(/(\A|(?<=\s))(\w)\s&\s(\w)(\z|(?=\s))/,"\\2&\\3") # A & P => A&P
query.gsub!(/((\A|(?<=[\s\.]))\w(\z|[\s\.]+))+/, query.scan(/((\A|(?<=[\s\.]))(\w)(\z|[\s\.]+))/).collect do |match|
match[2]
end.join + " ") # I.B.M. or I. B. M. => IBM
query.gsub!(/\s+/," ") # MY FIRST TRUCKING CO => MY FIRST TRUCKING CO
query.gsub!("'","") # AL'S TOY BARN => ALS TOY BARN
query.strip! # " IBM " => IBM
query
@barelyknown
barelyknown / cacheable.rb
Created August 24, 2012 21:09
Rails Cacheable Module
module Cacheable
extend ActiveSupport::Concern
def cacheback(relationship)
if relationship.cacheable_attributes.any?
relationship.target_attributes.each do |target_attribute|
relationship.target.each { |member| member.send("calc_#{target_attribute}")}
end
relationship.target.each(&:save!)
end
@barelyknown
barelyknown / gist:3649793
Created September 6, 2012 01:50
Approach to Getter/Setter for Hash Access
def earliest_window_end
@earliest_window_end ||= {}
end
def method_missing(method, *args, &block)
if method =~ /^(.+?)_earliest_window_end(\=*)$/
!$2.blank? ? earliest_window_end[$1] = args[0] : earliest_window_end[$1]
end
end
@barelyknown
barelyknown / gist:3692433
Created September 10, 2012 17:42
Send Email From Rails Console
# Simple approach to sending email from the Rails console
# Implementation idea courtesy of Steve Klabnik
# http://blog.steveklabnik.com/posts/2012-09-09-random-ruby-tricks--class-new
# Create the mailer class with a block and assign to a variable
mailer = Class.new(ActionMailer::Base) do
def example_message
mail(to: "test@test.com", from: "test@test.com", subject: "Example Message") do |format|
format.text { render text: "Example message body" }
end
@barelyknown
barelyknown / gist:3739741
Created September 17, 2012 21:01
space_case method for strings to humanize camelcase class names
class String
# Change "HelloWorld" into "Hello World" while keeping "HELLOWORLD" as "HELLOWORLD"
# Useful as an alternative to humanize when presenting a list of class names
def space_case
split(/((?<=[a-z])[A-Z])/).inject { |whole, part| whole += (part =~ /^[A-Z]$/) ? " #{part}" : part }
end
end
@barelyknown
barelyknown / gist:3790422
Created September 26, 2012 20:32
Decorator Modules
# This:
# <div name="#{@article.div_name"}>
# Instead of:
# <div name="<%= "#{@article.class.to_s.underscore}[#{@article.try(:id)}]"></div>
# What's the DRY way that you'd recommend to include this method (and other similar ideas) in every decorator?
@barelyknown
barelyknown / gist:3989109
Created October 31, 2012 18:58
Letterpress Simulation Output
Players playing an edge and corner strategy with player 2 looking for a 1st defended corner. Strategies defined as ruby procs:
EDGE_AND_CORNER = ->(move) do
move.score[:covered] + move.score[:defended] + move.score[:edges] + move.score[:corners]
end
CORNER_LOVER = ->(move) do
score = move.score[:covered] + move.score[:defended] + move.score[:edges] + move.score[:corners]
score += 10 if (move.score[:corners_defended_before] == 0 && move.score[:corners_defended_after] > 0)
score
@barelyknown
barelyknown / gist:3989169
Created October 31, 2012 19:08
Letterpress Simulation Output
# Any arbitrary strategy is supported. Some predefined strategies are included too.
module Linotype
class Strategy
EDGE_AND_CORNER = ->(move) do
move.score[:covered] + move.score[:defended] + move.score[:edges] + move.score[:corners]
end
CORNER_LOVER = ->(move) do
score = move.score[:covered] + move.score[:defended] + move.score[:edges] + move.score[:corners]
score += 10 if (move.score[:corners_defended_before] == 0 && move.score[:corners_defended_after] > 0)
score