self_renderer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +92 -0
- data/LICENSE.txt +21 -0
- data/README.md +36 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/self_renderer/version.rb +3 -0
- data/lib/self_renderer.rb +49 -0
- data/test/self_renderer_test.rb +11 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 848e357e1ba51e052c701abea3fbeb1a8f289969
|
4
|
+
data.tar.gz: 7241ccc769b20cfe5f4a5c0d3a5a7c9976723bf7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd399730d8ccf230e03ea73fdb0969a8cba1a1f27b2c359fe299611e59a815cf575002b5611fdc20d68a72c8909a98121324eef635ccafaf1a36e414a6defc73
|
7
|
+
data.tar.gz: 29a9655309e952a3f9599b59f2fcfac7870e5e6cd0a52fa704f0edcd2d5e68582d4b19a1195cf35bfa6b798500f94f52f1f73c68dffc5ea751698ac7b7b3b39b
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
self_renderer (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.0.2)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
ast (2.3.0)
|
16
|
+
binding_of_caller (0.7.2)
|
17
|
+
debug_inspector (>= 0.0.1)
|
18
|
+
byebug (9.0.6)
|
19
|
+
coderay (1.1.1)
|
20
|
+
concurrent-ruby (1.0.5)
|
21
|
+
coveralls (0.8.20)
|
22
|
+
json (>= 1.8, < 3)
|
23
|
+
simplecov (~> 0.14.1)
|
24
|
+
term-ansicolor (~> 1.3)
|
25
|
+
thor (~> 0.19.4)
|
26
|
+
tins (~> 1.6)
|
27
|
+
debug_inspector (0.0.2)
|
28
|
+
docile (1.1.5)
|
29
|
+
i18n (0.8.1)
|
30
|
+
interception (0.5)
|
31
|
+
json (2.0.4)
|
32
|
+
method_source (0.8.2)
|
33
|
+
minitest (5.10.1)
|
34
|
+
os (1.0.0)
|
35
|
+
parser (2.4.0.0)
|
36
|
+
ast (~> 2.2)
|
37
|
+
powerpack (0.1.1)
|
38
|
+
pry (0.10.4)
|
39
|
+
coderay (~> 1.1.0)
|
40
|
+
method_source (~> 0.8.1)
|
41
|
+
slop (~> 3.4)
|
42
|
+
pry-byebug (3.4.2)
|
43
|
+
byebug (~> 9.0)
|
44
|
+
pry (~> 0.10)
|
45
|
+
pry-rescue (1.4.5)
|
46
|
+
interception (>= 0.5)
|
47
|
+
pry
|
48
|
+
pry-stack_explorer (0.4.9.2)
|
49
|
+
binding_of_caller (>= 0.7)
|
50
|
+
pry (>= 0.9.11)
|
51
|
+
pry-test (0.6.4)
|
52
|
+
os
|
53
|
+
pry
|
54
|
+
pry-byebug
|
55
|
+
pry-rescue
|
56
|
+
pry-stack_explorer
|
57
|
+
rainbow (2.2.1)
|
58
|
+
rake (12.0.0)
|
59
|
+
rubocop (0.48.1)
|
60
|
+
parser (>= 2.3.3.1, < 3.0)
|
61
|
+
powerpack (~> 0.1)
|
62
|
+
rainbow (>= 1.99.1, < 3.0)
|
63
|
+
ruby-progressbar (~> 1.7)
|
64
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
65
|
+
ruby-progressbar (1.8.1)
|
66
|
+
simplecov (0.14.1)
|
67
|
+
docile (~> 1.1.0)
|
68
|
+
json (>= 1.8, < 3)
|
69
|
+
simplecov-html (~> 0.10.0)
|
70
|
+
simplecov-html (0.10.0)
|
71
|
+
slop (3.6.0)
|
72
|
+
term-ansicolor (1.5.0)
|
73
|
+
tins (~> 1.0)
|
74
|
+
thor (0.19.4)
|
75
|
+
thread_safe (0.3.6)
|
76
|
+
tins (1.13.2)
|
77
|
+
tzinfo (1.2.3)
|
78
|
+
thread_safe (~> 0.1)
|
79
|
+
unicode-display_width (1.2.1)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
coveralls
|
86
|
+
pry-test
|
87
|
+
rake
|
88
|
+
rubocop
|
89
|
+
self_renderer!
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
1.14.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nathan Hopkins
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# SelfRenderer
|
2
|
+
|
3
|
+
Rails model & object rendering outside the context of web requests.
|
4
|
+
|
5
|
+
## Use Cases
|
6
|
+
|
7
|
+
* Serialize model in background job to send over ActionCable
|
8
|
+
* etc....
|
9
|
+
|
10
|
+
## Quick Start
|
11
|
+
|
12
|
+
```sh
|
13
|
+
gem install self_renderer
|
14
|
+
```
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require "self_renderer"
|
18
|
+
|
19
|
+
class User < ApplicationRecord
|
20
|
+
include SelfRenderer
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# render html strings
|
26
|
+
User.find(1).render_to_html(template: "users/show.html.erb")
|
27
|
+
User.find(2).render_to_html(partial: "users/_item.html.erb")
|
28
|
+
|
29
|
+
# render json strings
|
30
|
+
User.find(3).render_to_json(template: "users/show.json.jbuilder")
|
31
|
+
User.find(4).render_to_json(partial: "users/_item.json.jbuilder")
|
32
|
+
|
33
|
+
# render ruby hashes
|
34
|
+
User.find(5).render_to_hash(template: "users/show.json.jbuilder")
|
35
|
+
User.find(6).render_to_hash(partial: "users/show.json.jbuilder")
|
36
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "self_renderer"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "json"
|
2
|
+
require "active_support/core_ext/string/inflections"
|
3
|
+
require "self_renderer/version"
|
4
|
+
|
5
|
+
module SelfRenderer
|
6
|
+
# Renders an HTML string reprsenting this object
|
7
|
+
def render_to_html(template: nil, partial: nil, assigns: {}, locals: {})
|
8
|
+
return render_self template: "#{template}.html", assigns: assigns, locals: locals if template.present?
|
9
|
+
render_self partial: "#{partial}.html", assigns: assigns, locals: locals
|
10
|
+
end
|
11
|
+
|
12
|
+
# Renders a JSON string reprsenting this object
|
13
|
+
def render_to_json(template: nil, partial: nil, assigns: {}, locals: {})
|
14
|
+
return render_self template: "#{template}.json", assigns: assigns, locals: locals if template.present?
|
15
|
+
render_self partial: "#{partial}.json", assigns: assigns, locals: locals
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a Hash representation of this object as rendered by #render_to_json
|
19
|
+
def render_to_hash(template: nil, partial: nil, assigns: {}, locals: {})
|
20
|
+
JSON.load render_to_json(template: template, partial: partial, assigns: assigns, locals: locals)
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def self_renderer
|
26
|
+
@self_renderer ||= begin
|
27
|
+
renderer = ApplicationController.renderer.new
|
28
|
+
|
29
|
+
# HACK: get around limitations in devise/warden when rendering
|
30
|
+
# views outside the contect of a formal http request
|
31
|
+
if defined? Warden
|
32
|
+
env = renderer.instance_eval { @env }
|
33
|
+
warden = Warden::Proxy.new(env, Warden::Manager.new(Rails.application))
|
34
|
+
env["warden"] = warden
|
35
|
+
end
|
36
|
+
|
37
|
+
renderer
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_self(template: nil, partial: nil, assigns: {}, locals: {})
|
42
|
+
name = self.class.name.parameterize(separator: "_")
|
43
|
+
assigns[name] = self
|
44
|
+
locals[name] = self
|
45
|
+
return self_renderer.render(template, assigns: assigns, locals: locals).squish if template
|
46
|
+
return self_renderer.render(partial: partial, assigns: assigns, locals: locals).squish if partial
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: self_renderer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Hopkins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-test
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- natehop@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- lib/self_renderer.rb
|
98
|
+
- lib/self_renderer/version.rb
|
99
|
+
- test/self_renderer_test.rb
|
100
|
+
homepage: https://github.com/hopsoft/self_renderer
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.6.11
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Rails model & object rendering outside the context of web requests
|
124
|
+
test_files:
|
125
|
+
- test/self_renderer_test.rb
|