Module: Model::TemplateConfig::TemplatePublic

Extended by:
ActiveSupport::Concern
Included in:
Wechat::TemplatePublic
Defined in:
app/models/wechat/model/template_config/template_public.rb

Instance Method Summary collapse

Instance Method Details

#data_hashObject



8
9
10
11
12
13
14
15
# File 'app/models/wechat/model/template_config/template_public.rb', line 8

def data_hash
  r = {}
  template_key_words.each do |i|
    r.merge! i.name => { value: i.mapping, color: i.color }
  end

  r
end

#sync_key_wordsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/wechat/model/template_config/template_public.rb', line 17

def sync_key_words
  if tid.present? && app
    template = app.api.templates.find { |i| i['template_id'] == tid }
    if template.blank?
      result = app.api.add_template tid
      template = app.api.templates.find { |i| i['template_id'] == result['template_id'] }
      app.api.del_template result['template_id']
    end
    self.update content: template['content']
  end

  return if content.blank?
  data_keys = Template.new(content: content).data_keys
  data_keys.each do |key|
    tkw = template_key_words.find_or_initialize_by(name: key)
    tkw.save
  end
end