Skip to content

Commit

Permalink
Merge pull request #2 from yamashun/send_message_by_other_than_e-mail
Browse files Browse the repository at this point in the history
#1 add module for templates other than mail
  • Loading branch information
yamashun committed May 5, 2019
2 parents 60f258f + 1f4e030 commit b7a0671
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/instantly_mail_changer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "instantly_mail_changer/version"
require "instantly_mail_changer/config"
require "instantly_mail_changer/deliverable"

module InstantlyMailChanger
class Error < StandardError; end
Expand Down
46 changes: 46 additions & 0 deletions lib/instantly_mail_changer/deliverable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module InstantlyMailChanger
module Deliverable
def self.included(klass)
klass.extend ClassMethods
end

module ClassMethods
def template_model(template_model)
@template_model_class = Object.const_get(template_model.to_s.camelize)
end

def template_columns(*args)
args.each do |column_name|
define_method "text_#{column_name.to_s}" do
generate_template(notice_template.send(column_name), @instance_for_template)
end
end
end
end

private

def notice_template
@notice_template ||= self.class.instance_variable_get("@template_model_class").find(@template_id)
end

def generate_template(template, obj)
merge_params(template, obj).each do |key, value|
template.gsub!(key, value.to_s)
end
template
end

def merge_params(body, obj)
params = {}
body.scan(/%{[\@\w\.]+}/).map do |key|
params[key] = nil
end
return params if params.size == 0
params.each do |key, _|
params[key] = eval(key[2..-2])
end
params
end
end
end
28 changes: 28 additions & 0 deletions spec/instantly_mail_changer/deliverable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RSpec.describe InstantlyMailChanger::Deliverable do
# Dummy classes is defined in 'spec/support/dummy_classes.rb'
describe 'class methods' do
it 'define template_model_class' do
expect(DummySendClass.instance_variable_get('@template_model_class'))
.to eq DummyTemplateModel
end

it 'define text_* methods' do
send_obj = DummySendClass.new(1, {})
expect(send_obj.respond_to?(:text_attr1)).to eq true
expect(send_obj.respond_to?(:text_attr2)).to eq true
end
end

describe 'instance methods' do
DummyObj = Struct.new(:title, :name, :url)
let(:template_obj) { DummyObj.new('Weekly', 'Weekly Digest', 'https://foo.bar.com') }

it 'generate text from template' do
send_obj = DummySendClass.new(1, template_obj)
expect(send_obj.send_something).to eq({
title: 'Weekly Digest',
body: 'Topic:Weekly Digest. URL:https://foo.bar.com',
})
end
end
end
8 changes: 2 additions & 6 deletions spec/instantly_mail_changer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
config.mailer_name = 'FlexibleMailer'
config.title_column = 'mail_subject'
config.body_column = 'mail_body'
config.template_model_name = 'DummyTemplateModel'
config.template_model_name = 'DummyMailTemplateModel'
config.mail_from_name = 'foo'
config.mail_from = 'bar@co.jp'
end

class DummyTemplateModel
class DummyMailTemplateModel
attr_accessor :id, :mail_subject, :mail_body

def initialize(**args)
Expand Down Expand Up @@ -64,10 +64,6 @@ def deliver_now
end
end

# after :each do
# remove_classes(DummyTemplateModel)
# end

DummyObj = Struct.new(:title, :name, :url)

it "call send_mail and interporate obj's method return values into template" do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "instantly_mail_changer"
require "action_mailer"

require "support/dummy_classes.rb"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down
32 changes: 32 additions & 0 deletions spec/support/dummy_classes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class DummyTemplateModel
attr_accessor :id, :attr1, :attr2

def initialize(**args)
args.each do |key, value|
eval("@#{key.to_s} = '#{value}'")
end
end

def self.find(id)
self.new(id: id, attr1: '%{obj.title} Digest', attr2: 'Topic:%{obj.name}. URL:%{obj.url}')
end
end

class DummySendClass
include InstantlyMailChanger::Deliverable

template_model :dummy_template_model
template_columns :attr1, :attr2

def initialize(template_id, template_obj)
@template_id = template_id
@instance_for_template = template_obj
end

def send_something
{
title: text_attr1,
body: text_attr2,
}
end
end

0 comments on commit b7a0671

Please sign in to comment.