Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 66a03888b39372be7d255cc5147c40167bc54998 @arkes committed Nov 9, 2012
Showing with 1,413 additions and 0 deletions.
  1. +11 −0 .gitignore
  2. +2 −0 .rspec
  3. +6 −0 Gemfile
  4. +20 −0 MIT-LICENSE
  5. +109 −0 README.rdoc
  6. +38 −0 Rakefile
  7. +24 −0 lazy_mail.gemspec
  8. +102 −0 lib/lazy_mail.rb
  9. +21 −0 lib/lazy_mail/git.rb
  10. +3 −0 lib/lazy_mail/version.rb
  11. +11 −0 spec/db/models.rb
  12. +18 −0 spec/db/schema.rb
  13. +4 −0 spec/db/seeds.rb
  14. +261 −0 spec/dummy/README.rdoc
  15. +7 −0 spec/dummy/Rakefile
  16. +15 −0 spec/dummy/app/assets/javascripts/application.js
  17. +13 −0 spec/dummy/app/assets/stylesheets/application.css
  18. +3 −0 spec/dummy/app/controllers/application_controller.rb
  19. +2 −0 spec/dummy/app/helpers/application_helper.rb
  20. 0 spec/dummy/app/mailers/.gitkeep
  21. 0 spec/dummy/app/models/.gitkeep
  22. +14 −0 spec/dummy/app/views/layouts/application.html.erb
  23. +4 −0 spec/dummy/config.ru
  24. +59 −0 spec/dummy/config/application.rb
  25. +10 −0 spec/dummy/config/boot.rb
  26. +25 −0 spec/dummy/config/database.yml
  27. +5 −0 spec/dummy/config/environment.rb
  28. +37 −0 spec/dummy/config/environments/development.rb
  29. +67 −0 spec/dummy/config/environments/production.rb
  30. +37 −0 spec/dummy/config/environments/test.rb
  31. +7 −0 spec/dummy/config/initializers/backtrace_silencers.rb
  32. +15 −0 spec/dummy/config/initializers/inflections.rb
  33. +5 −0 spec/dummy/config/initializers/mime_types.rb
  34. +7 −0 spec/dummy/config/initializers/secret_token.rb
  35. +8 −0 spec/dummy/config/initializers/session_store.rb
  36. +14 −0 spec/dummy/config/initializers/wrap_parameters.rb
  37. +5 −0 spec/dummy/config/locales/en.yml
  38. +58 −0 spec/dummy/config/routes.rb
  39. 0 spec/dummy/db/test.sqlite3
  40. 0 spec/dummy/lib/assets/.gitkeep
  41. 0 spec/dummy/log/.gitkeep
  42. +26 −0 spec/dummy/public/404.html
  43. +26 −0 spec/dummy/public/422.html
  44. +25 −0 spec/dummy/public/500.html
  45. 0 spec/dummy/public/favicon.ico
  46. +6 −0 spec/dummy/script/rails
  47. +1 −0 spec/dummy/spec
  48. +229 −0 spec/lazy_mail/lazy_mail_spec.rb
  49. +19 −0 spec/locales/lang_spec.yml
  50. +1 −0 spec/notifications/my_mail_test/en/test.text.erb
  51. +1 −0 spec/notifications/my_mail_test/en/test_client.text.erb
  52. +1 −0 spec/notifications/my_mail_test/en/test_obj.text.erb
  53. +1 −0 spec/notifications/my_mail_test/en/test_res.text.erb
  54. +1 −0 spec/notifications/my_mail_test/fr/test.text.erb
  55. +29 −0 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
