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
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) { |
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
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); | |
} |
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
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 |
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 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 |
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
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 |
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
# 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: "[email protected]", from: "[email protected]", subject: "Example Message") do |format| | |
format.text { render text: "Example message body" } | |
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
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 |
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: | |
# <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? |
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
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 |
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
# 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 |
OlderNewer