pinkman 0.9.1.3 → 0.9.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/pinkman_base/collection.coffee +19 -10
- data/app/assets/javascripts/pinkman_base/pinkman.coffee +14 -0
- data/lib/generators/pinkman/templates/api_controller.rb +8 -0
- data/lib/pinkman.rb +16 -6
- data/lib/pinkman/serializer.rb +5 -0
- data/lib/pinkman/serializer/base.rb +43 -4
- data/lib/pinkman/serializer/scope.rb +9 -3
- data/lib/pinkman/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 399b3c9f42d0fcb24b807a240ad12aa788db9331
|
4
|
+
data.tar.gz: df42a3182412ac46d3f9b9dc5ca7500e8b82f719
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb465a093d630bdf86a411f81d2d4215a0209bdf3463acdc3c8b219d0fbc90017bcb0257189c67be6c75c2731c99432bbd77b7f2191db0ddf0d77e1580865c08
|
7
|
+
data.tar.gz: 2479695b881b68b6d3ee1e7f1e1f05bf9a392817a94e7496b57e30794b045cb4e30f8da22eec2e402dc6421781d5b32af14f70e9c317ed79e420dd94ec282539
|
@@ -347,22 +347,21 @@ class window.PinkmanCollection extends window.PinkmanCommon
|
|
347
347
|
# Example: fetching all orders of a user
|
348
348
|
# coffee: orders.fetchFrom 'user', user.id
|
349
349
|
# api::controler#order: render json: User.find(params[:id]).orders.to_json
|
350
|
-
fetchFrom: (action,id,callback
|
350
|
+
fetchFrom: (action,id,callback) ->
|
351
351
|
if action? and id?
|
352
|
-
@fetchFromUrl
|
352
|
+
@fetchFromUrl
|
353
|
+
url: @api() + "#{action}/#{id}"
|
354
|
+
callback: callback
|
353
355
|
else
|
354
356
|
return false
|
355
357
|
|
356
|
-
|
357
358
|
# Desc: Fetch records from URL
|
358
359
|
# request: get /api/API_URL/
|
359
360
|
fetchFromUrl: (options) ->
|
360
361
|
if options? and typeof options == 'object' and options.url?
|
361
|
-
limit = if options.limit? then options.limit else 1000
|
362
|
-
offset = if options.offset? then options.offset else 0
|
363
362
|
@fetchingFrom = options.url
|
364
363
|
Pinkman.ajax.get
|
365
|
-
url: options.url
|
364
|
+
url: Pinkman.json2url(options.url,options.params)
|
366
365
|
complete: (response) =>
|
367
366
|
if response.errors? or response.error?
|
368
367
|
[@errors, @error] = [response.errors, response.error]
|
@@ -378,15 +377,25 @@ class window.PinkmanCollection extends window.PinkmanCommon
|
|
378
377
|
# in rails: api::controller#index
|
379
378
|
fetchMore: (n=10,callback='') ->
|
380
379
|
if @fetchingFrom?
|
381
|
-
@fetchFromUrl
|
380
|
+
@fetchFromUrl
|
381
|
+
url: @fetchingFrom
|
382
|
+
params:
|
383
|
+
limit: n
|
384
|
+
offset: @count()
|
385
|
+
callback: callback
|
382
386
|
else
|
383
|
-
@fetchFromUrl
|
387
|
+
@fetchFromUrl
|
388
|
+
url: @api()
|
389
|
+
params:
|
390
|
+
limit: n
|
391
|
+
offset: @count()
|
392
|
+
callback: callback
|
384
393
|
|
385
394
|
# Desc: Fetch next n records from a action
|
386
395
|
# request: get /api/API_URL/:action/?offset="COLLECTION_SIZE"&limit=n
|
387
396
|
# in rails: api::controller#action
|
388
397
|
fetchMoreFrom: (n,action,id,callback='') ->
|
389
|
-
@fetchFromUrl {url: @api() + "#{action}/#{id}", limit: n, offset: @count(), callback: callback}
|
398
|
+
@fetchFromUrl {url: @api() + "#{action}/#{id}", params: {limit: n, offset: @count()}, callback: callback}
|
390
399
|
|
391
400
|
# Desc: Connect to api to search in this model a query
|
392
401
|
# request: get /api/API_URL/search?query=YOUR_QUERY
|
@@ -394,6 +403,6 @@ class window.PinkmanCollection extends window.PinkmanCommon
|
|
394
403
|
# assume models to have a Model.search("YOUR_QUERY") method.
|
395
404
|
search: (query,callback='') ->
|
396
405
|
@removeAll()
|
397
|
-
@fetchFromUrl { url: @api()
|
406
|
+
@fetchFromUrl { url: @api(), params: {query: query}, callback: callback }
|
398
407
|
|
399
408
|
window.Pinkman.collection = window.PinkmanCollection
|
@@ -45,6 +45,20 @@ class window.Pinkman
|
|
45
45
|
@calledFunctions.push(func)
|
46
46
|
f(args...)
|
47
47
|
|
48
|
+
@json2url: (url,opts) ->
|
49
|
+
if url?
|
50
|
+
if opts? and typeof opts == 'object'
|
51
|
+
params = []
|
52
|
+
keysCount = Object.keys(opts).length
|
53
|
+
if keysCount >= 1
|
54
|
+
params.push("#{k}=#{v}") for k,v of opts
|
55
|
+
url = url + '?'
|
56
|
+
url = url + params.join('&')
|
57
|
+
return(url)
|
58
|
+
else
|
59
|
+
''
|
60
|
+
|
61
|
+
|
48
62
|
@calledFunctions = []
|
49
63
|
|
50
64
|
# --- Ajax
|
data/lib/pinkman.rb
CHANGED
@@ -39,18 +39,28 @@ module Pinkman
|
|
39
39
|
|
40
40
|
# Active Record Relation: json_for
|
41
41
|
ActiveRecord::Relation.class_eval do
|
42
|
-
def json_for scope_name
|
43
|
-
|
44
|
-
|
42
|
+
def json_for scope_name, params_hash = {}
|
43
|
+
serialize_for(scope_name,params_hash).to_json
|
44
|
+
end
|
45
|
+
|
46
|
+
def serialize_for scope_name, params_hash = {}
|
47
|
+
options = {scope: scope_name}.merge(params: params_hash)
|
48
|
+
s = Pinkman::Serializer::array(self, options)
|
49
|
+
s
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
48
53
|
# Instance method: json_for
|
49
54
|
ActiveRecord::Base.class_eval do
|
50
|
-
def
|
51
|
-
|
52
|
-
|
55
|
+
def serialize_for scope_name, params_hash = {}
|
56
|
+
options = {scope: scope_name}.merge(params: params_hash)
|
57
|
+
self.class.serializer.new(self,options)
|
58
|
+
end
|
59
|
+
|
60
|
+
def json_for scope_name, params_hash={}
|
61
|
+
serialize_for(scope_name,params_hash).to_json
|
53
62
|
end
|
63
|
+
|
54
64
|
end
|
55
65
|
end
|
56
66
|
end
|
data/lib/pinkman/serializer.rb
CHANGED
@@ -2,5 +2,10 @@
|
|
2
2
|
|
3
3
|
module Pinkman
|
4
4
|
module Serializer
|
5
|
+
|
6
|
+
def self.array *args
|
7
|
+
args.first.map {|obj| obj.serialize_for(args[1][:scope],args[1][:params])} if args.first.methods.include?(:map) and args.length > 1 and args[1].is_a?(Hash) and args[1][:scope]
|
8
|
+
end
|
9
|
+
|
5
10
|
end
|
6
11
|
end
|
@@ -3,20 +3,33 @@ require_relative 'scope'
|
|
3
3
|
|
4
4
|
module Pinkman
|
5
5
|
module Serializer
|
6
|
+
|
6
7
|
class Base < ActiveModel::Serializer
|
7
|
-
|
8
|
+
|
9
|
+
def initialize *args
|
10
|
+
super(*args)
|
11
|
+
@params = OpenStruct.new(args[1][:params]) if args.length > 1 and args[1].is_a?(Hash) and args[1][:params]
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_accessor :params
|
8
16
|
|
9
17
|
self.root = false
|
10
18
|
|
11
19
|
def self.scope name=:all, &block
|
20
|
+
@scopes ||= {}
|
12
21
|
if block_given?
|
13
|
-
|
14
|
-
yield(
|
22
|
+
@scopes[name.to_sym] = Pinkman::Serializer::Scope.new(serializer: self)
|
23
|
+
yield(@scopes[name.to_sym])
|
15
24
|
else
|
16
|
-
|
25
|
+
@scopes[name.to_sym]
|
17
26
|
end
|
18
27
|
end
|
19
28
|
|
29
|
+
def self.scopes
|
30
|
+
@scopes
|
31
|
+
end
|
32
|
+
|
20
33
|
def self.model
|
21
34
|
@model || (begin eval(self.to_s.sub('Serializer','')) rescue nil end)
|
22
35
|
end
|
@@ -25,6 +38,32 @@ module Pinkman
|
|
25
38
|
@model = value
|
26
39
|
end
|
27
40
|
|
41
|
+
def self.has_many *args
|
42
|
+
args.each do |attribute|
|
43
|
+
self.class_eval do |c|
|
44
|
+
define_method attribute do
|
45
|
+
reflection = object.class.reflections[attribute.to_s]
|
46
|
+
if reflection
|
47
|
+
Pinkman::Serializer::Array.new(object.send(attribute), each_serializer: reflection.klass.serializer, scope: @scope)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.has_one *args
|
55
|
+
args.each do |attribute|
|
56
|
+
self.class_eval do |c|
|
57
|
+
define_method attribute do
|
58
|
+
reflection = object.class.reflections[attribute.to_s]
|
59
|
+
if reflection
|
60
|
+
reflection.klass.serializer.new(object.send(attribute), scope: @scope)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
28
67
|
def attributes *args
|
29
68
|
hash = super(*args)
|
30
69
|
if scope
|
@@ -9,15 +9,21 @@ module Pinkman
|
|
9
9
|
attr_accessor :read, :write, :access, :serializer
|
10
10
|
|
11
11
|
def read_attributes *args
|
12
|
-
self.read =
|
12
|
+
self.read = args
|
13
|
+
self.read = [] unless args.first
|
14
|
+
read
|
13
15
|
end
|
14
16
|
|
15
17
|
def write_attributes *args
|
16
|
-
self.write =
|
18
|
+
self.write = args
|
19
|
+
self.write = [] unless args.first
|
20
|
+
write
|
17
21
|
end
|
18
22
|
|
19
23
|
def access_actions *args
|
20
|
-
self.access =
|
24
|
+
self.access = args
|
25
|
+
self.access = [] unless args.first
|
26
|
+
access
|
21
27
|
end
|
22
28
|
|
23
29
|
def can_read? attribute
|
data/lib/pinkman/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pinkman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.1.
|
4
|
+
version: 0.9.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Agilso Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|