2015-04-02 88 views
0

替換數組值我有一個電子郵件列表的YML文件,我試圖做一個輔助文件中的一些修正,但我似乎無法更新,並保持這種更新的信息從陽明

我需要改變在配置文件上發送的環境電子郵件基礎。 我最大的問題是,更新信息不被除生產我希望所有的電子郵件,得到了DEV-電子郵件地址,但在生產中我可以做一個eval()裝載其它所有環境中保存的file_data陣列

上從配置文件中的電子郵件

例YML文件

#config/brands_mailer.yml 
brand1: 
    support: 'Appname::Application.config.support_email' 
    sales: 'Appname::Application.config.email' 
    accounting: 'Appname::Application.config.accounting_email' 
brand2: 
    support: 'Appname::Application.config.barnd2_support_email' 
    sales: 'Appname::Application.config.barnd2_email' 
    accounting: 'Appname::Application.config.barnd2_accounting_email' 
brand3: 
    support: 'Appname::Application.config.barnd3_support_email' 
    sales: 'Appname::Application.config.barnd3_email' 
    accounting: 'Appname::Application.config.barnd3_accounting_email' 

現在我有一個郵件助手,看起來像

# app/helpers/mailers/mailr_helper.rb 

module Mailers 
    module MailrHelper 
    def get_brand_emails(options ={}) 
     file_data = YAML.load_file(File.join(Rails.root, 'config','brands_mailer.yml'))[options[:brand].to_s] 
     file_data.each do |fd| 
     unless Rails.env.production? 
      fd[1] << '[email protected]' # This appends to the current value 
      fd[1] = fd[1].gsub(fd[1], '[email protected]') # this changes the data but it does not persist 
     else 
      fd[1] << eval(fd[1]) 
     end 
     end 
     file_data 
    end  
end 
+0

您可以添加一些關於您正在嘗試做什麼的更多信息,以及具體問題是什麼,如果有的話? – 2015-04-02 13:15:24

+0

@MaxWilliams我更新了這個問題,更多的信息 – MZaragoza 2015-04-02 13:25:26

回答

0

這是我想出的解決方案。我還是想重構,使之清潔

應用程序/傭工/郵寄/ mailr_helper.rb

module Mailers 
    module MailrHelper 
    def get_brand_emails(options ={}) 
     file_data = YAML.load_file(File.join(Rails.root, 'config','brands_mailer.yml'))[options[:brand].to_s] 
     file_data.each do |fd| 
     unless Rails.env.production? 
      file_data['customer_support'] = "[email protected]" 
      file_data['new_pro']   = "[email protected]" 
      file_data['new_user']   = "[email protected]" 
     else 
      file_data['customer_support'] = eval(file_data['customer_support']) 
      file_data['new_pro']   = eval(file_data['new_pro']) 
      file_data['new_user']   = eval(file_data['new_user']) 
     end 
     end 
     file_data 
    end  
end 

這樣,我還是可以強制所有電子郵件要到一個地方,並在生產中僅轉發電子郵件

2

我強烈建議您在非生產環境的rails配置文件中使用郵件攔截器。有了這個功能,您就可以在所有環境中使用並測試相同的郵件進程,而無需擔心爲活動收件人生成電子郵件。 Here我已經描述了我傾向於爲我的應用程序設置郵件攔截器。

+2

謝謝,但我們在開發中使用郵件捕獲器,但我們仍然必須能夠改變這些值,因爲我們測試跨g-mail,雅虎,Hotmail,手機等。另外,我不想將郵件從暫存服務器發送給公司的銷售人員。 – MZaragoza 2015-04-02 14:14:30

+1

我認爲@MZaragoza得到的問題是,他似乎無法更新鍵值數組的值。用戶***郵件攔截器***或其他事情的業務決策並不真正相關。 – 2015-04-03 12:14:01

+1

@AldoDelgado,是的,現在從MZaragoza做出的迴應中顯而易見,但是當我發佈我的「解決方案」時並不是這樣。 – ReggieB 2015-04-04 13:33:10