horse_power 0.1.6 → 0.2.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74a9429a6b6ff1e06abc3f1010ca6bba229aea53
|
4
|
+
data.tar.gz: 1cc768cbd9db5e3a58bac42cb2c32e5dada92ae0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8081e40f69ec18e216eab7f59677c3b085618330de26f29af7ccae15b6d5f175ab1ef4550b1f86fafce7dfd76129c716bbed88d70aedecfc601164096543974c
|
7
|
+
data.tar.gz: be50322fe5a2cfbb50edb86f3981a6cdb96a0c2d8d8200ce784fbe4509abeca196b91d66d1b0e87905c055bb03582bbe1ac71ecb348353c245202ac9d5229c56
|
@@ -3,6 +3,8 @@ require 'authorization'
|
|
3
3
|
|
4
4
|
class Api::V<%= api_version %>::<%= HorsePower.get_camel_plural(resource_name) %>Controller < Api::V<%= api_version %>::ApplicationController
|
5
5
|
before_action :set_<%= HorsePower.get_singular(resource_name) %>, only: [:show, :update, :destroy]
|
6
|
+
before_action :set_<%= HorsePower.get_plural(resource_name) %>, only: [:updateAll]
|
7
|
+
before_action :set_<%= HorsePower.get_plural(resource_name) %>_from_ids, only: [:showAll, :destroyAll]
|
6
8
|
before_action :index_authorize, only: [:index]
|
7
9
|
before_action :show_authorize, only: [:show]
|
8
10
|
before_action :create_authorize, only: [:create]
|
@@ -20,6 +22,22 @@ class Api::V<%= api_version %>::<%= HorsePower.get_camel_plural(resource_name) %
|
|
20
22
|
render json: @<%= HorsePower.get_singular(resource_name) %>, serializer: ::V<%= api_version %>::<%= HorsePower.get_camel(resource_name) %>Serializer
|
21
23
|
end
|
22
24
|
|
25
|
+
# POST /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/showAll
|
26
|
+
def showAll
|
27
|
+
all_<%= HorsePower.get_plural(resource_name) %> = [];
|
28
|
+
ActiveRecord::Base.transaction do
|
29
|
+
@<%= HorsePower.get_plural(resource_name) %>.each do |<%= HorsePower.get_singular(resource_name) %>|
|
30
|
+
if !::Authorization::V<%= api_version %>::<%= HorsePower.get_camel(resource_name) %>.show?(<%= HorsePower.get_singular(resource_name) %>,current_user)
|
31
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
32
|
+
raise ActiveRecord::Rollback
|
33
|
+
return
|
34
|
+
end
|
35
|
+
all_<%= HorsePower.get_plural(resource_name) %>.push(<%= HorsePower.get_singular(resource_name) %>)
|
36
|
+
end
|
37
|
+
render json: all_<%= HorsePower.get_plural(resource_name) %>, each_serializer: ::V<%= api_version %>::<%= HorsePower.get_camel(resource_name) %>Serializer
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
23
41
|
# POST /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>
|
24
42
|
def create
|
25
43
|
@<%= HorsePower.get_singular(resource_name) %> = ::<%= HorsePower.get_camel(resource_name) %>.new(<%= HorsePower.get_singular(resource_name) %>_params)
|
@@ -39,12 +57,43 @@ class Api::V<%= api_version %>::<%= HorsePower.get_camel_plural(resource_name) %
|
|
39
57
|
end
|
40
58
|
end
|
41
59
|
|
60
|
+
# POST /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/updateAll
|
61
|
+
def updateAll
|
62
|
+
all_<%= HorsePower.get_plural(resource_name) %> = [];
|
63
|
+
ActiveRecord::Base.transaction do
|
64
|
+
@<%= HorsePower.get_plural(resource_name) %>.each do |json_<%= HorsePower.get_singular(resource_name) %>|
|
65
|
+
<%= HorsePower.get_singular(resource_name) %> = updateAllLogic(json_<%= HorsePower.get_singular(resource_name) %>)
|
66
|
+
if !<%= HorsePower.get_singular(resource_name) %>.nil?
|
67
|
+
all_<%= HorsePower.get_plural(resource_name) %>.push(<%= HorsePower.get_singular(resource_name) %>)
|
68
|
+
else
|
69
|
+
return
|
70
|
+
end
|
71
|
+
end
|
72
|
+
render json: all_<%= HorsePower.get_plural(resource_name) %>, each_serializer: ::V<%= api_version %>::<%= HorsePower.get_camel(resource_name) %>Serializer
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
42
76
|
# DELETE /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1
|
43
77
|
def destroy
|
44
78
|
@<%= HorsePower.get_singular(resource_name) %>.destroy
|
45
79
|
render json: {}
|
46
80
|
end
|
47
81
|
|
82
|
+
# POST /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/destroyAll
|
83
|
+
def destroyAll
|
84
|
+
ActiveRecord::Base.transaction do
|
85
|
+
@<%= HorsePower.get_plural(resource_name) %>.each do |<%= HorsePower.get_singular(resource_name) %>|
|
86
|
+
if !::Authorization::V<%= api_version %>::<%= HorsePower.get_camel(resource_name) %>.destroy?(<%= HorsePower.get_singular(resource_name) %>,current_user)
|
87
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
88
|
+
raise ActiveRecord::Rollback
|
89
|
+
return
|
90
|
+
end
|
91
|
+
<%= HorsePower.get_singular(resource_name) %>.destroy
|
92
|
+
end
|
93
|
+
render json: {}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
48
97
|
private
|
49
98
|
|
50
99
|
# Use callbacks to share common setup or constraints between actions.
|
@@ -56,12 +105,65 @@ class Api::V<%= api_version %>::<%= HorsePower.get_camel_plural(resource_name) %
|
|
56
105
|
end
|
57
106
|
end
|
58
107
|
|
108
|
+
def set_<%= HorsePower.get_plural(resource_name) %>
|
109
|
+
@<%= HorsePower.get_plural(resource_name) %> = params[:<%= HorsePower.get_plural(resource_name) %>]
|
110
|
+
if @<%= HorsePower.get_plural(resource_name) %>.nil?
|
111
|
+
render :json => {errors: "<%= HorsePower.get_camel_plural(resource_name) %> were not found"}, status: :not_found
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def set_<%= HorsePower.get_plural(resource_name) %>_from_ids
|
116
|
+
ids = params[:ids]
|
117
|
+
if ids.nil?
|
118
|
+
render :json => {errors: "Ids were not found"}, status: :not_found
|
119
|
+
end
|
120
|
+
@<%= HorsePower.get_plural(resource_name) %> = []
|
121
|
+
ActiveRecord::Base.transaction do
|
122
|
+
ids.each do |id|
|
123
|
+
<%= HorsePower.get_singular(resource_name) %> = ::<%= HorsePower.get_camel(resource_name) %>.find_by_id(id)
|
124
|
+
if !<%= HorsePower.get_singular(resource_name) %>.nil?
|
125
|
+
@<%= HorsePower.get_plural(resource_name) %>.push(<%= HorsePower.get_singular(resource_name) %>)
|
126
|
+
else
|
127
|
+
render :json => {errors: "<%= HorsePower.get_camel(resource_name) %> with id #{id} was not found"}, status: :not_found
|
128
|
+
raise ActiveRecord::Rollback
|
129
|
+
return
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
59
135
|
# Only allow a trusted parameter "white list" through.
|
60
136
|
|
61
137
|
def <%= HorsePower.get_singular(resource_name) %>_params
|
62
138
|
params.require(:<%= HorsePower.get_singular(resource_name) %>).permit(<%= HorsePower.params_list(attributes) %>)
|
63
139
|
end
|
64
140
|
|
141
|
+
def <%= HorsePower.get_singular(resource_name) %>_values(json_<%= HorsePower.get_singular(resource_name) %>)
|
142
|
+
hash = ActionController::Parameters.new({<%= HorsePower.get_singular(resource_name) %>: json_<%= HorsePower.get_singular(resource_name) %>})
|
143
|
+
hash.require(:<%= HorsePower.get_singular(resource_name) %>).permit(<%= HorsePower.params_list(attributes) %>)
|
144
|
+
end
|
145
|
+
|
146
|
+
# Batch logic
|
147
|
+
|
148
|
+
def updateAllLogic(json_<%= HorsePower.get_singular(resource_name) %>)
|
149
|
+
<%= HorsePower.get_singular(resource_name) %> = ::<%= HorsePower.get_camel(resource_name) %>.find_by_id(json_<%= HorsePower.get_singular(resource_name) %>[:id])
|
150
|
+
if <%= HorsePower.get_singular(resource_name) %>.nil?
|
151
|
+
render :json => {errors: "<%= HorsePower.get_camel(resource_name) %> with id #{json_<%= HorsePower.get_singular(resource_name) %>[:id]} was not found"}, status: :not_found
|
152
|
+
return nil
|
153
|
+
end
|
154
|
+
if !::Authorization::V<%= api_version %>::<%= HorsePower.get_camel(resource_name) %>.update?(<%= HorsePower.get_singular(resource_name) %>,<%= HorsePower.get_singular(resource_name) %>_values(json_<%= HorsePower.get_singular(resource_name) %>),current_user)
|
155
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
156
|
+
return nil
|
157
|
+
end
|
158
|
+
if <%= HorsePower.get_singular(resource_name) %>.update(<%= HorsePower.get_singular(resource_name) %>_values(json_<%= HorsePower.get_singular(resource_name) %>))
|
159
|
+
return <%= HorsePower.get_singular(resource_name) %>
|
160
|
+
else
|
161
|
+
render :json => {errors: <%= HorsePower.get_singular(resource_name) %>.errors.full_messages}, status: :unprocessable_entity
|
162
|
+
raise ActiveRecord::Rollback
|
163
|
+
return nil
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
65
167
|
# Authorizations below here
|
66
168
|
|
67
169
|
def index_authorize
|
@@ -47,7 +47,13 @@ module HorsePower
|
|
47
47
|
# Expects the routes to already be setup
|
48
48
|
gsub_file 'config/routes.rb',
|
49
49
|
"scope \'#{api_version}\' do\n",
|
50
|
-
"scope \'#{api_version}\' do\n\t\t\tresources :#{HorsePower.get_plural(resource_name)}, except: [:new, :edit], controller: \'api/v#{api_version}/#{HorsePower.get_plural(resource_name)}\'
|
50
|
+
"scope \'#{api_version}\' do\n\t\t\tresources :#{HorsePower.get_plural(resource_name)}, except: [:new, :edit], controller: \'api/v#{api_version}/#{HorsePower.get_plural(resource_name)}\' do
|
51
|
+
collection do
|
52
|
+
post \'showAll\', to: \"api/v1/#{HorsePower.get_plural(resource_name)}#showAll\"
|
53
|
+
post \'updateAll\', to: \"api/v1/#{HorsePower.get_plural(resource_name)}#updateAll\"
|
54
|
+
post \'destroyAll\', to: \"api/v1/#{HorsePower.get_plural(resource_name)}#destroyAll\"
|
55
|
+
end
|
56
|
+
end\n"
|
51
57
|
end
|
52
58
|
|
53
59
|
end
|
data/lib/horse_power/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: horse_power
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cleophus Robinson IV
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|