Reputation: 1617
So I have an application where users can create emails, and I'd like them to be able to chose their email template design.
How might I implement this? I understand it may be a bit complex perhaps but I'm open to ideas.
newsletter.rb
class Newsletter < ActiveRecord::Base
has_many :subscriptions
has_many :users, through: :subscriptions
has_many :posts, dependent: :destroy
belongs_to :user
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_presence_of :image, :name
scope :for, ->(user) do
if user
Newsletter.all
end
end
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable
has_many :subscriptions
has_many :subscribed_newsletters, through: :subscriptions, class_name: "Newsletter"
has_many :newsletters
validates :email, uniqueness: true, presence: true
validates_presence_of :password, on: :create
end
post.rb <---- this is what would go inside the template, the content from the post model.
class Post < ActiveRecord::Base
belongs_to :newsletter
end
Upvotes: 1
Views: 73
Reputation: 2080
Lets assume it's a user preference of which template they wish to be sent.
User.newsletter_template = 'my_custom_template'
Now, create a generic activemailer template that simply renders the user's chosen template
newsletter_email.html.erb
<%= render "/some/path/#{user.newsletter_template}.html.erb" %>
my_custom_template.html.erb
<html>stuff</html>
my_other_template.html.erb
<html>alt template</html>
I know this isn't an in-depth explanation. Don't be shy to ask!
Upvotes: 2