pinkman 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pinkman/model/base.rb +80 -0
- data/lib/pinkman/serializer/base.rb +15 -2
- data/lib/pinkman/serializer/scope.rb +110 -5
- data/lib/pinkman/version.rb +1 -1
- data/lib/pinkman.rb +1 -58
- data/pinkman.gemspec +2 -1
- metadata +21 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3763200ced79aa36f7e47198b0195abdcdfd0318f3b553b3875bfcecdb1883a3
|
4
|
+
data.tar.gz: a6d43c350ee70c96812144476c408b3b46612b3b16a0b9ed4b4d2f14919b7263
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 900495e9e1845b8c5f55d70d5236bed4f1f602e548c59833bdd0fc62f1a30520d73810f93e1a08255f180cab5a34155d38dd3df59a446cdaaeb34744e83afde1
|
7
|
+
data.tar.gz: ecb1773b0a1b504c4872786460def8868949209eb91ecdc2f28005c7d73c3a7c67c43806d21127c62e9ed28c72cb7cd473f78ca11d4f3429996316f2c59c2f7d
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module Pinkman
|
4
|
+
module Model
|
5
|
+
class Base < ::ActiveRecord::Base
|
6
|
+
|
7
|
+
self.abstract_class = true
|
8
|
+
|
9
|
+
# Serializer
|
10
|
+
def self.serializer= value
|
11
|
+
@serializer = value
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.serializer
|
15
|
+
@serializer || (begin eval(self.to_s + 'Serializer') rescue nil end)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get: Pinkman way of fetching records
|
19
|
+
def self.get query
|
20
|
+
if (begin Integer(query) rescue false end)
|
21
|
+
[find(query)]
|
22
|
+
elsif query.is_a? Hash
|
23
|
+
where(query)
|
24
|
+
elsif query.is_a? Array
|
25
|
+
where(id: query)
|
26
|
+
elsif query.is_a? String
|
27
|
+
search(query)
|
28
|
+
else
|
29
|
+
[]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.optimize(scope, options = {})
|
34
|
+
if (options.keys - [:includes, :select]).any?
|
35
|
+
raise ArgumentError, "#{self}.optimize called with invalid options. Options hash accepts :includes and :select only."
|
36
|
+
end
|
37
|
+
options_includes = options[:includes] || []
|
38
|
+
options_select = options[:select] || []
|
39
|
+
|
40
|
+
scope_obj = serializer.scope(scope)
|
41
|
+
including_array = scope_obj.including + options_includes
|
42
|
+
selecting_array = scope_obj.selecting + options_select
|
43
|
+
|
44
|
+
includes(including_array).select(selecting_array)
|
45
|
+
end
|
46
|
+
def self.optim(*args)
|
47
|
+
optimize(*args)
|
48
|
+
end
|
49
|
+
|
50
|
+
def serialize_for scope_name, params_hash = {}
|
51
|
+
options = {scope: scope_name}.merge(params: params_hash)
|
52
|
+
self.class.serializer.new(self,options)
|
53
|
+
end
|
54
|
+
|
55
|
+
def json scope_name=:public, params_hash={}
|
56
|
+
serialize_for(scope_name, params_hash).to_json
|
57
|
+
end
|
58
|
+
|
59
|
+
def json_for *args, &block
|
60
|
+
ActiveSupport::Deprecation.warn('"json_for" deprecated. Use "json" instead.')
|
61
|
+
json(*args, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
def has_json_key? key, scope=:public, options={}
|
65
|
+
json_version(scope, options).has_key?(key.to_s) and json_version(scope, options)[key.to_s].present?
|
66
|
+
end
|
67
|
+
|
68
|
+
def json_version *args
|
69
|
+
JSON.parse(json(*args))
|
70
|
+
end
|
71
|
+
alias json_hash json_version
|
72
|
+
|
73
|
+
def json_key key, scope=:public, options={}
|
74
|
+
json_version(scope, options)[key.to_s]
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -17,11 +17,14 @@ module Pinkman
|
|
17
17
|
end
|
18
18
|
|
19
19
|
self.root = false
|
20
|
-
|
20
|
+
def self.table_name
|
21
|
+
model.table_name
|
22
|
+
end
|
23
|
+
|
21
24
|
def self.scope name=:public, &block
|
22
25
|
@scopes ||= {}
|
23
26
|
if block_given?
|
24
|
-
@scopes[name.to_sym] = Pinkman::Serializer::Scope.new(serializer: self)
|
27
|
+
@scopes[name.to_sym] = Pinkman::Serializer::Scope.new(serializer: self, name: name.to_s)
|
25
28
|
yield(@scopes[name.to_sym])
|
26
29
|
else
|
27
30
|
if @scopes[name.to_sym]
|
@@ -33,6 +36,10 @@ module Pinkman
|
|
33
36
|
end
|
34
37
|
end
|
35
38
|
end
|
39
|
+
|
40
|
+
def self.has_scope?(name)
|
41
|
+
scopes.has_key?(name.to_sym)
|
42
|
+
end
|
36
43
|
|
37
44
|
def self.scopes
|
38
45
|
@scopes
|
@@ -77,6 +84,12 @@ module Pinkman
|
|
77
84
|
errors.each {|k,v| e[k] = [v].flatten}
|
78
85
|
e
|
79
86
|
end
|
87
|
+
|
88
|
+
# assocs
|
89
|
+
|
90
|
+
def self.reflections
|
91
|
+
model.reflections
|
92
|
+
end
|
80
93
|
|
81
94
|
def attributes *args
|
82
95
|
hash = super(*args)
|
@@ -1,12 +1,16 @@
|
|
1
1
|
module Pinkman
|
2
2
|
module Serializer
|
3
3
|
class Scope
|
4
|
+
|
5
|
+
attr_accessor :name, :read, :write, :access, :serializer
|
4
6
|
|
5
7
|
def initialize hash
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
if hash.is_a?(Hash)
|
9
|
+
self.serializer = hash[:serializer]
|
10
|
+
self.name = hash[:name]
|
11
|
+
end
|
12
|
+
self
|
13
|
+
end
|
10
14
|
|
11
15
|
def read_ghost
|
12
16
|
@read_ghost || []
|
@@ -19,9 +23,47 @@ module Pinkman
|
|
19
23
|
def read_attributes *args
|
20
24
|
self.read = args
|
21
25
|
self.read = [] unless args.first
|
26
|
+
query_optimizer
|
22
27
|
read
|
23
28
|
end
|
24
29
|
|
30
|
+
def associations
|
31
|
+
read.select{ |attribute| attribute_is_association?(attribute) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def fields
|
35
|
+
read.select{ |attribute| attribute_is_in_db?(attribute) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def query_optimizer
|
39
|
+
select_optimizer
|
40
|
+
include_optimizer
|
41
|
+
end
|
42
|
+
|
43
|
+
def select_optimizer
|
44
|
+
unless :all.in?(read.map(&:to_sym))
|
45
|
+
fields.each do |attribute|
|
46
|
+
selecting << "#{serializer.table_name}.#{attribute}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
selecting
|
50
|
+
end
|
51
|
+
|
52
|
+
def include_optimizer
|
53
|
+
associations.each do |attribute|
|
54
|
+
including << attribute_inclusion_clause(attribute)
|
55
|
+
end
|
56
|
+
including
|
57
|
+
end
|
58
|
+
|
59
|
+
def selecting
|
60
|
+
@selecting ||= []
|
61
|
+
end
|
62
|
+
|
63
|
+
def including
|
64
|
+
@including ||= []
|
65
|
+
end
|
66
|
+
|
25
67
|
def read_ghost_attributes *args
|
26
68
|
self.read_ghost = args
|
27
69
|
self.read_ghost = [] unless args.first
|
@@ -45,7 +87,7 @@ module Pinkman
|
|
45
87
|
end
|
46
88
|
|
47
89
|
def can_write? attribute
|
48
|
-
(write.include?(:all) or write.include?(attribute.to_sym)) and (serializer.
|
90
|
+
(write.include?(:all) or write.include?(attribute.to_sym)) and (serializer.column_names.include?(attribute.to_s) or (serializer.instance_methods.include?("#{attribute.to_s}=".to_sym) and write.include?(attribute.to_sym)))
|
49
91
|
end
|
50
92
|
|
51
93
|
def can_access? action
|
@@ -63,6 +105,69 @@ module Pinkman
|
|
63
105
|
def can_access
|
64
106
|
access
|
65
107
|
end
|
108
|
+
|
109
|
+
# private
|
110
|
+
|
111
|
+
def reflections
|
112
|
+
serializer.reflections
|
113
|
+
end
|
114
|
+
|
115
|
+
def attribute_is_in_db?(attribute)
|
116
|
+
attribute.to_s.in?(serializer.model.column_names)
|
117
|
+
end
|
118
|
+
|
119
|
+
def attribute_is_association?(attribute)
|
120
|
+
attribute.to_s.in?(reflections.keys.map(&:to_s))
|
121
|
+
end
|
122
|
+
|
123
|
+
def attribute_inclusion_clause attribute
|
124
|
+
if attribute_has_nested_associated?(attribute)
|
125
|
+
a = []
|
126
|
+
assoc_scope = attribute_assoc_scope(attribute)
|
127
|
+
assoc_scope.associations.each do |assoc_attribute|
|
128
|
+
a << assoc_scope.attribute_inclusion_clause(assoc_attribute)
|
129
|
+
end
|
130
|
+
{ attribute.to_sym => a }
|
131
|
+
else
|
132
|
+
attribute.to_sym
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
def get_associated_model(reflection)
|
139
|
+
reflection.klass
|
140
|
+
end
|
141
|
+
|
142
|
+
def get_associated_serializer(attribute)
|
143
|
+
begin
|
144
|
+
get_associated_model(reflections[attribute.to_s]).serializer if attribute_is_association?(attribute.to_s)
|
145
|
+
rescue
|
146
|
+
raise ArgumentError, "#{serializer}.#{name}: association named - #{attribute} - found but I can't find its serializer."
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def attribute_assoc_scope(attribute)
|
151
|
+
assoc_serializer = get_associated_serializer(attribute)
|
152
|
+
if assoc_serializer
|
153
|
+
assoc_serializer.scope(name.to_sym)
|
154
|
+
else
|
155
|
+
raise ArgumentError, "#{serializer}.#{name}: association named - #{attribute} - found but I can't find its serializer."
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# given that a attribute is a association,
|
160
|
+
# and the associated serializer has same scope defined,
|
161
|
+
# checks if a nested association is present
|
162
|
+
def attribute_has_nested_associated?(attribute)
|
163
|
+
attribute = attribute.to_s
|
164
|
+
if attribute_is_association?(attribute)
|
165
|
+
assoc_scope = attribute_assoc_scope(attribute)
|
166
|
+
assoc_scope && assoc_scope.associations.any? && assoc_scope.serializer.model != serializer.model
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
66
170
|
end
|
171
|
+
|
67
172
|
end
|
68
173
|
end
|
data/lib/pinkman/version.rb
CHANGED
data/lib/pinkman.rb
CHANGED
@@ -2,6 +2,7 @@ require 'rails'
|
|
2
2
|
require 'pathname'
|
3
3
|
require 'active_model_serializers'
|
4
4
|
require 'pinkman/serializer'
|
5
|
+
require 'pinkman/model/base'
|
5
6
|
require 'pinkman/views_helpers'
|
6
7
|
require 'pinkman/form_helper'
|
7
8
|
require 'pinkman/version'
|
@@ -37,34 +38,6 @@ module Pinkman
|
|
37
38
|
Slim::Engine.set_options(attr_list_delims: {'(' => ')', '[' => ']'})
|
38
39
|
end
|
39
40
|
|
40
|
-
# Extending ActiveRecord
|
41
|
-
ActiveRecord::Base.singleton_class.class_eval do
|
42
|
-
|
43
|
-
# Serializer
|
44
|
-
def serializer= value
|
45
|
-
@serializer = value
|
46
|
-
end
|
47
|
-
|
48
|
-
def serializer
|
49
|
-
@serializer || (begin eval(self.to_s + 'Serializer') rescue nil end)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get: Pinkman way of fetching records
|
53
|
-
def get query
|
54
|
-
if (begin Integer(query) rescue false end)
|
55
|
-
[find(query)]
|
56
|
-
elsif query.is_a? Hash
|
57
|
-
where(query)
|
58
|
-
elsif query.is_a? Array
|
59
|
-
where(id: query)
|
60
|
-
elsif query.is_a? String
|
61
|
-
search(query)
|
62
|
-
else
|
63
|
-
[]
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
41
|
|
69
42
|
# Active Record Relation: json
|
70
43
|
ActiveRecord::Relation.class_eval do
|
@@ -101,36 +74,6 @@ module Pinkman
|
|
101
74
|
end
|
102
75
|
end
|
103
76
|
|
104
|
-
# Instance method: json
|
105
|
-
ActiveRecord::Base.class_eval do
|
106
|
-
def serialize_for scope_name, params_hash = {}
|
107
|
-
options = {scope: scope_name}.merge(params: params_hash)
|
108
|
-
self.class.serializer.new(self,options)
|
109
|
-
end
|
110
|
-
|
111
|
-
def json scope_name=:public, params_hash={}
|
112
|
-
serialize_for(scope_name, params_hash).to_json
|
113
|
-
end
|
114
|
-
|
115
|
-
def json_for *args, &block
|
116
|
-
ActiveSupport::Deprecation.warn('"json_for" deprecated. Use "json" instead.')
|
117
|
-
json(*args, &block)
|
118
|
-
end
|
119
|
-
|
120
|
-
def has_json_key? key, scope=:public, options={}
|
121
|
-
json_version(scope, options).has_key?(key.to_s) and json_version(scope, options)[key.to_s].present?
|
122
|
-
end
|
123
|
-
|
124
|
-
def json_version *args
|
125
|
-
JSON.parse(json(*args))
|
126
|
-
end
|
127
|
-
alias json_hash json_version
|
128
|
-
|
129
|
-
def json_key key, scope=:public, options={}
|
130
|
-
json_version(scope, options)[key.to_s]
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|
134
77
|
end
|
135
78
|
|
136
79
|
end
|
data/pinkman.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
32
|
spec.require_paths = ['lib']
|
33
33
|
|
34
|
-
spec.add_development_dependency 'bundler'
|
34
|
+
spec.add_development_dependency 'bundler'
|
35
35
|
spec.add_development_dependency 'coffee-script'
|
36
36
|
spec.add_development_dependency 'sprockets', '~> 3.0'
|
37
37
|
spec.add_development_dependency 'uglifier'
|
@@ -41,6 +41,7 @@ Gem::Specification.new do |spec|
|
|
41
41
|
spec.add_development_dependency 'rails'
|
42
42
|
|
43
43
|
spec.add_dependency 'rails'
|
44
|
+
spec.add_dependency 'activerecord'
|
44
45
|
spec.add_dependency 'jquery-rails'
|
45
46
|
spec.add_dependency 'active_model_serializers', '~> 0.9.0'
|
46
47
|
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pinkman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Agilso Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: coffee-script
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: activerecord
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: jquery-rails
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -273,6 +287,7 @@ files:
|
|
273
287
|
- lib/pinkman/broadcaster.rb
|
274
288
|
- lib/pinkman/form_helper.rb
|
275
289
|
- lib/pinkman/form_helper/form_helpers.rb
|
290
|
+
- lib/pinkman/model/base.rb
|
276
291
|
- lib/pinkman/serializer.rb
|
277
292
|
- lib/pinkman/serializer/base.rb
|
278
293
|
- lib/pinkman/serializer/scope.rb
|