the_merger 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ spec/internal/db/combustion_test.sqlite
data/Gemfile CHANGED
@@ -1,4 +1,15 @@
1
1
  source 'https://rubygems.org'
2
- gem 'rake'
2
+
3
+ group :test do
4
+ gem 'activerecord'
5
+ gem 'actionmailer'
6
+ gem 'actionpack' # action_controller, action_view
7
+ gem 'sqlite3'
8
+ gem 'debugger'
9
+ gem 'sprockets'
10
+ gem 'rspec-rails'
11
+ gem 'combustion', '~> 0.5.0'
12
+ end
13
+
3
14
  # Specify your gem's dependencies in the_merger.gemspec
4
15
  gemspec
@@ -1,4 +1,4 @@
1
- # TheMerger
1
+ * TheMerger
2
2
 
3
3
  A mail merge gem which allows you to do the following;
4
4
  - link to a table eg: clients.
@@ -8,10 +8,10 @@ A mail merge gem which allows you to do the following;
8
8
  - manages bulk emails
9
9
  - manages scheduled emails (optional)
10
10
 
11
- ## Demo app
11
+ ** Demo app
12
12
  There is a demo app here: https://github.com/map7/mailout
13
13
 
14
- ## Installation
14
+ ** Installation
15
15
 
16
16
  Add this line to your application's Gemfile:
17
17
 
@@ -25,7 +25,7 @@ Or install it yourself as:
25
25
 
26
26
  $ gem install the_merger
27
27
 
28
- ## Configure
28
+ ** Configure
29
29
 
30
30
  When using TheMerger in a controller you have to add the following line to the top
31
31
 
@@ -47,15 +47,17 @@ Where you need to insert fields (I usually create a 'letters' table) put the fol
47
47
  <%= field_selection %>
48
48
 
49
49
 
50
- ## Usage
50
+ ** Usage
51
51
 
52
52
  Currently it's just one method and this is how you pass it information
53
53
 
54
- body = "Dear [firstname] [lastname], Please update your listing, from Mick"
55
- TheMerger.merge_fields(body, "firstname", "lastname")
54
+ TheMerger.mail_merge(
55
+ from: "from@example.com",
56
+ subject: "update_listing",
57
+ body: "Dear [firstname] [lastname], Please update your listing, from Mick")
56
58
 
57
59
 
58
- ## Contributing
60
+ ** Contributing
59
61
 
60
62
  1. Fork it
61
63
  2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -63,12 +65,14 @@ Currently it's just one method and this is how you pass it information
63
65
  4. Push to the branch (`git push origin my-new-feature`)
64
66
  5. Create new Pull Request
65
67
 
66
- ## TODO
67
-
68
- - DONE - Get all users from the users table
69
- - DONE - Mail merge and output to the screen
70
- - DONE - Make the table a variable
71
- - DONE - Create a method to get all fields from the selected table.
72
- - DONE - Create a helper method to have a dropdown of fields
73
- - DONE - Turn this into an engine so that I can include asset pipeline javascript.
74
- - DONE - Get the Email all button to work
68
+ ** Tasks
69
+ *** DONE Get all users from the users table
70
+ *** DONE Mail merge and output to the screen
71
+ *** DONE Make the table a variable
72
+ *** DONE Create a method to get all fields from the selected table.
73
+ *** DONE Create a helper method to have a dropdown of fields
74
+ *** DONE Turn this into an engine so that I can include asset pipeline javascript.
75
+ *** DONE Get the Email all button to work
76
+
77
+ *** TODO Write tests
78
+ *** TODO Schedule repetitive mail outs
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize!
7
+ run Combustion::Application
@@ -1,3 +1,3 @@
1
1
  module TheMerger
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/the_merger.rb CHANGED
@@ -7,45 +7,57 @@ module TheMerger
7
7
  # - merge_model: User
8
8
  #
9
9
 
10
- def merge_fields(subject,original_body)
11
- parse_config
12
-
13
- @merge_model.constantize.all.each do |user|
14
- body = original_body.dup
10
+ #
11
+ # Merge the fields into the body and send emails
12
+ #
13
+ def mail_merge(options={})
14
+ # For all users
15
+ model.all.each do |user|
16
+
17
+ # Merge fields for this user into the body
18
+ body = merge_fields(options[:body].dup, user)
15
19
 
16
- model_fields.each do |field|
17
- newbody = body.gsub!("[#{field}]", user.send(field))
18
- body = newbody unless newbody.nil?
19
- end
20
-
21
20
  # Send the emails
22
- mailer = TheMerger::Mailer.batch_mail("from@example.com", subject, body, user)
23
- mailer.deliver
24
-
21
+ TheMerger::Mailer.batch_mail(options[:from], options[:subject], body, user).deliver
25
22
  end
26
23
  end
24
+
25
+ #
26
+ # Replace fields which are in square brackets with data from the chosen model.
27
+ #
28
+ def merge_fields(body,user)
29
+ fields.each do |field|
30
+ body = body.gsub!("[#{field}]", user.send(field)) || body
31
+ end
32
+ body
33
+ end
27
34
 
35
+ #
36
+ # View Helper method (Optional)
37
+ #
38
+ # Selection box with fields from your model and an insert button
39
+ #
28
40
  def field_selection
29
- body = select_tag :field, options_for_select(model_fields)
41
+ body = select_tag :field, options_for_select(fields)
30
42
  body += button_tag "Insert", class: "insert_field"
31
43
  content_tag(:div, body, class: "merge_field")