+test/dummy/.sass-cache
+TODO
+*.gem
+gems
+*.lock
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--backtrace
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in lazy_mail.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gemspec
@@ -0,0 +1,20 @@
+Copyright 2012 thomas floch
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,109 @@
+= Lazy Mail
+
+lazy_mail is a lazy and quick way to use the function mail and offers configurations to write less code.
+
+== Installation
+
+<b>This Gem works only for Rails 3</b>
+
+Add this to your Gemfile and run the +bundle+ command.
+
+ gem 'lazy_mail'
+
+== Usage
+
+=== 1. Basics
+
+Your mailer before was:
+
+ class UserMailer < ActionMailer::Base
+ default :from => 'test@test.com'
+
+ def confirmation(user)
+ @user = user
+ mail(:to => user.email, :subject => 'confirm')
+ end
+ end
+
+With lazy_mail you just write less code.
+Lazy_mail set +user+ as an instance variable +@user+ by default.
+
+ class UserMailer < ActionMailer::Base
+ def confirmation(user)
+ lazy_mail user
+ end
+ end
+
+And you email view doesn't change:
+
+ Hello <%= @user.username %>
+
+You can still use the options of mail:
+
+ lazy_mail user, { :to => 'another@test.com', :subject => 'my subject' }
+
+
+You can pass many arguments as you like:
+
+ def confirmation(user, post, author)
+ lazy_mail user, post, author
+ end
+
+and just use in your mail view +@user+, +@post+, +@autor+.
+
+
+=== 2. Configurations
+
+=== Mailer Views
+
+The Views are now located in a different folder: +app/views/notifications/mailer_name/current_locale/+
+For example your locale is english the path will be:
+
+ views/notifications/user_mailer/en/confirmation.text.erb
+
+You can set the path you want in your initializer with:
+
+ LazyMail.mailer_templates_path = 'notification'
+
+If you set it to +nil+ it will take the default rails path
+
+=== Option :to
+
+The mail option :to is set by default, but you have to configure it in your initializer:
+
+ Lazy.user_model = User
+
+Just put the name of the model you want to use to set the :to.
+
+By default it will call the method +email+, if you have another name change it in your initializer.
+
+ LazyMail.email_field = :email
+
+=== I18n subject
+
+The subject is a scope: +[:mailer, :mailer_name, :action_name]+, you can change it:
+
+ LazyMail.i18n_scope = [:mailer, :class_name, :action_name]
+ => 'mailer.user_mailer.confirmation.subject'
+
+If you prefer to use the rails default just set it to +nil+
+
+=== Option :from
+
+Set the option :from in the initializer
+
+ LazyMail.default_no_reply = 'no-reply@test.com'
+
+=== Development
+
+For the development phase, you can set an option :to by default to overwrite user.email
+
+ LazyMail.development_mail = 'my_mail@test.com'
+
+If you use Git:
+
+ LazyMail.development_mail = :git
+
+lazy_mail will take your git +git config user.email+
+
+You can find the initializer file {here}[https://github.com/arkes/lazy_mail/wiki/initializer]
@@ -0,0 +1,38 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'LazyMail'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
@@ -0,0 +1,24 @@
+$:.push File.expand_path("../lib", __FILE__)
+
+require "lazy_mail/version"
+
+Gem::Specification.new do |s|
+ s.name = "lazy_mail"
+ s.version = LazyMail::VERSION
+ s.authors = ["thomas floch"]
+ s.email = ["thomas.floch@gmail.com"]
+ s.homepage = "http://github.com/arkes/lazy_mail"
+ s.summary = "A lazy and quick way to use the function mail"
+ s.description = "lazy_mail is a lazy and quick way to use the function mail and offers configurations to write less code"
+
+ s.files = Dir["{lib,spec}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
+ s.test_files = Dir["test/**/*"]
+
+ s.add_dependency "rails", ">= 3.0.0"
+
+ s.add_development_dependency "rails", "~> 3.2"
+ s.add_development_dependency 'rspec-rails', '~> 2.11.4'
+ s.add_development_dependency 'sqlite3-ruby', '~> 1.3.3'
+
+ s.required_rubygems_version = ">= 1.3.4"
+end
@@ -0,0 +1,102 @@
+require 'lazy_mail/git'
+
+module LazyMail
+
+ class << self
+ class_attribute :mailer_templates_path, :default_no_reply, :user_model,
+ :email_field, :development_mail, :i18n_scope
+
+ self.mailer_templates_path = 'notifications'
+ self.default_no_reply = 'no-reply@set-the-no-reply.com'
+ self.user_model = nil
+ self.email_field = :email
+ self.development_mail = :git
+ self.i18n_scope = [:mailer, :class_name, :action_name]
+ end
+
+ def self.included(base)
+ base.class_eval do
+ include LazyMail::Git
+ include LazyMail::InstanceMethods
+ end
+ end
+
+ module InstanceMethods
+
+ def lazy_mail(*args)
+ setup_mail(args)
+ mail headers_for
+ end
+
+ private
+
+ def setup_mail(args)
+ @options = args.extract_options!
+ set_resources(args)
+ @class_name = self.class.name.underscore
+ @action_name = self.action_name
+ end
+
+ def set_resources(args)
+ if LazyMail.user_model.nil?
+ raise ArgumentError, 'lazy_mail: you need to define a user_model or use option :to'
+ end
+ args.each do |arg|
+ variable_name = arg.class.name.underscore
+ variable_name = set_valid_name("#{variable_name}", 2) if instance_variable_defined?("@#{variable_name}")
+ instance_variable_set("@#{variable_name}", arg)
+ end
+ if !instance_variable_defined?("@#{user_model.to_s.downcase}") and !@options.has_key?(:to)
+ raise ArgumentError, "lazy_mail should have an instance of #{user_model.to_s} or an option :to"
+ end
+ end
+
+ def set_valid_name(variable_name, count)
+ return set_valid_name("#{variable_name}", count + 1) if instance_variable_defined?("@#{variable_name}_#{count}")
+ return "#{variable_name}_#{count}"
+ end
+
+ def headers_for
+ headers = {
+ :subject => @options.has_key?(:subject) ? @options[:subject] : translate_subject,
+ :from => @options.has_key?(:from) ? @options[:from] : LazyMail.default_no_reply,
+ :to => @options.has_key?(:to) ? @options[:to] : get_email,
+ :template_path => @options.has_key?(:template_path) ? @options[:template_path] : template_path
+ }
+ headers
+ end
+
+ def template_path
+ unless LazyMail.mailer_templates_path.nil?
+ File.join(LazyMail.mailer_templates_path, @class_name, I18n.locale.to_s)
+ end
+ end
+
+ def translate_subject
+ return nil if LazyMail.i18n_scope.nil?
+ i18n_scope = LazyMail.i18n_scope.map { |key| instance_variable_defined?("@#{key}") ? instance_variable_get("@#{key}") : key }
+ I18n.t(:subject, :scope => i18n_scope)
+ end
+
+ def user_model
+ LazyMail.user_model
+ end
+
+ def get_email
+ if Rails.env == 'development'
+ return development_mail if development_mail.is_a?(String)
+ return send("#{development_mail}_email") if development_mail.is_a?(Symbol)
+ raise ArgumentError, 'option development_mail should be a String or a Symbol'
+ else
+ instance_variable_get("@#{user_model.to_s.downcase}").send(LazyMail.email_field)
+ end
+ end
+
+ def development_mail
+ LazyMail.development_mail
+ end
+
+ end
+end
+
+ActionMailer::Base.send :include, LazyMail
@@ -0,0 +1,21 @@
+module LazyMail
+ module Git
+
+ def git_email
+ email = user_email
+ return email.chomp unless email.blank?
+ message_no_email
+ end
+
+ private
+
+ def user_email
+ `git config user.email`
+ end
+
+ def message_no_email
+ warn "* You have not configured your git with user.email"
+ end
+
+ end
+end
@@ -0,0 +1,3 @@
+module LazyMail
+ VERSION = "0.1.0"
+end
@@ -0,0 +1,11 @@
+class User < ActiveRecord::Base
+ attr_accessible :email, :username
+end
+
+class Client < ActiveRecord::Base
+ attr_accessible :email, :username
+end
+
+class Other < ActiveRecord::Base
+ attr_accessible :test_mail, :username
+end
@@ -0,0 +1,18 @@
+ActiveRecord::Schema.define(:version => 0) do
+
+ create_table :users, :force => true do |t|
+ t.string :username
+ t.string :email
+ end
+
+ create_table :clients, :force => true do |t|
+ t.string :username
+ t.string :email
+ end
+
+ create_table :others, :force => true do |t|
+ t.string :username
+ t.string :test_mail
+ end
+
+end
@@ -0,0 +1,4 @@
+User.create!(:email => 'tom@test.com', :username => 'tom')
+User.create!(:email => 'tomtom@test.com', :username => 'tomtom')
+Client.create!(:email => 'bob@test.com', :username => 'bob')
+Other.create!(:test_mail => 'plop@test.com', :username => 'plop')
Oops, something went wrong.

0 comments on commit 66a0388

Please sign in to comment.