Skip to content

Instantly share code, notes, and snippets.

@ehlertij
Created April 22, 2011 19:33
Show Gist options
  • Save ehlertij/937423 to your computer and use it in GitHub Desktop.
Save ehlertij/937423 to your computer and use it in GitHub Desktop.
Mail Merge method
# Performs a mail merge based on the options hash that is passed in
# Example: text_block.merged_content({:site => @site, :user => @user})
# will search text_block.content and find all instances of ::user.[method]::
# and ::site.[method]:: and send the [method] to the associated object
# Chaining works as well (ex. ::site.organization.name:: yields 'TST Media')
def merged_content(content, options = {})
merged = content
options.keys.each do |key|
obj = options[key]
merged = merged.gsub(/::#{key.to_s}\.[\w|\.]*::/) do |s|
res = s[/\..*\w/][/\w.*/].split('.').collect{|m| obj = obj.send(m)}.last
obj = options[key]
res
end
end
merged
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment