jbuilder-jpartial 0.1.2 → 0.1.3

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: fd2f9434cebd1a5017cad21be3ca7fea80c032df
4
- data.tar.gz: d2411db16a9402ca9e6c4598cc04369b0c2aba80
3
+ metadata.gz: 75e8c4257f48d6ef56dcaaf8047bef63df892137
4
+ data.tar.gz: 4f22e415d52ea9f82f097f5e05e2a42c22200c9a
5
5
  SHA512:
6
- metadata.gz: f4b7ea3b7df144a67dac206210cba740d1f5bea80c948fde961122243bdb4da4d2f9559b1a2a7b0471a96a3352e0918144ce2868be1c9199c3c4dba6dd02dbc2
7
- data.tar.gz: a9778f7acc70ae28acd6421bdfaa29e8341f4578131913e83da817f018a5e168fef69565ebad0b102f53242b018cfcaf5fb67042c12b5f291f16ea28eba3358f
6
+ metadata.gz: da10106742ea3e98614392ee921e09caf4d8f77d6047632e4b1d4a9a065b01253d704c7409c8045dfb84d5804e411ba97a14b47eb61194e24499a94f0af70238
7
+ data.tar.gz: 118c304e9eca4ac54df216e59bc8146ca10e427b6dbae58a9e46ed807557cd53b8eea1942cd3e4a04e2487384f257e1d2c992dda09ec627ac1c3af9812acee33
@@ -20,19 +20,37 @@ class Jbuilder
20
20
  module Jpartial
21
21
  DangerousMethodName = Class.new(ArgumentError)
22
22
 
23
+ @defined_by_user = []
24
+
23
25
  def self.jpartial(name, &block)
24
- Jbuilder.class_eval do
25
- if method_defined?(name) || private_method_defined?(name)
26
- raise DangerousMethodName, "The method `##{name}` is already defined"\
26
+ if dangerous_method_name?(name)
27
+ raise DangerousMethodName, "The method `##{name}` is already defined"\
27
28
  ' by Jbuilder. Please choose another name to define your partial'
28
- else
29
+ else
30
+ Jbuilder.class_eval do
29
31
  define_method(name, &block)
30
32
  end
33
+ @defined_by_user << name unless @defined_by_user.include?(name)
31
34
  end
32
35
  end
33
36
 
37
+ def self.dangerous_method_name?(name)
38
+ !@defined_by_user.include?(name) &&
39
+ Jbuilder.method_defined?(name) ||
40
+ Jbuilder.private_method_defined?(name)
41
+ end
42
+
34
43
  def self.configure(&block)
35
44
  module_eval(&block)
36
45
  end
46
+
47
+ # Sends all method calls to Jpartial.jpartial for definition
48
+ class Template
49
+ def method_missing(method_name, &block)
50
+ Jpartial.jpartial(method_name, &block)
51
+ end
52
+ end
37
53
  end
38
54
  end
55
+
56
+ require 'jbuilder/jpartial/railtie' if defined?(Rails)
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+ require 'jbuilder/jbuilder_template'
5
+ require 'jbuilder/jpartial'
6
+
7
+ class Jbuilder
8
+ module Jpartial
9
+ # Adds the #json and #jpartial methods to .jbuilder templates
10
+ class JpartialHandler < ::JbuilderHandler
11
+ def self.call(template)
12
+ %{__already_defined = defined?(json); json||=JbuilderTemplate.new(self);
13
+ jpartial||=Jbuilder::Jpartial::Template.new;
14
+ #{template.source} json.target! unless
15
+ (__already_defined && __already_defined != "method")}
16
+ end
17
+ end
18
+
19
+ # Loads the template handler, overriding the JbuilderTemplate
20
+ class Railtie < ::Rails::Railtie
21
+ initializer :jpartial do
22
+ ActiveSupport.on_load :action_view do
23
+ ActionView::Template.register_template_handler :jbuilder,
24
+ JpartialHandler
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  class Jbuilder
2
2
  module Jpartial
3
- VERSION = '0.1.2'.freeze
3
+ VERSION = '0.1.3'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder-jpartial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
@@ -62,6 +62,7 @@ files:
62
62
  - LICENSE.txt
63
63
  - README.md
64
64
  - lib/jbuilder/jpartial.rb
65
+ - lib/jbuilder/jpartial/railtie.rb
65
66
  - lib/jbuilder/jpartial/version.rb
66
67
  homepage: https://github.com/msimonborg/jbuilder-jpartial
67
68
  licenses: