digestifier 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6571f35b67e51f1547d094219716e4545cf9a635
4
+ data.tar.gz: 0768e76da9516ffca6a432db97a5ff484df60444
5
+ SHA512:
6
+ metadata.gz: 6c54ef82d778a0e4dfda4b7608f1ac382c1dfaa8b00c9c47382433a6fd185e272a989f58a49fd368f8c32ae9d9904f56a73a7a9d86d5fb79f56e4468516404cf
7
+ data.tar.gz: c474429545bc4fc5600df7c69ebb346fb4443da12cf52cdfd4b2c91aaeff4650aa2706b07ea6076a8a617255d4009886efc29065c6a25879a5cf6e379a0495d0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/internal/db/*.sqlite
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ notifications:
6
+ campfire:
7
+ rooms:
8
+ - secure: "OmCCqj/S4Yj/KzSEN7BoL86l16LhydqcJqOGMB44A/7H9ExcMfv6prD1X7or/NKaqM3IWNZX7ElvCuc8u3R8QD377ry1L+d3vqgfXSbIjKhW/g4YgktRIzBEEH37Bs/3pfcU9qtZoeEeKAM3T2pduK7OVUV64jflHQyXK0NBgig="
9
+ on_success: change
10
+ on_failure: change
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Inspire9
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Digestifier
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'digestifier'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install digestifier
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
@@ -0,0 +1,19 @@
1
+ module Digestifier::PartialHelper
2
+ def render_digest_partial(item)
3
+ render partial: digest_partial_path(item), object: item,
4
+ locals: {digest_item: item}
5
+ end
6
+
7
+ private
8
+
9
+ def digest_partial_path(item)
10
+ if lookup_context.exists?(
11
+ item.class.name.underscore, [:digestifier, 'mailer'].compact.join('/'),
12
+ true
13
+ )
14
+ [:digestifier, 'mailer', item.class.name.underscore].compact.join('/')
15
+ else
16
+ [:digestifier, 'mailer', 'digest_item'].compact.join('/')
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ class Digestifier::Mailer < ActionMailer::Base
2
+ helper 'digestifier/partial'
3
+
4
+ def digest(recipient, content_items)
5
+ @recipient = recipient
6
+ @content_items = content_items
7
+
8
+ mail(
9
+ from: Digestifier.sender,
10
+ to: recipient.email
11
+ )
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ class Digestifier::Receipt < ActiveRecord::Base
2
+ self.table_name = 'digestifier_receipts'
3
+
4
+ belongs_to :recipient, polymorphic: true
5
+
6
+ validates :recipient, presence: true
7
+ validates :captured_at, presence: true
8
+
9
+ def self.capture(recipient)
10
+ receipt = last_for recipient
11
+
12
+ if receipt.nil?
13
+ create recipient: recipient, captured_at: Time.zone.now
14
+ else
15
+ receipt.update_attributes captured_at: Time.zone.now
16
+ end
17
+ end
18
+
19
+ def self.last_for(recipient)
20
+ where(
21
+ recipient_type: recipient.class.name,
22
+ recipient_id: recipient.id
23
+ ).order('captured_at DESC').first
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ class Digestifier::Setting < ActiveRecord::Base
2
+ self.table_name = 'digestifier_settings'
3
+
4
+ belongs_to :recipient, polymorphic: true
5
+
6
+ serialize :preferences, JSON
7
+
8
+ validates :recipient, presence: true
9
+ validates :preferences, presence: true
10
+
11
+ def self.for(recipient)
12
+ where(
13
+ recipient_type: recipient.class.name,
14
+ recipient_id: recipient.id
15
+ ).first || create(recipient: recipient)
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ <%= digest_item.name %>
@@ -0,0 +1,9 @@
1
+ <p>Hi <%= @recipient.email %></p>
2
+
3
+ <p>Here's the latest news:</p>
4
+
5
+ <ul>
6
+ <% @content_items.each do |item| %>
7
+ <li><%= render_digest_partial item %></li>
8
+ <% end %>
9
+ </ul>
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize! :action_controller, :active_record, :action_mailer
7
+ run Combustion::Application
@@ -0,0 +1,12 @@
1
+ class CreateReceipts < ActiveRecord::Migration
2
+ def change
3
+ create_table :digestifier_receipts do |t|
4
+ t.string :recipient_type, null: false
5
+ t.integer :recipient_id, null: false
6
+ t.datetime :captured_at, null: false
7
+ end
8
+
9
+ add_index :digestifier_receipts, [:recipient_type, :recipient_id],
10
+ unique: true
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class CreateSettings < ActiveRecord::Migration
2
+ def change
3
+ create_table :digestifier_settings do |t|
4
+ t.string :recipient_type, null: false
5
+ t.integer :recipient_id, null: false
6
+ t.text :preferences, null: false, default: '{}'
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :digestifier_settings, [:recipient_type, :recipient_id],
11
+ unique: true
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'digestifier'
4
+ spec.version = '0.0.1'
5
+ spec.authors = ['Pat Allan']
6
+ spec.email = ['pat@freelancing-gods.com']
7
+ spec.summary = 'Digests as a Rails Engine'
8
+ spec.description = 'A Rails engine that lets you define and customise digest emails'
9
+ spec.homepage = 'https://github.com/inspire9/digestifier'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_runtime_dependency 'rails', '>= 3.2'
18
+
19
+ spec.add_development_dependency 'combustion', '~> 0.5.1'
20
+ spec.add_development_dependency 'rspec-rails', '~> 2.14.0'
21
+ spec.add_development_dependency 'sqlite3', '~> 1.3.8'
22
+ end
@@ -0,0 +1,40 @@
1
+ class Digestifier::Delivery
2
+ def self.deliver(digest)
3
+ digest.recipients.call.find_each do |recipient|
4
+ new(digest, recipient).deliver
5
+ end
6
+ end
7
+
8
+ def initialize(digest, recipient)
9
+ @digest, @recipient = digest, recipient
10
+ end
11
+
12
+ def deliver
13
+ Digestifier.mailer.digest(recipient, contents).deliver
14
+ Digestifier::Receipt.capture recipient
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :digest, :recipient
20
+
21
+ delegate :default_frequency, to: :digest
22
+
23
+ def contents
24
+ digest.contents.call.where(
25
+ created_at: last_sent..Time.zone.now
26
+ )
27
+ end
28
+
29
+ def frequency
30
+ settings = Digestifier::Setting.for recipient
31
+ return default_frequency unless settings.preferences['frequency']
32
+
33
+ settings.preferences['frequency']
34
+ end
35
+
36
+ def last_sent
37
+ receipt = Digestifier::Receipt.last_for(recipient)
38
+ receipt.nil? ? frequency.ago : receipt.captured_at
39
+ end
40
+ end
@@ -0,0 +1,8 @@
1
+ class Digestifier::Digest
2
+ attr_accessor :contents, :default_frequency, :recipients
3
+
4
+ def initialize
5
+ @default_frequency = 24.hours
6
+ @recipients = Proc.new { User.order(:id) }
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ class Digestifier::Engine < Rails::Engine
2
+ engine_name :digestifier
3
+
4
+ config.after_initialize do
5
+ Digestifier.mailer = Digestifier::Mailer
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Digestifier
2
+ mattr_accessor :mailer, :sender
3
+ end
4
+
5
+ require 'digestifier/delivery'
6
+ require 'digestifier/digest'
7
+ require 'digestifier/engine'
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Custom digest partials' do
4
+ let(:digest) { Digestifier::Digest.new }
5
+ let(:user) { User.create! email: 'me@somewhere.com' }
6
+
7
+ before :each do
8
+ ActionMailer::Base.deliveries.clear
9
+
10
+ digest.contents = lambda { Book }
11
+
12
+ user
13
+ end
14
+
15
+ it 'respects object-specific partials' do
16
+ Book.create! title: 'Recent Book'
17
+
18
+ Digestifier::Delivery.deliver digest
19
+
20
+ mail = ActionMailer::Base.deliveries.detect { |mail|
21
+ mail.to.include?('me@somewhere.com')
22
+ }
23
+ expect(mail.body).to match(/Recent Book/)
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Delivering digests' do
4
+ let(:digest) { Digestifier::Digest.new }
5
+ let(:user) { User.create! email: 'me@somewhere.com' }
6
+
7
+ before :each do
8
+ ActionMailer::Base.deliveries.clear
9
+
10
+ digest.contents = lambda { Article }
11
+
12
+ user
13
+ end
14
+
15
+ it 'sends an email of posts within the past day' do
16
+ Article.create! name: 'Recent Post'
17
+ Article.create!(name: 'Old Post').
18
+ update_attribute :created_at, 25.hours.ago
19
+
20
+ Digestifier::Delivery.deliver digest
21
+
22
+ mail = ActionMailer::Base.deliveries.detect { |mail|
23
+ mail.to.include?('me@somewhere.com')
24
+ }
25
+ expect(mail.body).to match(/Recent Post/)
26
+ expect(mail.body).to_not match(/Old Post/)
27
+ end
28
+
29
+ it "allows the digest to have a different frequency" do
30
+ digest.default_frequency = 12.hours
31
+
32
+ Article.create! name: 'Recent Post'
33
+ Article.create!(name: 'Old Post').
34
+ update_attribute :created_at, 13.hours.ago
35
+
36
+ Digestifier::Delivery.deliver digest
37
+
38
+ mail = ActionMailer::Base.deliveries.detect { |mail|
39
+ mail.to.include?('me@somewhere.com')
40
+ }
41
+ expect(mail.body).to match(/Recent Post/)
42
+ expect(mail.body).to_not match(/Old Post/)
43
+ end
44
+
45
+ it "does not include already sent items if sending sooner than expected" do
46
+ Article.create!(name: 'Old Post').
47
+ update_attribute :created_at, 13.hours.ago
48
+ Digestifier::Delivery.deliver digest
49
+ ActionMailer::Base.deliveries.clear
50
+
51
+ Article.create! name: 'Recent Post'
52
+ Digestifier::Delivery.deliver digest
53
+
54
+ mail = ActionMailer::Base.deliveries.detect { |mail|
55
+ mail.to.include?('me@somewhere.com')
56
+ }
57
+ expect(mail.body).to match(/Recent Post/)
58
+ expect(mail.body).to_not match(/Old Post/)
59
+ end
60
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Custom digest frequency' do
4
+ let(:digest) { Digestifier::Digest.new }
5
+ let(:user) { User.create! email: 'me@somewhere.com' }
6
+
7
+ before :each do
8
+ ActionMailer::Base.deliveries.clear
9
+
10
+ digest.contents = lambda { Article }
11
+ end
12
+
13
+ it 'respects receipient frequency preferences' do
14
+ Article.create! name: 'Recent Post'
15
+ Article.create!(name: 'Old Post').
16
+ update_attribute :created_at, 13.hours.ago
17
+
18
+ setting = Digestifier::Setting.for(user)
19
+ setting.preferences['frequency'] = 12.hours.to_i
20
+ setting.save
21
+
22
+ Digestifier::Delivery.deliver digest
23
+
24
+ mail = ActionMailer::Base.deliveries.detect { |mail|
25
+ mail.to.include?('me@somewhere.com')
26
+ }
27
+ expect(mail.body).to match(/Recent Post/)
28
+ expect(mail.body).to_not match(/Old Post/)
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ class Article < ActiveRecord::Base
2
+ #
3
+ end
@@ -0,0 +1,3 @@
1
+ class Book < ActiveRecord::Base
2
+ #
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ #
3
+ end
@@ -0,0 +1 @@
1
+ <%= book.title %>
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1 @@
1
+ Digestifier.sender = 'hello@inspire9.com'
@@ -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 :articles, :force => true do |table|
3
+ table.string :name
4
+ table.timestamps
5
+ end
6
+
7
+ create_table :books, :force => true do |table|
8
+ table.string :title
9
+ table.timestamps
10
+ end
11
+
12
+ create_table :users, :force => true do |table|
13
+ table.string :email
14
+ table.timestamps
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+
3
+ Bundler.setup :default, :development
4
+
5
+ require 'combustion'
6
+ require 'digestifier'
7
+
8
+ Combustion.initialize! :action_controller, :active_record, :action_mailer
9
+
10
+ require 'rspec/rails'
11
+
12
+ Dir["./spec/support/**/*.rb"].each { |file| require file }
13
+
14
+ RSpec.configure do |config|
15
+ config.use_transactional_fixtures = true
16
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digestifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: combustion
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.8
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.8
69
+ description: A Rails engine that lets you define and customise digest emails
70
+ email:
71
+ - pat@freelancing-gods.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - app/helpers/digestifier/partial_helper.rb
83
+ - app/mailers/digestifier/mailer.rb
84
+ - app/models/digestifier/receipt.rb
85
+ - app/models/digestifier/setting.rb
86
+ - app/views/digestifier/mailer/_digest_item.html.erb
87
+ - app/views/digestifier/mailer/digest.html.erb
88
+ - config.ru
89
+ - db/migrate/1_create_receipts.rb
90
+ - db/migrate/2_create_settings.rb
91
+ - digestifier.gemspec
92
+ - lib/digestifier.rb
93
+ - lib/digestifier/delivery.rb
94
+ - lib/digestifier/digest.rb
95
+ - lib/digestifier/engine.rb
96
+ - spec/acceptance/custom_views_spec.rb
97
+ - spec/acceptance/delivering_digests_spec.rb
98
+ - spec/acceptance/recipient_preferences_spec.rb
99
+ - spec/internal/app/models/article.rb
100
+ - spec/internal/app/models/book.rb
101
+ - spec/internal/app/models/user.rb
102
+ - spec/internal/app/views/digestifier/mailer/_book.html.erb
103
+ - spec/internal/config/database.yml
104
+ - spec/internal/config/initializers/digestifier.rb
105
+ - spec/internal/config/routes.rb
106
+ - spec/internal/db/schema.rb
107
+ - spec/internal/log/.gitignore
108
+ - spec/internal/public/favicon.ico
109
+ - spec/spec_helper.rb
110
+ homepage: https://github.com/inspire9/digestifier
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.1.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Digests as a Rails Engine
134
+ test_files:
135
+ - spec/acceptance/custom_views_spec.rb
136
+ - spec/acceptance/delivering_digests_spec.rb
137
+ - spec/acceptance/recipient_preferences_spec.rb
138
+ - spec/internal/app/models/article.rb
139
+ - spec/internal/app/models/book.rb
140
+ - spec/internal/app/models/user.rb
141
+ - spec/internal/app/views/digestifier/mailer/_book.html.erb
142
+ - spec/internal/config/database.yml
143
+ - spec/internal/config/initializers/digestifier.rb
144
+ - spec/internal/config/routes.rb
145
+ - spec/internal/db/schema.rb
146
+ - spec/internal/log/.gitignore
147
+ - spec/internal/public/favicon.ico
148
+ - spec/spec_helper.rb