client_data 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/client_data/before_render.rb +41 -0
- data/lib/client_data/builder.rb +6 -0
- data/lib/client_data/methods.rb +70 -40
- data/lib/client_data/version.rb +1 -1
- data/lib/client_data.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11f350caa58a1d9912dbf17d79541886b0ff30dd
|
4
|
+
data.tar.gz: aa876738e11ba40782ba84cf50ff4c82417c5f5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 509626eced24f1ed1cf92d92da9a6f76830b63ded9bdbb62cb69d9e3eef5bcd0f098631900d999ddef064213bb920b337188a980d7ac7360803d3a36e9a02e5c
|
7
|
+
data.tar.gz: acc3f301f35175dfd8a529d214e7065c5f2006964e0d5c17c6ba45dbc049d8f8f2f5df5bf92487ae6a7dcf09de7652f11b3e54c3f658d4761b153ad19f7b49f9
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# Thanks to: https://coderwall.com/p/ih8atg
|
2
|
+
module ClientData
|
3
|
+
module BeforeRender
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
alias_method_chain :render, :before_render_action
|
8
|
+
define_callbacks :render
|
9
|
+
end
|
10
|
+
|
11
|
+
def render_with_before_render_action(*options, &block)
|
12
|
+
run_callbacks :render do
|
13
|
+
render_without_before_render_action(*options, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def append_before_render_action(*names, &block)
|
19
|
+
_insert_callbacks(names, block) do |name, options|
|
20
|
+
set_callback :render, :before, name, options
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def prepend_before_render_action(*names, &block)
|
25
|
+
_insert_callbacks(names, block) do |name, options|
|
26
|
+
set_callback :render, :before, name, options.merge(:prepend => true)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def skip_before_render_action(*names, &block)
|
31
|
+
_insert_callbacks(names, block) do |name, options|
|
32
|
+
skip_callback :render, :before, name, options
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
alias_method :before_render, :append_before_render_action
|
37
|
+
alias_method :prepend_before_render, :prepend_before_render_action
|
38
|
+
alias_method :skip_before_render, :skip_before_render_action
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/client_data/builder.rb
CHANGED
data/lib/client_data/methods.rb
CHANGED
@@ -1,62 +1,92 @@
|
|
1
1
|
module ClientData
|
2
2
|
module Methods
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
send(:include, ClientData::BeforeRender) unless respond_to?(:before_render)
|
7
|
+
before_render :client_data_builder_filter
|
8
|
+
|
9
|
+
class_eval %(
|
10
|
+
class << self; attr_accessor :__cs_builders; end
|
7
11
|
)
|
8
|
-
klass.send(:include, InstanceMethods)
|
9
|
-
klass.send(:extend, ClassMethods)
|
10
12
|
end
|
11
13
|
|
12
14
|
module ClassMethods
|
13
|
-
def client_data(*
|
14
|
-
|
15
|
-
self.__cs_builders
|
15
|
+
def client_data(*build_keys)
|
16
|
+
options = build_keys.extract_options!
|
17
|
+
self.__cs_builders ||= {}
|
18
|
+
build_keys.each do |key|
|
19
|
+
self.__cs_builders[key] = options
|
20
|
+
end
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
rescue ::NameError => e
|
26
|
-
raise ClientData::Error, "Unable to find constant #{klass}, " \
|
27
|
-
"has ClientData.load_resources! been called? " \
|
28
|
-
"Error: #{e.message}"
|
29
|
-
end
|
30
|
-
|
31
|
-
data = build_data(klass)
|
32
|
-
provider.set(key, data)
|
24
|
+
def builders
|
25
|
+
@builders ||= begin
|
26
|
+
builders_hash = {}
|
27
|
+
builder_options_hash.each do |key, options|
|
28
|
+
name = options[:as].nil? ? key : options[:as]
|
29
|
+
builders_hash[name] = create_builder(key)
|
33
30
|
end
|
31
|
+
builders_hash
|
34
32
|
end
|
33
|
+
end
|
35
34
|
|
36
|
-
|
35
|
+
protected
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
builder
|
37
|
+
def client_data_builder_filter
|
38
|
+
builder_options_hash.each do |key, options|
|
39
|
+
name = options[:as].nil? ? key : options[:as]
|
40
|
+
builder = builders[name]
|
41
|
+
provider.set(name.to_s, builder.build) if should_build_for?(key)
|
42
42
|
end
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
def should_build_for?(key)
|
46
|
+
options = builder_options_hash[key] || {}
|
47
|
+
action = self.action_name.to_sym
|
48
|
+
only = options[:only]
|
49
|
+
if only.nil?
|
50
|
+
true
|
51
|
+
else
|
52
|
+
if only.is_a?(Symbol)
|
53
|
+
only = [only]
|
54
|
+
end
|
55
|
+
only.include?(action)
|
46
56
|
end
|
57
|
+
end
|
47
58
|
|
48
|
-
|
49
|
-
|
59
|
+
def create_builder(key)
|
60
|
+
begin
|
61
|
+
klass = key
|
62
|
+
if klass.is_a?(Symbol) || klass.is_a?(String)
|
63
|
+
klass = "#{builder_namespace}#{key.to_s.camelize}Builder"
|
64
|
+
klass = klass.constantize
|
65
|
+
end
|
66
|
+
rescue ::NameError => e
|
67
|
+
raise ClientData::Error, "Unable to find constant #{klass}, " \
|
68
|
+
"has ClientData.load_resources! been called? " \
|
69
|
+
"Error: #{e.message}"
|
50
70
|
end
|
51
71
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
72
|
+
klass.new.tap { |o| o.controller = self }
|
73
|
+
end
|
74
|
+
|
75
|
+
def builder_namespace
|
76
|
+
ClientData.configuration.builder_namespace
|
77
|
+
end
|
78
|
+
|
79
|
+
def provider
|
80
|
+
@_builder_provider ||= Adapters.factory(self)
|
81
|
+
end
|
82
|
+
|
83
|
+
def builder_options_hash
|
84
|
+
@_builder_options_hash ||= begin
|
85
|
+
keys = {}
|
86
|
+
klass = self.class
|
87
|
+
loop do
|
88
|
+
keys.merge!(klass.__cs_builders || {}) if klass.respond_to?(:__cs_builders)
|
89
|
+
break keys unless klass.respond_to?(:superclass) && klass = klass.superclass
|
60
90
|
end
|
61
91
|
end
|
62
92
|
end
|
data/lib/client_data/version.rb
CHANGED
data/lib/client_data.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: client_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stan Bondi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03
|
11
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/client_data.rb
|
68
68
|
- lib/client_data/adapters.rb
|
69
69
|
- lib/client_data/adapters/gon_adapter.rb
|
70
|
+
- lib/client_data/before_render.rb
|
70
71
|
- lib/client_data/builder.rb
|
71
72
|
- lib/client_data/configuration.rb
|
72
73
|
- lib/client_data/methods.rb
|
@@ -97,3 +98,4 @@ signing_key:
|
|
97
98
|
specification_version: 4
|
98
99
|
summary: Use builder classes to build up data which JS can use.
|
99
100
|
test_files: []
|
101
|
+
has_rdoc:
|