-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from yamashun/send_message_by_other_than_e-mail
#1 add module for templates other than mail
- Loading branch information
Showing
6 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
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
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
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 |
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
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 |
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
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
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
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 |