tricle 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +17 -0
- data/gemfiles/rails_3.gemfile +1 -1
- data/gemfiles/rails_4.gemfile +1 -1
- data/lib/tricle/aggregation.rb +7 -11
- data/lib/tricle/email_helper.rb +7 -1
- data/lib/tricle/mailer.rb +4 -4
- data/lib/tricle/metric.rb +9 -2
- data/lib/tricle/presenters/group.rb +2 -2
- data/lib/tricle/presenters/list.rb +3 -3
- data/lib/tricle/presenters/report.rb +4 -4
- data/lib/tricle/templates/email.css +4 -0
- data/lib/tricle/templates/email.html.erb +7 -1
- data/lib/tricle/version.rb +1 -1
- data/screenshot.png +0 -0
- data/spec/app/list_test_mailer_with_options.rb +10 -0
- data/spec/app/test_metric_with_no_total.rb +2 -2
- data/spec/presenters/group_spec.rb +6 -0
- data/spec/unit/mailer_spec.rb +7 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ab31c91268e7d383dbc8090108459f7359d9d94
|
4
|
+
data.tar.gz: 2408d69e0b97e949ff16e572eece95f8ceca80c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef63f1335cb81ca70af935ae1e495b0a2f78c260f7ca7a02a232de3f059dc267b40505ff99d4f10d89fa704420154ae309f9f8ad253570321cc1e0caff5ee649
|
7
|
+
data.tar.gz: f2eadcb789c8caf60fa2a1446740646831898eea9f734856c03f1355844628ef46fefc3a10bd93c5e6669649a48111572571bd3a54bdb5df8e419b3999dc8145
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -181,6 +181,23 @@ end
|
|
181
181
|
|
182
182
|
The subject line will be based on the Mailer class name.
|
183
183
|
|
184
|
+
#### Passing options to a Metric
|
185
|
+
|
186
|
+
Sometimes, you'll want to initialize a Metric with specific options. If you pass a hash as a second argument to the mailer's `metric` method, the Metric will be initialized with an `@options` instance variable.
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
class IntelligenceBrief < Tricle::Mailer
|
190
|
+
|
191
|
+
metric NewUsers, matching_email: '@gmail.com'
|
192
|
+
|
193
|
+
# or for a list...
|
194
|
+
list NewUsers, matching_email: '@gmail.com' do |item|
|
195
|
+
...
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
184
201
|
### Previewing
|
185
202
|
|
186
203
|
Since you'd probably like to preview your mailers before sending them, set up the `Tricle::MailPreview` Rack app (which uses [MailView](https://github.com/37signals/mail_view)).
|
data/gemfiles/rails_3.gemfile
CHANGED
data/gemfiles/rails_4.gemfile
CHANGED
data/lib/tricle/aggregation.rb
CHANGED
@@ -4,15 +4,6 @@ module Tricle
|
|
4
4
|
module Aggregation
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
included do
|
8
|
-
attr_reader :now
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
# TODO allow Time to be passed in so it can be frozen
|
12
|
-
@now = Time.now
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
7
|
def days_ago(n)
|
17
8
|
start_at = self.now.beginning_of_day.ago(n.days)
|
18
9
|
end_at = start_at + 1.day
|
@@ -33,9 +24,14 @@ module Tricle
|
|
33
24
|
self.weeks_ago(1)
|
34
25
|
end
|
35
26
|
|
36
|
-
def
|
27
|
+
def weekly_values(past_num_weeks)
|
37
28
|
weeks_range = 1..past_num_weeks
|
38
|
-
|
29
|
+
weeks_range.map{|n| self.weeks_ago(n) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def weeks_average(past_num_weeks)
|
33
|
+
values = self.weekly_values(past_num_weeks)
|
34
|
+
total = values.reduce(0, :+)
|
39
35
|
total.to_f / past_num_weeks
|
40
36
|
end
|
41
37
|
|
data/lib/tricle/email_helper.rb
CHANGED
@@ -44,7 +44,7 @@ module Tricle
|
|
44
44
|
when :higher
|
45
45
|
(new_val >= old_val) ? 'good' : 'bad'
|
46
46
|
when :lower
|
47
|
-
(new_val
|
47
|
+
(new_val > old_val) ? 'bad' : 'good'
|
48
48
|
else
|
49
49
|
''
|
50
50
|
end
|
@@ -87,5 +87,11 @@ module Tricle
|
|
87
87
|
end_at = start_at + 7.days
|
88
88
|
list.items_markup(start_at, end_at).html_safe
|
89
89
|
end
|
90
|
+
|
91
|
+
def sparkline(metric)
|
92
|
+
values = metric.weekly_values(13)
|
93
|
+
attachment_url = "https://sparklines.herokuapp.com/api/v1.png?values=#{values.join(',')}"
|
94
|
+
image_tag(attachment_url, alt: 'sparkline').html_safe
|
95
|
+
end
|
90
96
|
end
|
91
97
|
end
|
data/lib/tricle/mailer.rb
CHANGED
@@ -53,12 +53,12 @@ module Tricle
|
|
53
53
|
yield if block_given?
|
54
54
|
end
|
55
55
|
|
56
|
-
def metric(klass)
|
57
|
-
self.report.add_metric(klass)
|
56
|
+
def metric(klass, opts = {})
|
57
|
+
self.report.add_metric(klass, opts)
|
58
58
|
end
|
59
59
|
|
60
|
-
def list(klass, &block)
|
61
|
-
self.report.add_list(klass, &block)
|
60
|
+
def list(klass, opts = {}, &block)
|
61
|
+
self.report.add_list(klass, opts, &block)
|
62
62
|
end
|
63
63
|
|
64
64
|
def send_all
|
data/lib/tricle/metric.rb
CHANGED
@@ -7,12 +7,19 @@ module Tricle
|
|
7
7
|
class Metric
|
8
8
|
include Aggregation
|
9
9
|
|
10
|
+
attr_reader :now, :options
|
11
|
+
|
12
|
+
def initialize(opts = {})
|
13
|
+
@now = opts[:now] || Time.now
|
14
|
+
@options = opts
|
15
|
+
end
|
16
|
+
|
10
17
|
def better
|
11
|
-
:higher
|
18
|
+
options[:better] || :higher
|
12
19
|
end
|
13
20
|
|
14
21
|
def title
|
15
|
-
self.class.name.titleize
|
22
|
+
options[:title] || self.class.name.titleize
|
16
23
|
end
|
17
24
|
|
18
25
|
def size_for_range(start_at, end_at)
|
@@ -4,13 +4,13 @@ module Tricle
|
|
4
4
|
class List < Section
|
5
5
|
attr_reader :block, :metric
|
6
6
|
|
7
|
-
def initialize(klass, &block)
|
8
|
-
@metric = klass.new
|
7
|
+
def initialize(klass, opts = {}, &block)
|
8
|
+
@metric = klass.new(opts)
|
9
9
|
@block = block
|
10
10
|
end
|
11
11
|
|
12
12
|
def title
|
13
|
-
self.metric.title
|
13
|
+
self.metric.options[:title] || self.metric.title
|
14
14
|
end
|
15
15
|
|
16
16
|
def items_markup(start_at, end_at)
|
@@ -21,18 +21,18 @@ module Tricle
|
|
21
21
|
self.add_section(group)
|
22
22
|
end
|
23
23
|
|
24
|
-
def add_metric(klass)
|
24
|
+
def add_metric(klass, opts = {})
|
25
25
|
last_section = self.sections.last
|
26
26
|
unless last_section.is_a?(Tricle::Presenters::Group)
|
27
27
|
last_section = self.add_group
|
28
28
|
end
|
29
29
|
|
30
30
|
# TODO don't assume they want to add this metric to the last group?
|
31
|
-
last_section.add_metric(klass)
|
31
|
+
last_section.add_metric(klass, opts)
|
32
32
|
end
|
33
33
|
|
34
|
-
def add_list(klass, &block)
|
35
|
-
list = Tricle::List.new(klass, &block)
|
34
|
+
def add_list(klass, opts = {}, &block)
|
35
|
+
list = Tricle::List.new(klass, opts, &block)
|
36
36
|
self.add_section(list)
|
37
37
|
end
|
38
38
|
end
|
@@ -29,7 +29,7 @@
|
|
29
29
|
</tr>
|
30
30
|
<% section.metrics.each do |metric| %>
|
31
31
|
<tr>
|
32
|
-
<th class="metric-title"><%= metric.title %></th>
|
32
|
+
<th class="metric-title" rowspan="2"><%= metric.title %></th>
|
33
33
|
<td>
|
34
34
|
<div><%= format_number(metric.last_week) %></div>
|
35
35
|
<% if metric.total? %>
|
@@ -39,6 +39,12 @@
|
|
39
39
|
<%= percent_change_cell(metric.last_week, metric.weeks_ago(2), metric.better) %>
|
40
40
|
<%= percent_change_cell(metric.last_week, metric.week_average_this_quarter, metric.better) %>
|
41
41
|
</tr>
|
42
|
+
<tr>
|
43
|
+
<td colspan="3">
|
44
|
+
<%= sparkline(metric) %>
|
45
|
+
</td>
|
46
|
+
</tr>
|
47
|
+
<tr class="separator"></tr>
|
42
48
|
<% end %>
|
43
49
|
<% end %>
|
44
50
|
<% end %>
|
data/lib/tricle/version.rb
CHANGED
data/screenshot.png
CHANGED
Binary file
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require_relative '../../lib/tricle/mailer'
|
2
|
+
require_relative 'test_metric'
|
3
|
+
|
4
|
+
class ListTestMailerWithOptions < Tricle::Mailer
|
5
|
+
default(to: 'recipient1@test.com', from: 'sender@test.com')
|
6
|
+
|
7
|
+
list TestMetric, title: 'TheNewTitle' do |val|
|
8
|
+
sprintf('%.1f', val)
|
9
|
+
end
|
10
|
+
end
|
@@ -12,5 +12,11 @@ describe Tricle::Presenters::Group do
|
|
12
12
|
metric = group.metrics.first
|
13
13
|
expect(metric).to be_a(TestMetric)
|
14
14
|
end
|
15
|
+
|
16
|
+
it "should pass options to the metric" do
|
17
|
+
group.add_metric(TestMetric, foo: 'bar')
|
18
|
+
metric = group.metrics.first
|
19
|
+
expect(metric.instance_variable_get(:@options)[:foo]).to eq 'bar'
|
20
|
+
end
|
15
21
|
end
|
16
22
|
end
|
data/spec/unit/mailer_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|
2
2
|
require_relative '../../lib/tricle/mailer'
|
3
3
|
require_relative '../app/group_test_mailer'
|
4
4
|
require_relative '../app/list_test_mailer'
|
5
|
+
require_relative '../app/list_test_mailer_with_options'
|
5
6
|
require_relative '../app/no_total_test_mailer'
|
6
7
|
require_relative '../app/test_mailer'
|
7
8
|
|
@@ -62,12 +63,17 @@ describe Tricle::Mailer do
|
|
62
63
|
expect(markup).to include('62.0')
|
63
64
|
expect(markup).not_to include('79.0')
|
64
65
|
end
|
66
|
+
|
67
|
+
it "should pass options" do
|
68
|
+
deliver(ListTestMailerWithOptions)
|
69
|
+
expect(markup).to include("TheNewTitle")
|
70
|
+
end
|
65
71
|
end
|
66
72
|
|
67
73
|
describe '.send_all' do
|
68
74
|
it "should .deliver all defined mailers" do
|
69
75
|
Tricle::Mailer.send_all
|
70
|
-
expect(ActionMailer::Base.deliveries.length).to eq(
|
76
|
+
expect(ActionMailer::Base.deliveries.length).to eq(5)
|
71
77
|
end
|
72
78
|
end
|
73
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tricle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aidan Feldman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionmailer
|
@@ -133,6 +133,7 @@ files:
|
|
133
133
|
- screenshot.png
|
134
134
|
- spec/app/group_test_mailer.rb
|
135
135
|
- spec/app/list_test_mailer.rb
|
136
|
+
- spec/app/list_test_mailer_with_options.rb
|
136
137
|
- spec/app/no_total_test_mailer.rb
|
137
138
|
- spec/app/test_mailer.rb
|
138
139
|
- spec/app/test_metric.rb
|
@@ -183,6 +184,7 @@ summary: A datastore-agnostic mailer where you can define custom metrics, where
|
|
183
184
|
test_files:
|
184
185
|
- spec/app/group_test_mailer.rb
|
185
186
|
- spec/app/list_test_mailer.rb
|
187
|
+
- spec/app/list_test_mailer_with_options.rb
|
186
188
|
- spec/app/no_total_test_mailer.rb
|
187
189
|
- spec/app/test_mailer.rb
|
188
190
|
- spec/app/test_metric.rb
|