platanus 0.0.17 → 0.0.19
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/platanus/api_base.rb +10 -16
- data/lib/platanus/canned.rb +2 -2
- data/lib/platanus/http_helpers.rb +9 -17
- data/lib/platanus/stacked.rb +37 -28
- data/lib/platanus/version.rb +1 -1
- metadata +3 -2
data/README.md
CHANGED
data/lib/platanus/api_base.rb
CHANGED
@@ -20,24 +20,24 @@ module Platanus
|
|
20
20
|
base.rescue_from 'Exception' do |exc|
|
21
21
|
logger.error exc.message
|
22
22
|
logger.error exc.backtrace.join("\n")
|
23
|
-
api_respond_error(:internal_server_error, { type: exc.class.to_s, msg: exc.message } )
|
23
|
+
api_respond_error(:internal_server_error, { msg: 'server_error', exception: { type: exc.class.to_s, msg: exc.message } } )
|
24
24
|
end
|
25
25
|
|
26
26
|
base.rescue_from 'ActiveRecord::RecordNotFound' do |exc|
|
27
|
-
api_respond_error(:not_found)
|
27
|
+
api_respond_error(:not_found, { msg: 'record_not_found', detail: exc.message })
|
28
28
|
end
|
29
29
|
|
30
|
-
base.rescue_from '
|
31
|
-
api_respond_error(:
|
30
|
+
base.rescue_from 'ActiveModel::MassAssignmentSecurity::Error' do |exc|
|
31
|
+
api_respond_error(:bad_request, { msg: 'protected_attributes', detail: exc.message } )
|
32
32
|
end
|
33
33
|
|
34
|
-
base.rescue_from '
|
35
|
-
api_respond_error(:bad_request, {
|
34
|
+
base.rescue_from 'ActiveRecord::RecordInvalid' do |exc|
|
35
|
+
api_respond_error(:bad_request, { msg: 'invalid_attributes', errors: exc.record.errors } )
|
36
36
|
end
|
37
37
|
|
38
38
|
# Platanus error support (of course)
|
39
39
|
base.rescue_from 'Platanus::StatusError' do |exc|
|
40
|
-
api_respond_error(exc.status, exc)
|
40
|
+
api_respond_error(exc.status, { msg: exc.message })
|
41
41
|
end
|
42
42
|
|
43
43
|
base.extend ClassMethods
|
@@ -67,6 +67,9 @@ module Platanus
|
|
67
67
|
# # Enables cors
|
68
68
|
def enable_cors
|
69
69
|
# TODO: maybe cors support should be implemented as a middleware.
|
70
|
+
# response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type'
|
71
|
+
# response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,GET,HEAD,POST,PUT,DELETE'
|
72
|
+
# response.headers['Access-Control-Allow-Origin'] = '*'
|
70
73
|
end
|
71
74
|
|
72
75
|
end
|
@@ -77,7 +80,6 @@ module Platanus
|
|
77
80
|
def api_respond_empty(_options={})
|
78
81
|
_options[:json] = {}
|
79
82
|
api_respond_with(_options)
|
80
|
-
render_headers
|
81
83
|
end
|
82
84
|
|
83
85
|
# # Renders a regular response
|
@@ -92,7 +94,6 @@ module Platanus
|
|
92
94
|
respond_with(*_params) do |format|
|
93
95
|
format.json { render _options }
|
94
96
|
end
|
95
|
-
render_headers
|
96
97
|
end
|
97
98
|
|
98
99
|
# # Renders an error response
|
@@ -104,13 +105,6 @@ module Platanus
|
|
104
105
|
respond_with do |format|
|
105
106
|
format.json { render :status => _status, :json => _error_obj }
|
106
107
|
end
|
107
|
-
render_headers
|
108
|
-
end
|
109
|
-
|
110
|
-
def render_headers
|
111
|
-
response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type'
|
112
|
-
response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,GET,HEAD,POST,PUT,DELETE'
|
113
|
-
response.headers['Access-Control-Allow-Origin'] = '*'
|
114
108
|
end
|
115
109
|
|
116
110
|
end
|
data/lib/platanus/canned.rb
CHANGED
@@ -204,8 +204,8 @@ module Platanus
|
|
204
204
|
# This can optionally take an inherit parameter to use
|
205
205
|
# another profile as base for the new one.
|
206
206
|
def profile(_name, _options={})
|
207
|
-
inherit = _options.fetch(:inherits,nil)
|
208
|
-
def_test = _options.fetch(:default
|
207
|
+
inherit = _options.fetch(:inherits, nil)
|
208
|
+
def_test = _options.fetch(:default, :equals)
|
209
209
|
yield self.profiles[_name.to_s] = BProfile.new(self,self.profiles[inherit],def_test)
|
210
210
|
end
|
211
211
|
|
@@ -8,25 +8,20 @@ module Platanus
|
|
8
8
|
|
9
9
|
# Allows to use an exception driven scheme to notify
|
10
10
|
# the request that a especial status should be send
|
11
|
-
# back to the client.
|
11
|
+
# back to the client.
|
12
|
+
# Usefull when used with +api_base+ or +rescue_from+.
|
12
13
|
class StatusError < StandardError
|
13
14
|
|
14
15
|
class << self
|
15
16
|
instance_variable_set(:@status, nil)
|
16
|
-
instance_variable_set(:@msg, nil)
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.setup(_status
|
19
|
+
def self.setup(_status)
|
20
20
|
@status = _status
|
21
|
-
@msg = _msg
|
22
21
|
end
|
23
22
|
|
24
23
|
def self.status; @status; end
|
25
|
-
def self.message; @
|
26
|
-
|
27
|
-
def self.as_json(_options={})
|
28
|
-
{ 'status' => @status.to_s, 'msg' => @msg }
|
29
|
-
end
|
24
|
+
def self.message; "status_#{@status.to_s}"; end
|
30
25
|
|
31
26
|
def initialize(_msg=nil)
|
32
27
|
@msg = _msg
|
@@ -34,28 +29,25 @@ module Platanus
|
|
34
29
|
|
35
30
|
def status; self.class.status; end
|
36
31
|
def message; if @msg.nil? then self.class.message else @msg end; end
|
37
|
-
def as_json(_options={})
|
38
|
-
{ 'status' => status.to_s, 'msg' => message }
|
39
|
-
end
|
40
32
|
end
|
41
33
|
|
42
34
|
class StatusNotFound < StatusError
|
43
|
-
setup :not_found
|
35
|
+
setup :not_found
|
44
36
|
end
|
45
37
|
|
46
38
|
class StatusUnauthorized < StatusError
|
47
|
-
setup :unauthorized
|
39
|
+
setup :unauthorized
|
48
40
|
end
|
49
41
|
|
50
42
|
class StatusUnprocessable < StatusError
|
51
|
-
setup :unprocessable_entity
|
43
|
+
setup :unprocessable_entity
|
52
44
|
end
|
53
45
|
|
54
46
|
class StatusBadRequest < StatusError
|
55
|
-
setup :bad_request
|
47
|
+
setup :bad_request
|
56
48
|
end
|
57
49
|
|
58
50
|
class StatusConflict < StatusError
|
59
|
-
setup :conflict
|
51
|
+
setup :conflict
|
60
52
|
end
|
61
53
|
end
|
data/lib/platanus/stacked.rb
CHANGED
@@ -8,6 +8,8 @@ module Platanus
|
|
8
8
|
#
|
9
9
|
module StackedAttr
|
10
10
|
|
11
|
+
class NotSupportedError < StandardError; end
|
12
|
+
|
11
13
|
def self.included(base)
|
12
14
|
base.extend ClassMethods
|
13
15
|
end
|
@@ -18,70 +20,77 @@ module Platanus
|
|
18
20
|
def has_stacked(_name, _options={}, &block)
|
19
21
|
|
20
22
|
tname = _name.to_s
|
23
|
+
tname_single = tname.singularize
|
24
|
+
|
21
25
|
to_cache = _options.delete(:cached)
|
22
26
|
to_cache_prf = if _options[:cache_prf].nil? then 'last_' else _options.delete(:cache_prf) end
|
23
|
-
stack_key = if _options[:stack_key].nil? then 'created_at DESC, id DESC' else _options.delete(:stack_key) end
|
24
27
|
|
25
28
|
# Retrieve callbacks
|
26
29
|
before_push = _options.delete(:before_push)
|
27
30
|
after_push = _options.delete(:after_push)
|
28
31
|
|
29
|
-
_options[:
|
32
|
+
raise NotSupportedError.new('Only autosave mode is supported') if _options[:autosave] == false
|
33
|
+
|
34
|
+
_options[:order] = 'created_at DESC, id DESC'
|
30
35
|
_options[:limit] = 10 if _options[:limit].nil?
|
31
36
|
|
32
|
-
#
|
37
|
+
# Prepare cached attributes, generate read-only aliases without prefix.
|
33
38
|
unless to_cache.nil?
|
34
|
-
to_cache = to_cache.map
|
35
|
-
|
39
|
+
to_cache = to_cache.map do |name|
|
40
|
+
name = name.to_s
|
41
|
+
# attr_protected(to_cache_prf + name)
|
42
|
+
send :define_method, name do self.send(to_cache_prf + name) end
|
43
|
+
name
|
44
|
+
end
|
36
45
|
end
|
37
46
|
|
38
47
|
private
|
39
|
-
has_many
|
48
|
+
has_many _name, _options
|
40
49
|
public
|
41
50
|
|
42
|
-
send :define_method,
|
51
|
+
send :define_method, "push_#{tname_single}!" do |obj|
|
43
52
|
self.class.transaction do
|
44
53
|
|
45
|
-
#
|
54
|
+
# execute before callbacks
|
46
55
|
self.send(before_push, obj) unless before_push.nil?
|
47
56
|
block.call(self, obj) unless block.nil?
|
48
57
|
|
49
|
-
#
|
58
|
+
# cache required fields
|
50
59
|
unless to_cache.nil?
|
51
60
|
to_cache.each { |name| send(to_cache_prf + name + '=', obj.send(name)) }
|
52
61
|
end
|
53
62
|
|
54
|
-
#
|
55
|
-
self.save! if _options.fetch(:autosave, true) and (self.new_record? or self.changed?)
|
63
|
+
# push attribute, this will save the model if new.
|
56
64
|
raise ActiveRecord::RecordInvalid.new(obj) unless self.send(tname).send('<<',obj)
|
57
|
-
@_stacked_last = obj
|
58
65
|
|
59
|
-
#
|
66
|
+
# update inverse association if posible
|
67
|
+
if self.attributes.has_key? "top_#{tname_single}_id"
|
68
|
+
self["top_#{tname_single}_id"] = obj.id
|
69
|
+
else
|
70
|
+
@_stacked_last = obj
|
71
|
+
end
|
72
|
+
|
73
|
+
# execute after callback
|
60
74
|
self.send(after_push, obj) unless after_push.nil?
|
75
|
+
|
76
|
+
self.save! if self.changed? # Must save again, no other way...
|
61
77
|
end
|
62
78
|
end
|
63
79
|
|
64
|
-
send :define_method,
|
80
|
+
send :define_method, "push_#{tname_single}" do |obj|
|
65
81
|
begin
|
66
|
-
return self.send(
|
82
|
+
return self.send("push_#{tname_single}!", obj)
|
67
83
|
rescue ActiveRecord::RecordInvalid
|
68
84
|
return false
|
69
85
|
end
|
70
86
|
end
|
71
87
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
# Generate shorcut properties for cached attributes.
|
80
|
-
unless to_cache.nil?
|
81
|
-
to_cache.each do |name|
|
82
|
-
send :define_method, name.to_s do
|
83
|
-
self.send(to_cache_prf + name)
|
84
|
-
end
|
88
|
+
unless self.column_names.include? "top_#{tname_single}_id"
|
89
|
+
send :define_method, "top_#{tname_single}" do
|
90
|
+
# Storing the last stacked value will not prevent race conditions
|
91
|
+
# when simultaneous updates occur.
|
92
|
+
return @_stacked_last unless @_stacked_last.nil?
|
93
|
+
@_stacked_last = self.send(_name).first
|
85
94
|
end
|
86
95
|
end
|
87
96
|
end
|
data/lib/platanus/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: platanus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.19
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Platan.us utility gem
|
15
15
|
email:
|
@@ -70,3 +70,4 @@ summary: This gem contains various ruby classes used by Platanus in our rails pr
|
|
70
70
|
test_files:
|
71
71
|
- spec/canned_spec.rb
|
72
72
|
- spec/spec_helper.rb
|
73
|
+
has_rdoc:
|