softwear-lib 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/softwear/lib/api_controller.rb +88 -85
- data/lib/softwear/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18852132ef59f403e4bf39ccbfb9924d26aa7a1b
|
4
|
+
data.tar.gz: 25a246735e20b6e342b804955844d0303daca545
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 276834739a66871a69a863e85dcb6529f900d5c58d409ee5bd5b874f7fe13e92d31c4f9f28546906baa02de865c41103f1ea1c50df112a66f5178624e885a331
|
7
|
+
data.tar.gz: 80e35e5017f1343c848ca76a650dadbaf890da1116ca890719d802b46806756c8d8938b7248e39e4c72cabef0a8afa93ed5f63f1297c2195dd384167db5c3a82
|
@@ -1,115 +1,118 @@
|
|
1
|
-
|
2
|
-
class ApiController < ActionController::Base
|
3
|
-
include InheritedResources::Actions
|
4
|
-
include InheritedResources::BaseHelpers
|
5
|
-
extend InheritedResources::ClassMethods
|
6
|
-
extend InheritedResources::UrlHelpers
|
1
|
+
require 'inherited_resources'
|
7
2
|
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module Softwear
|
4
|
+
module Lib
|
5
|
+
class ApiController < ActionController::Base
|
6
|
+
include ::InheritedResources::Actions
|
7
|
+
include ::InheritedResources::BaseHelpers
|
8
|
+
extend ::InheritedResources::ClassMethods
|
9
|
+
extend ::InheritedResources::UrlHelpers
|
11
10
|
|
12
|
-
|
11
|
+
skip_before_filter :authenticate_user!
|
12
|
+
skip_before_filter :verify_authenticity_token
|
13
13
|
|
14
|
-
|
15
|
-
self.responder = InheritedResources::Responder
|
14
|
+
before_filter :allow_origin
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
respond_to :json
|
17
|
+
self.responder = InheritedResources::Responder
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
self.class_attribute :resource_class, :instance_writer => false unless self.respond_to? :resource_class
|
20
|
+
self.class_attribute :parents_symbols, :resources_configuration, :instance_writer => false
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
.compact
|
22
|
+
def index(&block)
|
23
|
+
yield if block_given?
|
27
24
|
|
28
|
-
|
29
|
-
|
25
|
+
key_values = permitted_attributes.map do |a|
|
26
|
+
[a, params[a]] if params.key?(a)
|
27
|
+
end
|
28
|
+
.compact
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
30
|
+
self.records ||= resource_class
|
31
|
+
self.records = records.where(Hash[key_values])
|
35
32
|
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
respond_to do |format|
|
34
|
+
format.json(&render_json(records))
|
35
|
+
end
|
39
36
|
end
|
40
|
-
end
|
41
37
|
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
def show
|
39
|
+
super do |format|
|
40
|
+
format.json(&render_json)
|
41
|
+
end
|
45
42
|
end
|
46
|
-
end
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
headers['Location'] = resource_url(record.id)
|
52
|
-
render_json(status: 201).call
|
44
|
+
def update
|
45
|
+
super do |format|
|
46
|
+
format.json(&render_json)
|
53
47
|
end
|
54
|
-
failure.json(&render_json)
|
55
48
|
end
|
56
|
-
end
|
57
49
|
|
58
|
-
|
59
|
-
|
60
|
-
|
50
|
+
def create
|
51
|
+
create! do |success, failure|
|
52
|
+
success.json do
|
53
|
+
headers['Location'] = resource_url(record.id)
|
54
|
+
render_json(status: 201).call
|
55
|
+
end
|
56
|
+
failure.json(&render_json)
|
57
|
+
end
|
58
|
+
end
|
61
59
|
|
62
|
-
|
60
|
+
def options
|
61
|
+
head(:ok) if request.request_method == 'OPTIONS'
|
62
|
+
end
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
64
|
+
protected
|
65
|
+
|
66
|
+
def render_json(options = {})
|
67
|
+
proc do
|
68
|
+
if options.is_a?(Hash)
|
69
|
+
records = nil
|
70
|
+
else
|
71
|
+
records = options
|
72
|
+
options = {}
|
73
|
+
end
|
74
|
+
rendering = {
|
75
|
+
json: (records || record),
|
76
|
+
methods: [:id] + permitted_attributes,
|
77
|
+
include: includes
|
78
|
+
}
|
79
|
+
.merge(options)
|
80
|
+
|
81
|
+
render rendering
|
71
82
|
end
|
72
|
-
rendering = {
|
73
|
-
json: (records || record),
|
74
|
-
methods: [:id] + permitted_attributes,
|
75
|
-
include: includes
|
76
|
-
}
|
77
|
-
.merge(options)
|
78
|
-
|
79
|
-
render rendering
|
80
83
|
end
|
81
|
-
end
|
82
84
|
|
83
|
-
|
84
|
-
|
85
|
-
|
85
|
+
def self.model_name
|
86
|
+
name.gsub('Api::', '').gsub('Controller', '').singularize
|
87
|
+
end
|
86
88
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
89
|
+
# Override this to specify the :include option of rendering json.
|
90
|
+
def includes
|
91
|
+
{}
|
92
|
+
end
|
91
93
|
|
92
|
-
|
93
|
-
|
94
|
-
|
94
|
+
def records
|
95
|
+
instance_variable_get("@#{self.class.model_name.underscore.pluralize}")
|
96
|
+
end
|
95
97
|
|
96
|
-
|
97
|
-
|
98
|
-
|
98
|
+
def records=(r)
|
99
|
+
instance_variable_set("@#{self.class.model_name.underscore.pluralize}", r)
|
100
|
+
end
|
99
101
|
|
100
|
-
|
101
|
-
|
102
|
-
|
102
|
+
def record
|
103
|
+
instance_variable_get("@#{self.class.model_name.underscore}")
|
104
|
+
end
|
103
105
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
106
|
+
def allow_origin
|
107
|
+
headers['Access-Control-Allow-Origin'] = '*'
|
108
|
+
headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, PATCH, DELETE'
|
109
|
+
headers['Access-Control-Allow-Headers'] = 'X-Requested-With'
|
110
|
+
headers['Access-Control-Allow-Credentials'] = 'true'
|
111
|
+
end
|
110
112
|
|
111
|
-
|
112
|
-
|
113
|
+
def permitted_attributes
|
114
|
+
resource_class.column_names
|
115
|
+
end
|
113
116
|
end
|
114
117
|
end
|
115
118
|
end
|
data/lib/softwear/lib/version.rb
CHANGED