module TheMerger
Constants
- VERSION
Public Instance Methods
field_selection()
click to toggle source
View Helper method (Optional)
Selection box with fields from your model and an insert button
# File lib/the_merger.rb, line 58 def field_selection body = select_tag :field, options_for_select(fields) body += button_tag "Insert", class: "insert_field" content_tag(:div, body, class: "merge_field") end
fields()
click to toggle source
Return all the fields from your configured model except created_at, updated_at & id
# File lib/the_merger.rb, line 67 def fields model.attribute_names.reject{|x| %w[created_at updated_at id].include?(x)} end
mail_merge(opts={})
click to toggle source
Merge the fields into the body and send emails
Options;
from subject body group (optional subset of users) single (optional single user)
# File lib/the_merger.rb, line 20 def mail_merge(opts={}) # Go through and get the collection to merge & send. if opts[:group] # Subset results = opts[:group] elsif opts[:single] # Single results = [opts[:single]] else # ALL entries in model results = model.all end # Go through all results, merge & send. results.each {|user| merge_and_send(opts[:from], opts[:subject], opts[:body].dup, user)} end
merge_and_send(from,subject,body,user)
click to toggle source
# File lib/the_merger.rb, line 35 def merge_and_send(from,subject,body,user) # Merge fields for this user into the body body = merge_fields(body, user) # Send the emails TheMerger::Mailer.batch_mail(from, subject, body, user).deliver end
merge_fields(body,user)
click to toggle source
Replace fields which are in square brackets with data from the chosen model.
# File lib/the_merger.rb, line 46 def merge_fields(body,user) fields.each do |field| body = body.gsub!("[#{field}]", user.send(field).to_s) || body end body end
Private Instance Methods
model()
click to toggle source
# File lib/the_merger.rb, line 73 def model if File.exists?(path = "#{Rails.root}/config/the_merger.yml") conf=YAML::load(IO.read(path)) conf["merge_model"].constantize else User end end