french_toast 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a63ea01184bb8c09aa820ba590173e4d369cfacd
4
+ data.tar.gz: 1765b295c089915f311b815e2f83f432573af171
5
+ SHA512:
6
+ metadata.gz: ad4a28c4eed8868d82215a46a3884f36261554ff309576e404b9b079ee4e0acd4eded01f3117e16348b2dbcf67635d31e6666934eb2bd01922059c5ddbc1949c
7
+ data.tar.gz: ee4a0e2f58caa3d6bbf318ddf06c6c4f42c713a00d36c0b747cac4a842729d7829a0c8ee0d572508e8dbd889ddd7f3c6efa525ddb0d449e82d3fd08465a43418
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/examples.txt
9
+ /spec/reports/
10
+ /tmp/
11
+ spec/examples.txt
12
+ /spec/dummy/.byebug_history
13
+ db/migrate/*_create_last_notifications.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ dist: trusty
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.7
5
+ - 2.3.4
6
+ - 2.4.1
7
+ services:
8
+ - redis-server
9
+ script: xvfb-run bundle exec rake
@@ -0,0 +1,17 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
+ and this project adheres to [Semantic Versioning](http://semver.org/).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ - This change log file.
13
+ - Contributing guidelines.
14
+ - A readme.
15
+ - A license.
16
+
17
+ [Unreleased]: https://github.com/thoughtbot/french_toast/compare/v0.3.0...HEAD
@@ -0,0 +1,42 @@
1
+ # Contributing
2
+
3
+ We love contributions from everyone.
4
+ By participating in this project,
5
+ you agree to abide by the thoughtbot [code of conduct].
6
+
7
+ [code of conduct]: https://thoughtbot.com/open-source-code-of-conduct
8
+
9
+ We expect everyone to follow the code of conduct
10
+ anywhere in thoughtbot's project codebases,
11
+ issue trackers, chatrooms, and mailing lists.
12
+
13
+ ## Contributing Code
14
+
15
+ 1. Fork the repo.
16
+
17
+ ```
18
+ git clone git@github.com:thoughtbot/french_toast.git
19
+ ```
20
+
21
+ 1. Make sure the tests pass:
22
+
23
+ ```
24
+ rake
25
+ ```
26
+
27
+ 1. Make your change, with new passing tests. Follow the [style guide][style].
28
+
29
+ [style]: https://github.com/thoughtbot/guides/tree/master/style
30
+
31
+ 1. Mention how your changes affect the project to other developers and users in
32
+ the [change log].
33
+
34
+ 1. Push to your fork. Write a [good commit message][commit]. Submit a pull request.
35
+
36
+ [change log]: CONTRIBUTING.md
37
+ [commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
38
+
39
+ Others will give constructive feedback.
40
+ This is a time for discussion and improvements,
41
+ and making the necessary changes will be required before we can
42
+ merge the contribution.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in french_toast.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 thoughtbot, inc.
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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,146 @@
1
+ # French Toast
2
+ [![Build Status](https://travis-ci.org/thoughtbot/french_toast.svg?branch=master)](https://travis-ci.org/thoughtbot/french_toast)
3
+ [![Code Climate](https://codeclimate.com/github/thoughtbot/french_toast/badges/gpa.svg)](https://codeclimate.com/github/thoughtbot/french_toast)
4
+
5
+ Communicate information about background jobs in Rails seamlessly.
6
+
7
+ FrenchToast attempts to streamline some [progressive enhancement](https://en.wikipedia.org/wiki/Progressive_enhancement) by providing a simple and opinionated interface. In order to best take advantage of this, it is important to render that information on the backend. This allows FrenchToast to figure out the best possible communication stream, by starting the most basic functionality and slowly building up to more enhanced features.
8
+
9
+ With `FrenchToast` it will:
10
+
11
+ 1. Communicate through websockets
12
+ 2. If that is unavailable, communicate on next page refresh
13
+ 3. If the user hasn't seen the content, send them an email
14
+
15
+ ## Installation
16
+
17
+ - Add the `FrenchToast` gem to your Gemfile:
18
+
19
+ ```
20
+ gem "french_toast"
21
+ ```
22
+
23
+ - Run `bundle install` from your shell.
24
+
25
+ - Generate migrations so ``FrenchToast`` can render information when javascript isn't available.
26
+
27
+ ```
28
+ rails generate french_toast:install
29
+ rake db:migrate
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### General
35
+
36
+ The `#french_toast` helper provides the simplest interface for `FrenchToast`. It can be used, for example, on the `application` layout.
37
+
38
+ ```erb
39
+ # application.html.erb
40
+ <%= french_toast %>
41
+ ```
42
+
43
+ `FrenchToast` looks for templates by default in the `views/french_toast` folder.
44
+
45
+ ```erb
46
+ # _foo.html.erb
47
+ <div>You'll see me eventually! <%= your_name %></div>
48
+ ```
49
+
50
+ Use `#notify` with the name of the template to be rendered to render that template on the page.
51
+
52
+ ```ruby
53
+ # something_job.rb
54
+ class SomethingJob
55
+ def perform
56
+ `FrenchToast`.notify("foo", payload: { your_name: "Jim-bob" })
57
+ end
58
+ end
59
+ ```
60
+
61
+ Where you specified `#french_toast` you will see the `foo.html.erb` template fully rendered using the best channel available to your user.
62
+
63
+ ### Specifying sections
64
+
65
+ `FrenchToast` gives you the option to be more specific about what kind of content should be rendered.
66
+
67
+ Provide a namespace to `#french_toast` helper
68
+
69
+ ```erb
70
+ # posts/index.html.erb
71
+ <%= french_toast :post %>
72
+ ```
73
+
74
+ Referencing a template the same way, provide `name` attribute to `#notify` will render the template only where it is namespaced.
75
+
76
+ ```ruby
77
+ #... something_job.rb
78
+ def perform
79
+ `FrenchToast`.notify("foo", name: :post, payload: {})
80
+ end
81
+ ```
82
+
83
+ Where `#french_toast` was specified with `:post`, the `foo.html.erb` template will be rendered.
84
+
85
+ ### Synchronous render control
86
+
87
+ `FrenchToast` by default will store information in your database to be displayed later if a user is unable to receive information asynchronously. This can be prevented if there is already a faster, simpler way to communicate information on page load.
88
+
89
+ For example, `FrenchToast` is being used to update a list of posts on a page through websockets, but there is already a code to render each post on the server. You can prevent `FrenchToast` from storing that content on the server.
90
+
91
+ On the `#french_toast` helper you can specify the `synchronous` attribute, which helps specify the type of communication allowed.
92
+
93
+ ```erb
94
+ <%= french_toast :post, synchronous: false %>
95
+ ```
96
+
97
+ ### Granular template control
98
+
99
+ `FrenchToast` by default assumes HTML templates are being rendered on the page. There are times that require more granular control of the template because the information being sent is not HTML.
100
+
101
+ A custom template can be specified and referenced using the `template` attribute on the `#french_toast` helper.
102
+
103
+ ```erb
104
+ <%= french_toast :text, template: "text_only" %>
105
+ ```
106
+
107
+ Create a template in `french_toast/templates` and use the `#french_toast_tag` helper to generate the necessary HTML that can be identified by `FrenchToast`.
108
+
109
+ ```erb
110
+ # _text_only.html.erb
111
+ <div class="my-own-thing" >
112
+ <%= french_toast_tag :p %>
113
+ </div>
114
+
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ See the [CONTRIBUTING] document.
120
+ Thank you, [contributors]!
121
+
122
+ [CONTRIBUTING]: CONTRIBUTING.md
123
+ [contributors]: https://github.com/thoughtbot/french_toast/graphs/contributors
124
+
125
+ ## License
126
+
127
+ French Toast is Copyright (c) 2016 thoughtbot, inc.
128
+ It is free software, and may be redistributed
129
+ under the terms specified in the [LICENSE] file.
130
+
131
+ [LICENSE]: LICENSE.md
132
+
133
+ ## About
134
+
135
+ [<img src="http://presskit.thoughtbot.com/images/signature.svg" width="250" alt="thoughtbot logo">][thoughtbot]
136
+
137
+ French Toast is maintained and funded by thoughtbot, inc.
138
+ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
139
+
140
+ We love open source software!
141
+ See [our other projects][community]
142
+ or [hire us][hire] to help build your product.
143
+
144
+ [thoughtbot]: https://thoughtbot.com?utm_source=github
145
+ [community]: https://thoughtbot.com/community?utm_source=github
146
+ [hire]: https://thoughtbot.com/hire-us?utm_source=github
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.pattern = '*/*_spec.rb'
8
+ end
9
+
10
+ task default: :spec
11
+ rescue LoadError
12
+ end
@@ -0,0 +1,9 @@
1
+ App.cable.subscriptions.create({
2
+ channel: "NotifierChannel"
3
+ }, {
4
+ received: function(content) {
5
+ if (content !== undefined) {
6
+ $("[data-french-toast-content]").html(content);
7
+ }
8
+ }
9
+ });
@@ -0,0 +1,22 @@
1
+ module FrenchToast
2
+ module Helper
3
+ def french_toast
4
+ render "application/french_toast_template"
5
+ end
6
+
7
+ def french_toast_notification
8
+ @french_toast_notification ||=
9
+ FrenchToast::LastNotification.find_or_create_by(
10
+ session: cookies[:key],
11
+ ).tap(&:destroy)
12
+ end
13
+
14
+ def french_toast_content?
15
+ french_toast_notification.data?
16
+ end
17
+
18
+ def french_toast_content
19
+ french_toast_notification.data
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module FrenchToast
2
+ class NotifierMailer < ActionMailer::Base
3
+ def article_processed(email)
4
+ mail(
5
+ from: "us@example.com",
6
+ to: email,
7
+ )
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module FrenchToast
2
+ class LastNotification < ActiveRecord::Base; end
3
+ end
@@ -0,0 +1,11 @@
1
+ <div id="french-toast-js-container" aria-live="polite" role="alert">
2
+ <div class="french-toast-c-content" data-french-toast-content>
3
+ <% if french_toast_content? %>
4
+ <%= french_toast_content.html_safe %>
5
+ <% end %>
6
+ </div>
7
+
8
+ <noscript>
9
+ Refresh page for updates.
10
+ </noscript>
11
+ </div>
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "french_toast"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,14 @@
1
+ #!/bin/sh
2
+
3
+ export RAILS_ENV=test
4
+
5
+ bundle install
6
+
7
+ (
8
+ cd spec/dummy && \
9
+ rm db/migrate/*_create_last_notifications.rb
10
+ bundle install && \
11
+ rake db:environment:set db:drop db:create && \
12
+ rails generate french_toast:install && \
13
+ rake db:migrate
14
+ )
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'french_toast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "french_toast"
8
+ spec.version = FrenchToast::VERSION
9
+ spec.email = ["ralph@thoughtbot.com"]
10
+ spec.summary = %q{Communicate information about background jobs}
11
+ spec.description = %q{Recipe for French toast from scratch: first, invent universe}
12
+ spec.homepage = "https://github.com/thoughtbot/french_toast"
13
+ spec.license = "MIT"
14
+ spec.authors = [
15
+ "Elle Meredith",
16
+ "Eric Collins",
17
+ "Geoff Harcourt",
18
+ "George Brocklehurst",
19
+ "Ian Zabel",
20
+ "Mike Burns",
21
+ "Tyson Gach",
22
+ "Yianna Kokalas",
23
+ ]
24
+
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+
32
+ spec.add_dependency "rails", "~> 5.0"
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "pry-byebug", "~> 3.5"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.5"
37
+ spec.add_development_dependency "jquery-rails", "~> 4.3"
38
+ end
@@ -0,0 +1,4 @@
1
+ require "french_toast/version"
2
+ require "french_toast/notifier"
3
+ require "french_toast/engine"
4
+ require "generators/french_toast/install_generator"
@@ -0,0 +1,6 @@
1
+ require "french_toast"
2
+
3
+ module FrenchToast
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ module FrenchToast
2
+ class Notifier
3
+ def initialize(session_key)
4
+ @session_key = session_key
5
+ end
6
+
7
+ def notify(partial_name)
8
+ processed_payload = process(partial_name)
9
+ store(processed_payload)
10
+ notify_by_action_cable(processed_payload)
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :session_key
16
+
17
+ def process(partial_name)
18
+ ApplicationController.render(partial: "french_toast/#{partial_name}")
19
+ end
20
+
21
+ def notify_by_action_cable(payload)
22
+ ActionCable.server.broadcast(session_key, payload.html_safe)
23
+ end
24
+
25
+ def store(payload)
26
+ LastNotification.find_or_create_by(
27
+ session: session_key,
28
+ ).tap do |notification|
29
+ notification.update(data: payload)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module FrenchToast
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ module FrenchToast
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include ActiveRecord::Generators::Migration
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def copy_last_notification_migration
12
+ migration_template(
13
+ "create_last_notifications.rb",
14
+ "db/migrate/create_last_notifications.rb",
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ class CreateLastNotifications < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :last_notifications do |t|
4
+ t.timestamps
5
+
6
+ t.string :session, index: true
7
+ t.string :data
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: french_toast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elle Meredith
8
+ - Eric Collins
9
+ - Geoff Harcourt
10
+ - George Brocklehurst
11
+ - Ian Zabel
12
+ - Mike Burns
13
+ - Tyson Gach
14
+ - Yianna Kokalas
15
+ autorequire:
16
+ bindir: exe
17
+ cert_chain: []
18
+ date: 2017-10-06 00:00:00.000000000 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ type: :runtime
28
+ prerelease: false
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ - !ruby/object:Gem::Dependency
35
+ name: bundler
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ - !ruby/object:Gem::Dependency
49
+ name: pry-byebug
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ - !ruby/object:Gem::Dependency
77
+ name: rspec
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.5'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.5'
90
+ - !ruby/object:Gem::Dependency
91
+ name: jquery-rails
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.3'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.3'
104
+ description: 'Recipe for French toast from scratch: first, invent universe'
105
+ email:
106
+ - ralph@thoughtbot.com
107
+ executables: []
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".travis.yml"
114
+ - CHANGELOG.md
115
+ - CONTRIBUTING.md
116
+ - Gemfile
117
+ - LICENSE.md
118
+ - README.md
119
+ - Rakefile
120
+ - app/assets/javascripts/french_toast.js
121
+ - app/helpers/french_toast/helper.rb
122
+ - app/mailers/french_toast/notifier_mailer.rb
123
+ - app/models/french_toast/last_notification.rb
124
+ - app/views/application/_french_toast_template.html.erb
125
+ - bin/console
126
+ - bin/setup
127
+ - french_toast.gemspec
128
+ - lib/french_toast.rb
129
+ - lib/french_toast/engine.rb
130
+ - lib/french_toast/notifier.rb
131
+ - lib/french_toast/version.rb
132
+ - lib/generators/french_toast/install_generator.rb
133
+ - lib/generators/french_toast/templates/create_last_notifications.rb
134
+ homepage: https://github.com/thoughtbot/french_toast
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.11
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Communicate information about background jobs
158
+ test_files: []