self_renderer 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +5 -5
- data/README.md +10 -6
- data/lib/self_renderer.rb +12 -22
- data/lib/self_renderer/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5d5cfa5c4012876f5fbf4b42c9a76cd0e9fabc9
|
4
|
+
data.tar.gz: 38b137dee602a13271249a210fe339a5ee3e0706
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c71389b06c1bfde98300cd23f23d034664897dc19e77479551b6607356fae958806a6092e66d126303a8b73b2557b8cfc8a4182f9ff9eb46060a5bedc8d0514
|
7
|
+
data.tar.gz: b682a01f45ac4d4153fac7a20571e0378d8f94ef025e27263a4b27c2e9f2070d82dcd4dd624d634dc4d5288ad73da39108e98ab833347806d85d28d75f341746
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
self_renderer (0.
|
4
|
+
self_renderer (1.0.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (5.
|
10
|
+
activesupport (5.1.4)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (~> 0.7)
|
13
13
|
minitest (~> 5.1)
|
@@ -26,11 +26,11 @@ GEM
|
|
26
26
|
tins (~> 1.6)
|
27
27
|
debug_inspector (0.0.2)
|
28
28
|
docile (1.1.5)
|
29
|
-
i18n (0.8.
|
29
|
+
i18n (0.8.6)
|
30
30
|
interception (0.5)
|
31
31
|
json (2.0.4)
|
32
32
|
method_source (0.8.2)
|
33
|
-
minitest (5.10.
|
33
|
+
minitest (5.10.3)
|
34
34
|
os (1.0.0)
|
35
35
|
parser (2.4.0.0)
|
36
36
|
ast (~> 2.2)
|
@@ -89,4 +89,4 @@ DEPENDENCIES
|
|
89
89
|
self_renderer!
|
90
90
|
|
91
91
|
BUNDLED WITH
|
92
|
-
1.
|
92
|
+
1.15.4
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[![Lines of Code](http://img.shields.io/badge/lines_of_code-33-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
|
2
|
+
[![Code Status](http://img.shields.io/codeclimate/github/hopsoft/self_renderer.svg?style=flat)](https://codeclimate.com/github/hopsoft/self_renderer)
|
3
|
+
[![Dependency Status](http://img.shields.io/gemnasium/hopsoft/self_renderer.svg?style=flat)](https://gemnasium.com/hopsoft/self_renderer)
|
4
|
+
|
1
5
|
# SelfRenderer
|
2
6
|
|
3
7
|
Rails model & object rendering outside the context of web requests.
|
@@ -44,14 +48,14 @@ json.name @user.name
|
|
44
48
|
|
45
49
|
```ruby
|
46
50
|
# render html strings
|
47
|
-
User.find(1).
|
48
|
-
User.find(2).
|
51
|
+
User.find(1).render_with(template: "users/show.html")
|
52
|
+
User.find(2).render_with(partial: "users/item.html")
|
49
53
|
|
50
54
|
# render json strings
|
51
|
-
User.find(3).
|
52
|
-
User.find(4).
|
55
|
+
User.find(3).render_with(template: "users/show.json")
|
56
|
+
User.find(4).render_with(partial: "users/item.json")
|
53
57
|
|
54
58
|
# render ruby hashes
|
55
|
-
User.find(5).render_to_hash(template: "users/show.json
|
56
|
-
User.find(6).render_to_hash(partial: "users/
|
59
|
+
User.find(5).render_to_hash(template: "users/show.json")
|
60
|
+
User.find(6).render_to_hash(partial: "users/item.json")
|
57
61
|
```
|
data/lib/self_renderer.rb
CHANGED
@@ -3,21 +3,21 @@ require "active_support/core_ext/string/inflections"
|
|
3
3
|
require "self_renderer/version"
|
4
4
|
|
5
5
|
module SelfRenderer
|
6
|
-
# Renders an
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
return render_self template: "#{template}.json", assigns: assigns, locals: locals if template.present?
|
15
|
-
render_self partial: "#{partial}.json", assigns: assigns, locals: locals
|
6
|
+
# Renders an string representing this object using the specified template or partial
|
7
|
+
def render_with(template: nil, partial: nil, assigns: {}, locals: {})
|
8
|
+
name = self.class.name.parameterize(separator: "_").to_sym
|
9
|
+
assigns[name] = self
|
10
|
+
locals[name] = self
|
11
|
+
return self_renderer.render(template, assigns: assigns, locals: locals).squish if template.present?
|
12
|
+
return self_renderer.render(partial: partial, assigns: assigns, locals: locals).squish if partial.present?
|
13
|
+
nil
|
16
14
|
end
|
17
15
|
|
18
16
|
# Returns a Hash representation of this object as rendered by #render_to_json
|
19
17
|
def render_to_hash(template: nil, partial: nil, assigns: {}, locals: {})
|
20
|
-
|
18
|
+
raise ArgumentError.new("Must use json template") unless template.nil? || template.to_s.include?(".json")
|
19
|
+
raise ArgumentError.new("Must use json partial") unless partial.nil? || partial.to_s.include?(".json")
|
20
|
+
JSON.load render_with(template: template, partial: partial, assigns: assigns, locals: locals)
|
21
21
|
end
|
22
22
|
|
23
23
|
protected
|
@@ -26,8 +26,7 @@ module SelfRenderer
|
|
26
26
|
@self_renderer ||= begin
|
27
27
|
renderer = ApplicationController.renderer.new
|
28
28
|
|
29
|
-
#
|
30
|
-
# views outside the contect of a formal http request
|
29
|
+
# get around limitations in devise/warden when rendering views outside the contect of a formal http request
|
31
30
|
if defined? Warden
|
32
31
|
env = renderer.instance_eval { @env }
|
33
32
|
warden = Warden::Proxy.new(env, Warden::Manager.new(Rails.application))
|
@@ -37,13 +36,4 @@ module SelfRenderer
|
|
37
36
|
renderer
|
38
37
|
end
|
39
38
|
end
|
40
|
-
|
41
|
-
def render_self(template: nil, partial: nil, assigns: {}, locals: {})
|
42
|
-
name = self.class.name.parameterize(separator: "_").to_sym
|
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
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: self_renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Hopkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
117
|
version: '0'
|
118
118
|
requirements: []
|
119
119
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.6.
|
120
|
+
rubygems_version: 2.6.13
|
121
121
|
signing_key:
|
122
122
|
specification_version: 4
|
123
123
|
summary: Rails model & object rendering outside the context of web requests
|