32
44
  end
33
45
 
34
- def model_fields
35
- parse_config
36
- @merge_model.constantize.attribute_names.reject{|x| %w[created_at updated_at id].include?(x)}
46
+ #
47
+ # Return all the fields from your configured model except created_at, updated_at & id
48
+ #
49
+ def fields
50
+ model.attribute_names.reject{|x| %w[created_at updated_at id].include?(x)}
37
51
  end
38
52
 
39
53
  private
40
54
 
41
- def parse_config
42
- path = "#{Rails.root}/config/the_merger.yml"
43
-
44
- if File.exists?(path)
55
+ def model
56
+ if File.exists?(path = "#{Rails.root}/config/the_merger.yml")
45
57
  conf=YAML::load(IO.read(path))
46
- @merge_model = conf["merge_model"]
58
+ conf["merge_model"].constantize
47
59
  else
48
- @merge_model = "User"
60
+ User
49
61
  end
50
62
  end
51
63
  end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ #
3
+ end
@@ -0,0 +1,16 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table "letters", :force => true do |t|
3
+ t.string "subject"
4
+ t.string "body"
5
+ t.datetime "created_at", :null => false
6
+ t.datetime "updated_at", :null => false
7
+ end
8
+
9
+ create_table "users", :force => true do |t|
10
+ t.string "firstname"
11
+ t.string "lastname"
12
+ t.string "email"
13
+ t.datetime "created_at", :null => false
14
+ t.datetime "updated_at", :null => false
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ include TheMerger
3
+
4
+ class User < ActiveRecord::Base
5
+ attr_accessible :email, :firstname, :lastname
6
+ end
7
+
8
+ describe TheMerger do
9
+
10
+ before do
11
+ @user = User.create(firstname: "Michael", lastname: "Pope", email: "map7777@gmail.com")
12
+ end
13
+
14
+ describe "#merge_fields" do
15
+ context "body is set to Dear [firstname] [lastname]" do
16
+ it "replaces [firstname] & [lastname] with Michael & Pope respectively" do
17
+ merge_fields("Dear [firstname] [lastname]",@user).should eq("Dear Michael Pope")
18
+ end
19
+ end
20
+
21
+ context "body is set to Dear [firstname] [lastname], [address]" do
22
+ it "replaces fields which exist in the model" do
23
+ merge_fields("Dear [firstname] [lastname], [address]",@user).should eq("Dear Michael Pope, [address]")
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "#fields" do
29
+ it "returns fields for the model excluding created_at, updated_at & id" do
30
+ fields.should eq(%w[firstname lastname email])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'combustion'
5
+
6
+ Combustion.initialize! :action_view, :action_mailer, :active_record, :sprockets
7
+
8
+ require 'rspec/rails'
9
+
10
+ # RSpec.configure do |config|
11
+ # config.use_transactional_fixtures = true
12
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_merger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-25 00:00:00.000000000 Z
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Mail merge a table of fields into a standard letter
15
15
  email:
@@ -21,16 +21,23 @@ files:
21
21
  - .gitignore
22
22
  - Gemfile
23
23
  - LICENSE.txt
24
- - README.md
24
+ - README.org
25
25
  - Rakefile
26
26
  - app/assets/javascript/jquery.caret.1.02.js
27
27
  - app/assets/javascript/the_merger/application.js.coffee
28
28
  - app/assets/javascript/the_merger/insert_field.js.coffee
29
29
  - app/mailer/the_merger/mailer.rb
30
+ - config.ru
30
31
  - lib/the_merger.rb
31
32
  - lib/the_merger/engine.rb
32
33
  - lib/the_merger/version.rb
33
- - lib/the_merger/view_helpers/field_selector.rb
34
+ - spec/internal/config/database.yml
35
+ - spec/internal/config/routes.rb
36
+ - spec/internal/db/schema.rb
37
+ - spec/internal/log/.gitignore
38
+ - spec/internal/public/favicon.ico
39
+ - spec/lib/the_merger_spec.rb
40
+ - spec/spec_helper.rb
34
41
  - the_merger.gemspec
35
42
  homepage: https://github.com/map7/the_merger
36
43
  licenses: []
@@ -44,16 +51,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
51
  - - ! '>='
45
52
  - !ruby/object:Gem::Version
46
53
  version: '0'
54
+ segments:
55
+ - 0
56
+ hash: 1235780733984697328
47
57
  required_rubygems_version: !ruby/object:Gem::Requirement
48
58
  none: false
49
59
  requirements:
50
60
  - - ! '>='
51
61
  - !ruby/object:Gem::Version
52
62
  version: '0'
63
+ segments:
64
+ - 0
65
+ hash: 1235780733984697328
53
66
  requirements: []
54
67
  rubyforge_project:
55
68
  rubygems_version: 1.8.23
56
69
  signing_key:
57
70
  specification_version: 3
58
71
  summary: Mail Merge
59
- test_files: []
72
+ test_files:
73
+ - spec/internal/config/database.yml
74
+ - spec/internal/config/routes.rb
75
+ - spec/internal/db/schema.rb
76
+ - spec/internal/log/.gitignore
77
+ - spec/internal/public/favicon.ico
78
+ - spec/lib/the_merger_spec.rb
79
+ - spec/spec_helper.rb
@@ -1,9 +0,0 @@
1
- module TheMerger
2
- module ViewHelpers
3
- class FieldSelector
4
- def select_field
5
- "<b>Select field box</b>"
6
- end
7
- end
8
- end
9
- end