pinkman 0.9.1.17 → 0.9.1.19

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: 5525b26d518f68b286579cb99d4911f9f73bafd1
4
- data.tar.gz: d09bd9ac3f50b34ac2c66266ef19e46350244941
3
+ metadata.gz: 7de72d804d1e06c2f17e2a96570065b2adfd7965
4
+ data.tar.gz: 5fe83b7dd531d17bd1ec517f2dd3b3b88bd5e566
5
5
  SHA512:
6
- metadata.gz: feb3528b7dc0b952aa88dac2a6b7818aefc6c15dda03d58a66aabdb143a614a1211c88bcb596562eabdc360bc8329d9c6cc7cdb693d6d27c9ebe31eaa79c6a61
7
- data.tar.gz: de515045378836182a228bf1045f9e48ab9f8d7a8c8c89740d46004c95725f8184a69cd5b4959f6cfc5b47c1226aa80c5c0a9f30d4fe65e4a2648f682fde689e
6
+ metadata.gz: 8f0dd6d62a3b6bc328c89625918de98594a91ad0f2713b5ba31c5a5cd28385e9d20d8047ff23d2f3f417dde70649d053c8fb315300b78c8d0e2f22e665d3103e
7
+ data.tar.gz: cda447526cf265a335e8c800dfa49f53d8838976b995a2a354a394ddfdce9e4f92a2e32d91fe1712d3eeb64c01060c4deb1c708a0d8c8fbf7b10304a1de985fb
@@ -358,22 +358,33 @@ class window.PinkmanCollection extends window.PinkmanCommon
358
358
  # request: get /api/API_URL/
359
359
  fetchFromUrl: (options) ->
360
360
  if options? and typeof options == 'object' and options.url?
361
- @emptyResponse = null
361
+
362
+ if Pinkman.hasScope(this)
363
+ options.params = new Object unless options.params?
364
+ options.params.scope = Pinkman.scope(this)
365
+
366
+ @doneFetching = null
362
367
  @fetchingFrom = options.url
368
+
363
369
  Pinkman.ajax.get
364
370
  url: Pinkman.json2url(options.url,options.params)
365
371
  complete: (response) =>
366
372
  if response?
367
373
  [@errors, @error] = [response.errors, response.error] if response.errors? or response.error?
368
374
 
375
+ if options.params? and options.params.limit?
376
+ @doneFetching = response.length < options.params.limit
377
+ else
378
+ @doneFetching = response.length == 0
379
+
369
380
  # separate recent when this collection is already populated
370
- if @any()
371
- @_recent = new this.constructor
372
- @_recent.fetchFromArray(response) if response.length > 0
381
+ @_recent = new this.constructor
373
382
 
374
- @fetchFromArray(response).emptyResponse = response.length == 0
383
+ if response.length > 0
384
+ @_recent.fetchFromArray(response)
385
+ @fetchFromArray(response)
375
386
 
376
- @emptyResponse = true unless response?
387
+ @doneFetching = true unless response?
377
388
  options.callback(this) if options.callback? and typeof options.callback == 'function'
378
389
  return(this)
379
390
 
@@ -381,7 +392,7 @@ class window.PinkmanCollection extends window.PinkmanCommon
381
392
  # request: get /api/API_URL/?offset="COLLECTION_SIZE"&limit=n
382
393
  # in rails: api::controller#index
383
394
  fetchMore: (n=10,callback='') ->
384
- unless @emptyResponse
395
+ unless @doneFetching
385
396
  if @fetchingFrom?
386
397
  @fetchFromUrl
387
398
  url: @fetchingFrom
@@ -118,8 +118,14 @@ class window.PinkmanObject extends window.PinkmanCommon
118
118
 
119
119
  @find: (id, callback='') ->
120
120
  obj = new this
121
+ params = new Object
122
+ params.scope = Pinkman.scope(this)
123
+
124
+ console.log params
125
+ console.log Pinkman.json2url(obj.api() + id,params)
126
+
121
127
  Pinkman.ajax.get
122
- url: obj.api() + id,
128
+ url: Pinkman.json2url(obj.api() + id,params)
123
129
  complete: (response) ->
124
130
  obj.assign(response)
125
131
  if obj.error? or obj.errors?
@@ -133,7 +139,7 @@ class window.PinkmanObject extends window.PinkmanCommon
133
139
  unless @id?
134
140
  Pinkman.ajax.post
135
141
  url: @api()
136
- data: { pink_obj: @json() }
142
+ data: { pink_obj: @json(), scope: Pinkman.scope(this) }
137
143
  complete: (response) =>
138
144
  @assign(response)
139
145
  delete @errors unless response.errors?
@@ -144,7 +150,7 @@ class window.PinkmanObject extends window.PinkmanCommon
144
150
  if @id?
