pinkman 0.9.1.3 → 0.9.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f071ffd276a4ffce048f6c50bf45867239a60193
4
- data.tar.gz: 7a745e4dfa2cf9d6011b3ef43a8fb694fbddb273
3
+ metadata.gz: 399b3c9f42d0fcb24b807a240ad12aa788db9331
4
+ data.tar.gz: df42a3182412ac46d3f9b9dc5ca7500e8b82f719
5
5
  SHA512:
6
- metadata.gz: 32ec6ef56b0fb4806b223ec8f157e1effb190db6e6082b0b9955ca6d179cb41b16077d6468a6a49d4a646c384246c49af68fc00677cf0a3587c523b2f9f0a48e
7
- data.tar.gz: f8b506709206275f684f6a235339a9f21e7ddf1cf00c7b7c37faa27c02b2e573b6b8282848a7e232f17c01df7e35891b355e07bb3c44eaa3ad3ed29b0f68af91
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(@api() + "#{action}/#{id}", callback)
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 + "?limit=#{limit}&offset=#{offset}"
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 url: @fetchingFrom, limit: n, offset: @count(), callback: callback
380
+ @fetchFromUrl
381
+ url: @fetchingFrom
382
+ params:
383
+ limit: n
384
+ offset: @count()
385
+ callback: callback
382
386
  else
383
- @fetchFromUrl url: @api(), limit: n, offset: @count(), callback: callback
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() + "search?query=#{query}", callback: callback }
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
@@ -1,2 +1,10 @@
1
1
  class ApiController < ApplicationController
2
+
3
+ before_action :default_limit_and_offset
4
+
5
+ def default_limit_and_offset
6
+ params[:limit] = 20 if params[:limit].blank?
7
+ params[:offset] = 0 if params[:offset].blank?
8
+ end
9
+
2
10
  end
@@ -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
- a = ActiveModel::ArraySerializer.new(self, each_serializer: model.serializer, scope: scope_name)
44
- a.to_json
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 json_for scope_name
51
- s = self.class.serializer.new(self,scope: scope_name)
52
- s.to_json
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
@@ -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
- @@scopes = {}
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
- @@scopes[name.to_sym] ||= Pinkman::Serializer::Scope.new(serializer: self)
14
- yield(@@scopes[name.to_sym])
22
+ @scopes[name.to_sym] = Pinkman::Serializer::Scope.new(serializer: self)
23
+ yield(@scopes[name.to_sym])
15
24
  else
16
- @@scopes[name.to_sym]
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 = (args.size > 0 and args.first) ? args : []
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 = (args.size > 0 and args.first) ? args : []
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 = (args.size > 0 and args.first) ? args : []
24
+ self.access = args
25
+ self.access = [] unless args.first
26
+ access
21
27
  end
22
28
 
23
29
  def can_read? attribute
@@ -1,3 +1,3 @@
1
1
  module Pinkman
2
- VERSION = "0.9.1.3"
2
+ VERSION = "0.9.1.4"
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.3
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-24 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler