From f34206c467faee028f8d9c502d397052ae4fad63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20=C3=96zg=C3=BCr=20=C3=96ZKAN?= Date: Mon, 20 May 2013 05:13:20 +0300 Subject: [PATCH] #35 setup smtp, setup open_letter --- lib/cybele/app_builder.rb | 59 ++++++++++++++++++++++++++ lib/cybele/generators/app_generator.rb | 8 ++++ templates/config/initializers/mail.rb | 13 ++++++ templates/cybele_Gemfile | 4 +- 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 templates/config/initializers/mail.rb diff --git a/lib/cybele/app_builder.rb b/lib/cybele/app_builder.rb index eff947a..cd9de3e 100644 --- a/lib/cybele/app_builder.rb +++ b/lib/cybele/app_builder.rb @@ -105,8 +105,67 @@ def convert_application_css_to_sass copy_file 'app/assets/stylesheets/application.css.sass', 'app/assets/stylesheets/application.css.sass' end + # Interval: Configure smtp + def configure_smtp + copy_file 'config/initializers/mail.rb', 'config/initializers/mail.rb' + + prepend_file 'config/environments/production.rb', + "require Rails.root.join('config/initializers/mail')\n" + + config = <<-RUBY + + + # Mail Settings + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = MAIL_SETTING + RUBY + + inject_into_file 'config/environments/production.rb', config, + :after => 'config.action_mailer.raise_delivery_errors = false' + end + + # Interval: Configure action mailer + def configure_action_mailer + action_mailer_host 'development', "#{app_name}.dev" + action_mailer_host 'test', 'www.example.com' + action_mailer_host 'production', "#{app_name}.com" + end + + # Interval: Setup letter opener + def setup_letter_opener + config = 'config.action_mailer.delivery_method = :letter_opener' + configure_environment 'development', config + end + # Internal: Leftovers def leftovers end + + private + + # Internal: Set action mailer hostname + # + # rail_env - rails env like development, text, production + # host - domain.dev, domain.com or example.com + # + # Returns nothing + def action_mailer_host(rails_env, host) + config = "config.action_mailer.default_url_options = { host: '#{host}' }" + configure_environment(rails_env, config) + end + + # Internal: Set configure environment + # + # rail_env - rails env like development, text, production + # config - config string which will add to rails_env file + # + # Return nothing + def configure_environment(rails_env, config) + inject_into_file( + "config/environments/#{rails_env}.rb", + "\n\n #{config}", + before: "\nend" + ) + end end end \ No newline at end of file diff --git a/lib/cybele/generators/app_generator.rb b/lib/cybele/generators/app_generator.rb index b1fa516..be48f94 100644 --- a/lib/cybele/generators/app_generator.rb +++ b/lib/cybele/generators/app_generator.rb @@ -31,6 +31,7 @@ def customization invoke :install_gems invoke :gitignore_files_and_folders invoke :setup_bootstrap_sass_coffee + invoke :configure_mail_setting end # Internal: Customize gemfile @@ -86,6 +87,13 @@ def setup_bootstrap_sass_coffee build :convert_application_css_to_sass end + # Internal: Setup mail setting + def configure_mail_setting + build :configure_mail_setting + build :configure_action_mailer + build :setup_letter_opener + end + # Internal: Let's not: We'll bundle manually at the right spot. def run_bundle diff --git a/templates/config/initializers/mail.rb b/templates/config/initializers/mail.rb new file mode 100644 index 0000000..4f267dd --- /dev/null +++ b/templates/config/initializers/mail.rb @@ -0,0 +1,13 @@ +if Rails.env.production? + MAIL_SETTING = { + address: ENV['MAIL_ADDRESS'], # example 'smtp.mandrillapp.com' + port: '587', + enable_starttls_auto: true, + domain: ENV['MAIL_DOMAIN'], # example 'domain.com' + user_name: ENV['MAIL_USERNAME'], # example 'email@gmail.com' + password: ENV['MAIL_PASSWORD'], + authentication: 'plain' + } +end + + diff --git a/templates/cybele_Gemfile b/templates/cybele_Gemfile index f85d810..7d6684b 100644 --- a/templates/cybele_Gemfile +++ b/templates/cybele_Gemfile @@ -14,4 +14,6 @@ gem 'responders' group :doc do gem 'sdoc', require: false -end \ No newline at end of file +end + +gem 'letter_opener', :group => :development \ No newline at end of file