145
151
  Pinkman.ajax.patch
146
152
  url: @api() + @id
147
- data: { pink_obj: @json() }
153
+ data: { pink_obj: @json(), scope: Pinkman.scope(this) }
148
154
  complete: (response) =>
149
155
  @assign(response)
150
156
  delete @errors unless response.errors?
@@ -152,8 +158,11 @@ class window.PinkmanObject extends window.PinkmanCommon
152
158
  return(this)
153
159
 
154
160
  reload: (callback) ->
161
+ params = new Object
162
+ params.scope = Pinkman.scope(this)
163
+
155
164
  Pinkman.ajax.get
156
- url: @api() + @id,
165
+ url: Pinkman.json2url(@api() + @id,params)
157
166
  complete: (response) =>
158
167
  @assign(response)
159
168
  if @error? or @errors?
@@ -61,6 +61,40 @@ class window.Pinkman
61
61
 
62
62
  @calledFunctions = []
63
63
 
64
+ # --- Scope
65
+
66
+ @scope: (obj) ->
67
+ if obj?
68
+ scope = @getSelfScope(obj)
69
+ scope = @getClassScope(obj) unless scope?
70
+ scope = @getAppScope(obj) unless scope?
71
+ scope
72
+
73
+ @hasScope: (obj) ->
74
+ obj? and (@hasSelfScope(obj) or @hasClassScope(obj) or @hasAppScope(obj))
75
+
76
+ @hasSelfScope: (obj) ->
77
+ obj? and obj.scope? and obj.scope != ''
78
+
79
+ @hasClassScope: (obj) ->
80
+ obj? and obj.constructor.scope? and obj.constructor.scope != ''
81
+
82
+ @hasAppScope: (obj) ->
83
+ obj? and ((obj.pinkmanType == 'collection' and AppCollection? and AppCollection.scope? and AppCollection != '') or (obj.pinkmanType == 'object' and AppObject? and AppObject.scope? and AppObject.scope != ''))
84
+
85
+ @getSelfScope: (obj) ->
86
+ obj.scope if @hasSelfScope(obj)
87
+
88
+ @getClassScope: (obj) ->
89
+ obj.constructor.scope if @hasClassScope(obj)
90
+
91
+ @getAppScope: (obj) ->
92
+ if typeof obj == 'object' and obj.pinkmanType == 'collection' and AppCollection? and AppCollection.scope?
93
+ AppCollection.scope
94
+ else if typeof obj == 'object' and obj.pinkmanType == 'object' and AppObject? and AppObject.scope?
95
+ AppObject.scope
96
+
97
+
64
98
  # --- Ajax
65
99
 
66
100
  @ajax:
@@ -7,14 +7,50 @@ class ApiController < ApplicationController
7
7
  params[:offset] = 0 if params[:offset].blank?
8
8
  end
9
9
 
10
- # To do: define the current scope the way you want
11
- # Example:
10
+ # TO DO: rewrite the current_scope method the way you want.
11
+
12
+ # You can define it according to the current user and his permissions
13
+ # or through params[:scope] or any other way you want.
14
+ # It's your choice.
15
+
16
+ # See some common examples and important securities notes below.
17
+
18
+ # Example 1:
12
19
  # def current_scope
13
20
  # current_user.admin? ? :admin : :public
21
+ # end
22
+
23
+ # Example 2:
24
+ # def current_scope
25
+ # params[:scope] == 'public' ? :public : (current_user.admin? ? :admin : :public)
26
+ # end
27
+
28
+ # Example 3:
29
+ # def current_scope
30
+ # your_scope_verification_method?(params[:scope]) ? params[:scope].to_sym : :public
14
31
  # end
32
+
33
+ # ***** IMPORTANT **********************************************************************************
34
+ # * *
35
+ # * params[:scope] must to be whitelisted if you are going to allow/use it. *
36
+ # * *
37
+ # **************************************************************************************************
38
+
39
+ # You can set params[:scope] value in the client (js/coffee).
40
+
41
+ # 1. In a single object/collection
42
+ # obj = new Pinkman.object; obj.set('scope','public')
43
+
44
+ # 2. In all instances of a given model (object or collection)
45
+ # class YourModel extends Pinkman.object
46
+ # YourModel.scope = 'public'
47
+
48
+ # 3. In all instances through AppObject and AppCollection
49
+ # AppObject.scope = 'public'
50
+ # AppCollection.scope = 'public'
51
+
15
52
  def current_scope
16
53
  :public
17
54
  end
18
-
19
55
 
20
56
  end
@@ -1,3 +1,3 @@
1
1
  module Pinkman
2
- VERSION = "0.9.1.17"
2
+ VERSION = "0.9.1.19"
3
3
  end
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.17
4
+ version: 0.9.1.19
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-05-16 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler