Skip to content

Instantly share code, notes, and snippets.

@stepantubanov
Created March 9, 2012 13:26
Show Gist options
  • Save stepantubanov/2006496 to your computer and use it in GitHub Desktop.
Save stepantubanov/2006496 to your computer and use it in GitHub Desktop.
Invitation classes presenters
# Models
module Invitations
class Base
belongs_to :sender
belongs_to :recipient
end
class JoinGroup < Base
...
end
class BecomeManager < JoinGroup
...
end
end
# Each has different email subject and body.
# The problem: do not want to add "email_subject" and "email_template" fields to the models.
# Solution: May be something like
module Decorators
module Email
# Can actually place any decorators for that are used specifically for emails here
module Invitations
class Base
def subject
"Join our cool website" # Options: return I18n key "join_website", or the value h.t("....join_website")
end
def template
"invitation_base"
end
end
class JoinGroup < Base
def subject
"You have been invited to join #{group.name}"
end
def template
"invitation_join_group"
end
end
class BecomeManager < Base
def subject
# Example of some logic that might be used
if recipient.member_of?(group)
"You have been invited to manage #{subordinate.name}"
else
"You have been invited to join #{group.name} and manage #{subordinate.name}"
end
end
def template
"invitation_become_manager"
end
end
end
end
# We can actually place our presenters in a module like this one:
# removing word "Presenter" from the class name of course...
module Views
end
end
# And use them
invitation = "Decorators::Email::#{invitation.class.name}".constantize.new
mail(to: invitation.recipient_email, subject: invitation.subject, template_name: invitation.template)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment