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