Created
April 22, 2011 19:33
-
-
Save ehlertij/937423 to your computer and use it in GitHub Desktop.
Mail Merge method
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
# 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