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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46e2a834d05504a734dbbdae8e4c4771eeb3f545
4
- data.tar.gz: 4bf622793d5201ec98bc2f2574a314a911e5d790
3
+ metadata.gz: e5d5cfa5c4012876f5fbf4b42c9a76cd0e9fabc9
4
+ data.tar.gz: 38b137dee602a13271249a210fe339a5ee3e0706
5
5
  SHA512:
6
- metadata.gz: 35e934940725feada8e1332c653c2555ffcf3c5ce2de15d3f28ce3c9e47db8482f73f0aa46f5cbad524bed80724ba852ed5b6a37c8ba91260a5a8d3ea2e114b4
7
- data.tar.gz: ca047a12c2b5e06d0aca3f58bfc26ebf04439d7722eb2c1ce1071a1eda1198745a46666d7ebcd87dc3cd53ff92e62b23e2d947fc83ec7ee3fe5b90a0c87de203
6
+ metadata.gz: 7c71389b06c1bfde98300cd23f23d034664897dc19e77479551b6607356fae958806a6092e66d126303a8b73b2557b8cfc8a4182f9ff9eb46060a5bedc8d0514
7
+ data.tar.gz: b682a01f45ac4d4153fac7a20571e0378d8f94ef025e27263a4b27c2e9f2070d82dcd4dd624d634dc4d5288ad73da39108e98ab833347806d85d28d75f341746
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- self_renderer (0.1.1)
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.0.2)
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.1)
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.1)
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.14.6
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).render_to_html(template: "users/show.html.erb")
48
- User.find(2).render_to_html(partial: "users/_item.html.erb")
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).render_to_json(template: "users/show.json.jbuilder")
52
- User.find(4).render_to_json(partial: "users/_item.json.jbuilder")
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.jbuilder")
56
- User.find(6).render_to_hash(partial: "users/show.json.jbuilder")
59
+ User.find(5).render_to_hash(template: "users/show.json")
60
+ User.find(6).render_to_hash(partial: "users/item.json")
57
61
  ```
@@ -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 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
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
- JSON.load render_to_json(template: template, partial: partial, assigns: assigns, locals: locals)
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
- # HACK: get around limitations in devise/warden when rendering
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
@@ -1,3 +1,3 @@
1
1
  module SelfRenderer
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  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.1.1
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-04-12 00:00:00.000000000 Z
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.11
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