api_maker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +476 -0
- data/Rakefile +27 -0
- data/app/channels/api_maker/subscriptions_channel.rb +80 -0
- data/app/controllers/api_maker/base_controller.rb +32 -0
- data/app/controllers/api_maker/commands_controller.rb +26 -0
- data/app/controllers/api_maker/devise_controller.rb +60 -0
- data/app/controllers/api_maker/session_statuses_controller.rb +33 -0
- data/app/services/api_maker/application_service.rb +7 -0
- data/app/services/api_maker/collection_command_service.rb +24 -0
- data/app/services/api_maker/command_response.rb +67 -0
- data/app/services/api_maker/command_service.rb +31 -0
- data/app/services/api_maker/create_command.rb +62 -0
- data/app/services/api_maker/create_command_service.rb +18 -0
- data/app/services/api_maker/destroy_command.rb +39 -0
- data/app/services/api_maker/destroy_command_service.rb +22 -0
- data/app/services/api_maker/generate_react_native_api_service.rb +61 -0
- data/app/services/api_maker/index_command.rb +96 -0
- data/app/services/api_maker/index_command_service.rb +22 -0
- data/app/services/api_maker/js_method_namer_service.rb +11 -0
- data/app/services/api_maker/member_command_service.rb +25 -0
- data/app/services/api_maker/model_content_generator_service.rb +108 -0
- data/app/services/api_maker/models_finder_service.rb +22 -0
- data/app/services/api_maker/models_generator_service.rb +104 -0
- data/app/services/api_maker/update_command.rb +43 -0
- data/app/services/api_maker/update_command_service.rb +21 -0
- data/app/services/api_maker/valid_command.rb +35 -0
- data/app/services/api_maker/valid_command_service.rb +21 -0
- data/app/views/api_maker/_data.html.erb +15 -0
- data/config/rails_best_practices.yml +55 -0
- data/config/routes.rb +7 -0
- data/lib/api_maker.rb +36 -0
- data/lib/api_maker/ability.rb +39 -0
- data/lib/api_maker/ability_loader.rb +21 -0
- data/lib/api_maker/action_controller_base_extensions.rb +5 -0
- data/lib/api_maker/base_command.rb +81 -0
- data/lib/api_maker/base_resource.rb +78 -0
- data/lib/api_maker/collection_serializer.rb +69 -0
- data/lib/api_maker/command_spec_helper.rb +57 -0
- data/lib/api_maker/configuration.rb +34 -0
- data/lib/api_maker/engine.rb +5 -0
- data/lib/api_maker/individual_command.rb +37 -0
- data/lib/api_maker/javascript/api.js +92 -0
- data/lib/api_maker/javascript/base-model.js +543 -0
- data/lib/api_maker/javascript/bootstrap/attribute-row.jsx +16 -0
- data/lib/api_maker/javascript/bootstrap/attribute-rows.jsx +47 -0
- data/lib/api_maker/javascript/bootstrap/card.jsx +79 -0
- data/lib/api_maker/javascript/bootstrap/checkbox.jsx +127 -0
- data/lib/api_maker/javascript/bootstrap/checkboxes.jsx +105 -0
- data/lib/api_maker/javascript/bootstrap/live-table.jsx +168 -0
- data/lib/api_maker/javascript/bootstrap/money-input.jsx +136 -0
- data/lib/api_maker/javascript/bootstrap/radio-buttons.jsx +80 -0
- data/lib/api_maker/javascript/bootstrap/select.jsx +168 -0
- data/lib/api_maker/javascript/bootstrap/string-input.jsx +203 -0
- data/lib/api_maker/javascript/cable-connection-pool.js +169 -0
- data/lib/api_maker/javascript/cable-subscription-pool.js +111 -0
- data/lib/api_maker/javascript/cable-subscription.js +33 -0
- data/lib/api_maker/javascript/collection.js +186 -0
- data/lib/api_maker/javascript/commands-pool.js +123 -0
- data/lib/api_maker/javascript/custom-error.js +14 -0
- data/lib/api_maker/javascript/deserializer.js +35 -0
- data/lib/api_maker/javascript/devise.js.erb +113 -0
- data/lib/api_maker/javascript/error-logger.js +119 -0
- data/lib/api_maker/javascript/event-connection.jsx +24 -0
- data/lib/api_maker/javascript/event-created.jsx +26 -0
- data/lib/api_maker/javascript/event-destroyed.jsx +26 -0
- data/lib/api_maker/javascript/event-emitter-listener.jsx +32 -0
- data/lib/api_maker/javascript/event-listener.jsx +41 -0
- data/lib/api_maker/javascript/event-updated.jsx +26 -0
- data/lib/api_maker/javascript/form-data-to-object.js +70 -0
- data/lib/api_maker/javascript/included.js +39 -0
- data/lib/api_maker/javascript/key-value-store.js +47 -0
- data/lib/api_maker/javascript/logger.js +23 -0
- data/lib/api_maker/javascript/model-name.js +21 -0
- data/lib/api_maker/javascript/model-template.js.erb +110 -0
- data/lib/api_maker/javascript/models-response-reader.js +43 -0
- data/lib/api_maker/javascript/paginate.jsx +128 -0
- data/lib/api_maker/javascript/params.js +68 -0
- data/lib/api_maker/javascript/resource-route.jsx +75 -0
- data/lib/api_maker/javascript/resource-routes.jsx +36 -0
- data/lib/api_maker/javascript/result.js +25 -0
- data/lib/api_maker/javascript/session-status-updater.js +113 -0
- data/lib/api_maker/javascript/sort-link.jsx +88 -0
- data/lib/api_maker/javascript/updated-attribute.jsx +60 -0
- data/lib/api_maker/loader.rb +14 -0
- data/lib/api_maker/memory_storage.rb +65 -0
- data/lib/api_maker/model_extensions.rb +96 -0
- data/lib/api_maker/permitted_params_argument.rb +12 -0
- data/lib/api_maker/preloader.rb +91 -0
- data/lib/api_maker/preloader_belongs_to.rb +58 -0
- data/lib/api_maker/preloader_has_many.rb +69 -0
- data/lib/api_maker/preloader_has_one.rb +70 -0
- data/lib/api_maker/preloader_through.rb +101 -0
- data/lib/api_maker/railtie.rb +14 -0
- data/lib/api_maker/relationship_includer.rb +42 -0
- data/lib/api_maker/resource_routing.rb +8 -0
- data/lib/api_maker/result_parser.rb +50 -0
- data/lib/api_maker/serializer.rb +86 -0
- data/lib/api_maker/spec_helper.rb +100 -0
- data/lib/api_maker/version.rb +3 -0
- data/lib/tasks/api_maker_tasks.rake +5 -0
- metadata +581 -0
@@ -0,0 +1,169 @@
|
|
1
|
+
import CableSubscriptionPool from "./cable-subscription-pool"
|
2
|
+
import CableSubscription from "./cable-subscription"
|
3
|
+
|
4
|
+
export default class ApiMakerCableConnectionPool {
|
5
|
+
static current() {
|
6
|
+
if (!window.apiMakerCableConnectionPool)
|
7
|
+
window.apiMakerCableConnectionPool = new ApiMakerCableConnectionPool()
|
8
|
+
|
9
|
+
return window.apiMakerCableConnectionPool
|
10
|
+
}
|
11
|
+
|
12
|
+
constructor() {
|
13
|
+
this.connections = {}
|
14
|
+
this.upcomingSubscriptionData = {}
|
15
|
+
this.upcomingSubscriptions = []
|
16
|
+
}
|
17
|
+
|
18
|
+
connectCreated(modelName, callback) {
|
19
|
+
if (!this.upcomingSubscriptionData[modelName])
|
20
|
+
this.upcomingSubscriptionData[modelName] = {}
|
21
|
+
|
22
|
+
if (!this.upcomingSubscriptionData[modelName]["creates"])
|
23
|
+
this.upcomingSubscriptionData[modelName]["creates"] = true
|
24
|
+
|
25
|
+
if (!this.upcomingSubscriptions[modelName])
|
26
|
+
this.upcomingSubscriptions[modelName] = {}
|
27
|
+
|
28
|
+
if (!this.upcomingSubscriptions[modelName]["creates"])
|
29
|
+
this.upcomingSubscriptions[modelName]["creates"] = []
|
30
|
+
|
31
|
+
var subscription = new CableSubscription({
|
32
|
+
callback: callback,
|
33
|
+
modelName: modelName
|
34
|
+
})
|
35
|
+
|
36
|
+
this.upcomingSubscriptions[modelName]["creates"].push(subscription)
|
37
|
+
|
38
|
+
this.scheduleConnectUpcoming()
|
39
|
+
|
40
|
+
return subscription
|
41
|
+
}
|
42
|
+
|
43
|
+
connectDestroyed(modelName, modelId, callback) {
|
44
|
+
if (!this.upcomingSubscriptionData[modelName])
|
45
|
+
this.upcomingSubscriptionData[modelName] = {}
|
46
|
+
|
47
|
+
if (!this.upcomingSubscriptionData[modelName]["destroys"])
|
48
|
+
this.upcomingSubscriptionData[modelName]["destroys"] = []
|
49
|
+
|
50
|
+
if (!this.upcomingSubscriptionData[modelName]["destroys"].includes(modelId))
|
51
|
+
this.upcomingSubscriptionData[modelName]["destroys"].push(modelId)
|
52
|
+
|
53
|
+
if (!this.upcomingSubscriptions[modelName])
|
54
|
+
this.upcomingSubscriptions[modelName] = {}
|
55
|
+
|
56
|
+
if (!this.upcomingSubscriptions[modelName]["destroys"])
|
57
|
+
this.upcomingSubscriptions[modelName]["destroys"] = {}
|
58
|
+
|
59
|
+
if (!this.upcomingSubscriptions[modelName]["destroys"][modelId])
|
60
|
+
this.upcomingSubscriptions[modelName]["destroys"][modelId] = []
|
61
|
+
|
62
|
+
var subscription = new CableSubscription({
|
63
|
+
callback: callback,
|
64
|
+
modelName: modelName,
|
65
|
+
modelId: modelId
|
66
|
+
})
|
67
|
+
|
68
|
+
this.upcomingSubscriptions[modelName]["destroys"][modelId].push(subscription)
|
69
|
+
|
70
|
+
this.scheduleConnectUpcoming()
|
71
|
+
|
72
|
+
return subscription
|
73
|
+
}
|
74
|
+
|
75
|
+
connectEvent(modelName, modelId, eventName, callback) {
|
76
|
+
if (!this.upcomingSubscriptionData[modelName])
|
77
|
+
this.upcomingSubscriptionData[modelName] = {}
|
78
|
+
|
79
|
+
if (!this.upcomingSubscriptionData[modelName]["events"])
|
80
|
+
this.upcomingSubscriptionData[modelName]["events"] = {}
|
81
|
+
|
82
|
+
if (!this.upcomingSubscriptionData[modelName]["events"][eventName])
|
83
|
+
this.upcomingSubscriptionData[modelName]["events"][eventName] = []
|
84
|
+
|
85
|
+
if (!this.upcomingSubscriptionData[modelName]["events"][eventName].includes(modelId))
|
86
|
+
this.upcomingSubscriptionData[modelName]["events"][eventName].push(modelId)
|
87
|
+
|
88
|
+
if (!this.upcomingSubscriptions[modelName])
|
89
|
+
this.upcomingSubscriptions[modelName] = {}
|
90
|
+
|
91
|
+
if (!this.upcomingSubscriptions[modelName]["events"])
|
92
|
+
this.upcomingSubscriptions[modelName]["events"] = {}
|
93
|
+
|
94
|
+
if (!this.upcomingSubscriptions[modelName]["events"])
|
95
|
+
this.upcomingSubscriptions[modelName]["events"] = {}
|
96
|
+
|
97
|
+
if (!this.upcomingSubscriptions[modelName]["events"][modelId])
|
98
|
+
this.upcomingSubscriptions[modelName]["events"][modelId] = {}
|
99
|
+
|
100
|
+
if (!this.upcomingSubscriptions[modelName]["events"][modelId][eventName])
|
101
|
+
this.upcomingSubscriptions[modelName]["events"][modelId][eventName] = []
|
102
|
+
|
103
|
+
var subscription = new CableSubscription({
|
104
|
+
callback: callback,
|
105
|
+
modelName: modelName,
|
106
|
+
modelId: modelId
|
107
|
+
})
|
108
|
+
|
109
|
+
this.upcomingSubscriptions[modelName]["events"][modelId][eventName].push(subscription)
|
110
|
+
|
111
|
+
this.scheduleConnectUpcoming()
|
112
|
+
|
113
|
+
return subscription
|
114
|
+
}
|
115
|
+
|
116
|
+
connectUpdate(modelName, modelId, callback) {
|
117
|
+
if (!this.upcomingSubscriptionData[modelName])
|
118
|
+
this.upcomingSubscriptionData[modelName] = {}
|
119
|
+
|
120
|
+
if (!this.upcomingSubscriptionData[modelName]["updates"])
|
121
|
+
this.upcomingSubscriptionData[modelName]["updates"] = []
|
122
|
+
|
123
|
+
if (!this.upcomingSubscriptionData[modelName]["updates"].includes(modelId))
|
124
|
+
this.upcomingSubscriptionData[modelName]["updates"].push(modelId)
|
125
|
+
|
126
|
+
if (!this.upcomingSubscriptions[modelName])
|
127
|
+
this.upcomingSubscriptions[modelName] = {}
|
128
|
+
|
129
|
+
if (!this.upcomingSubscriptions[modelName]["updates"])
|
130
|
+
this.upcomingSubscriptions[modelName]["updates"] = {}
|
131
|
+
|
132
|
+
if (!this.upcomingSubscriptions[modelName]["updates"][modelId])
|
133
|
+
this.upcomingSubscriptions[modelName]["updates"][modelId] = []
|
134
|
+
|
135
|
+
var subscription = new CableSubscription({
|
136
|
+
callback: callback,
|
137
|
+
modelName: modelName,
|
138
|
+
modelId: modelId
|
139
|
+
})
|
140
|
+
|
141
|
+
this.upcomingSubscriptions[modelName]["updates"][modelId].push(subscription)
|
142
|
+
|
143
|
+
this.scheduleConnectUpcoming()
|
144
|
+
|
145
|
+
return subscription
|
146
|
+
}
|
147
|
+
|
148
|
+
connectUpcoming() {
|
149
|
+
var subscriptionData = this.upcomingSubscriptionData
|
150
|
+
var subscriptions = this.upcomingSubscriptions
|
151
|
+
|
152
|
+
this.upcomingSubscriptionData = {}
|
153
|
+
this.upcomingSubscriptions = {}
|
154
|
+
|
155
|
+
var cableSubscriptionPool = new CableSubscriptionPool({
|
156
|
+
subscriptionData: subscriptionData,
|
157
|
+
subscriptions: subscriptions
|
158
|
+
})
|
159
|
+
|
160
|
+
return cableSubscriptionPool
|
161
|
+
}
|
162
|
+
|
163
|
+
scheduleConnectUpcoming() {
|
164
|
+
if (this.scheduleConnectUpcomingTimeout)
|
165
|
+
clearTimeout(this.scheduleConnectUpcomingTimeout)
|
166
|
+
|
167
|
+
this.scheduleConnectUpcomingTimeout = setTimeout(() => this.connectUpcoming(), 50)
|
168
|
+
}
|
169
|
+
}
|
@@ -0,0 +1,111 @@
|
|
1
|
+
import CommandsPool from "./commands-pool"
|
2
|
+
import Deserializer from "./deserializer"
|
3
|
+
import Logger from "./logger"
|
4
|
+
|
5
|
+
const inflection = require("inflection")
|
6
|
+
|
7
|
+
export default class ApiMakerCableSubscriptionPool {
|
8
|
+
constructor(props) {
|
9
|
+
this.props = props
|
10
|
+
this.activeSubscriptions = 0
|
11
|
+
this.registerSubscriptions()
|
12
|
+
this.connect()
|
13
|
+
}
|
14
|
+
|
15
|
+
connect() {
|
16
|
+
var globalData = CommandsPool.current().globalRequestData
|
17
|
+
|
18
|
+
this.subscription = App.cable.subscriptions.create(
|
19
|
+
{channel: "ApiMaker::SubscriptionsChannel", global: globalData, subscription_data: this.props.subscriptionData},
|
20
|
+
{received: (data) => this.onReceived(data)}
|
21
|
+
)
|
22
|
+
}
|
23
|
+
|
24
|
+
onReceived(rawData) {
|
25
|
+
var data = Deserializer.parse(rawData)
|
26
|
+
var modelType = data.model_type
|
27
|
+
var modelName = inflection.camelize(inflection.singularize(modelType.replace(/-/g, "_")))
|
28
|
+
var modelId = data.model_id
|
29
|
+
var modelInstance = data.model
|
30
|
+
var subscriptions = this.props.subscriptions
|
31
|
+
|
32
|
+
if (data.type == "update") {
|
33
|
+
for(var subscription of subscriptions[modelName]["updates"][modelId]) {
|
34
|
+
subscription.onReceived({model: modelInstance})
|
35
|
+
}
|
36
|
+
} else if (data.type == "create") {
|
37
|
+
for(var subscription of subscriptions[modelName]["creates"]) {
|
38
|
+
subscription.onReceived({model: modelInstance})
|
39
|
+
}
|
40
|
+
} else if (data.type == "destroy") {
|
41
|
+
for(var subscription of subscriptions[modelName]["destroys"][modelId]) {
|
42
|
+
subscription.onReceived({model: modelInstance})
|
43
|
+
}
|
44
|
+
} else if (data.type == "event") {
|
45
|
+
for(var subscription of subscriptions[modelName]["events"][modelId][data.event_name]) {
|
46
|
+
subscription.onReceived({
|
47
|
+
args: data.args,
|
48
|
+
eventName: data.event_name,
|
49
|
+
model: modelInstance
|
50
|
+
})
|
51
|
+
}
|
52
|
+
} else {
|
53
|
+
throw new Error(`Unknown type: ${data.type}`)
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
onUnsubscribe() {
|
58
|
+
Logger.log(`activeSubscriptions before unsub: ${this.activeSubscriptions}`)
|
59
|
+
this.activeSubscriptions -= 1
|
60
|
+
Logger.log(`activeSubscriptions after unsub: ${this.activeSubscriptions}`)
|
61
|
+
|
62
|
+
if (this.activeSubscriptions <= 0) {
|
63
|
+
Logger.log("Unsubscribe from ActionCable subscription")
|
64
|
+
this.subscription.unsubscribe()
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
registerSubscriptions() {
|
69
|
+
Logger.log(`registerSubscriptions: ${this.props.subscriptions.length}`)
|
70
|
+
Logger.log(this.props.subscriptions)
|
71
|
+
|
72
|
+
for(var modelName in this.props.subscriptions) {
|
73
|
+
if (this.props.subscriptions[modelName]["creates"]) {
|
74
|
+
for(var subscription of this.props.subscriptions[modelName]["creates"]) {
|
75
|
+
this.connectUnsubscriptionForSubscription(subscription)
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
if (this.props.subscriptions[modelName]["events"]) {
|
80
|
+
for(var eventName in this.props.subscriptions[modelName]["events"]) {
|
81
|
+
for(var modelId in this.props.subscriptions[modelName]["events"][eventName]) {
|
82
|
+
for(var subscription of this.props.subscriptions[modelName]["events"][eventName][modelId]) {
|
83
|
+
this.connectUnsubscriptionForSubscription(subscription)
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
if (this.props.subscriptions[modelName]["updates"]) {
|
90
|
+
for(var modelId in this.props.subscriptions[modelName]["updates"]) {
|
91
|
+
for(var subscription of this.props.subscriptions[modelName]["updates"][modelId]) {
|
92
|
+
this.connectUnsubscriptionForSubscription(subscription)
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
connectUnsubscriptionForSubscription(subscription) {
|
100
|
+
Logger.log("Connecting to unsubscribe on subscription")
|
101
|
+
Logger.log({ subscription })
|
102
|
+
|
103
|
+
this.activeSubscriptions += 1
|
104
|
+
|
105
|
+
subscription.onUnsubscribe(() => {
|
106
|
+
Logger.log("Call onUnsubscribe on self")
|
107
|
+
|
108
|
+
this.onUnsubscribe(subscription)
|
109
|
+
})
|
110
|
+
}
|
111
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import Logger from "./logger"
|
2
|
+
|
3
|
+
export default class ApiMakerCableSubscription {
|
4
|
+
constructor(props) {
|
5
|
+
this.props = props
|
6
|
+
this.onUnsubscribeCallbacks = []
|
7
|
+
this.subscribed = true
|
8
|
+
}
|
9
|
+
|
10
|
+
onReceived(data) {
|
11
|
+
this.props.callback.apply(null, [data])
|
12
|
+
}
|
13
|
+
|
14
|
+
onUnsubscribe(callback) {
|
15
|
+
this.onUnsubscribeCallbacks.push(callback)
|
16
|
+
}
|
17
|
+
|
18
|
+
unsubscribe() {
|
19
|
+
if (!this.subscribed) {
|
20
|
+
Logger.log("Unsubscribed already called")
|
21
|
+
return
|
22
|
+
}
|
23
|
+
|
24
|
+
Logger.log(`Unsubscribe called: ${this.onUnsubscribeCallbacks.length}`)
|
25
|
+
|
26
|
+
for(var onUnsubscribeCallback of this.onUnsubscribeCallbacks) {
|
27
|
+
Logger.log("onUnsubscribe called for a callback")
|
28
|
+
onUnsubscribeCallback.call()
|
29
|
+
}
|
30
|
+
|
31
|
+
this.subscribed = false
|
32
|
+
}
|
33
|
+
}
|
@@ -0,0 +1,186 @@
|
|
1
|
+
import CommandsPool from "./commands-pool"
|
2
|
+
import merge from "merge"
|
3
|
+
import ModelsResponseReader from "./models-response-reader"
|
4
|
+
import Result from "./result"
|
5
|
+
|
6
|
+
const inflection = require("inflection")
|
7
|
+
|
8
|
+
export default class Collection {
|
9
|
+
constructor(args, queryArgs = {}) {
|
10
|
+
this.queryArgs = queryArgs
|
11
|
+
this.args = args
|
12
|
+
}
|
13
|
+
|
14
|
+
accessibleBy(abilityName) {
|
15
|
+
return this._clone({accessibleBy: abilityName})
|
16
|
+
}
|
17
|
+
|
18
|
+
distinct() {
|
19
|
+
return this._clone({distinct: true})
|
20
|
+
}
|
21
|
+
|
22
|
+
async each(callback) {
|
23
|
+
var array = await this.toArray()
|
24
|
+
|
25
|
+
for(var model in array) {
|
26
|
+
callback.call(model)
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
async first() {
|
31
|
+
var models = await this.toArray()
|
32
|
+
return models[0]
|
33
|
+
}
|
34
|
+
|
35
|
+
async count() {
|
36
|
+
var response = await this._clone({count: true})._response()
|
37
|
+
return response.count
|
38
|
+
}
|
39
|
+
|
40
|
+
isLoaded() {
|
41
|
+
if (this.args.reflectionName in this.args.model.relationshipsCache)
|
42
|
+
return true
|
43
|
+
|
44
|
+
return false
|
45
|
+
}
|
46
|
+
|
47
|
+
limit(amount) {
|
48
|
+
return this._clone({limit: amount})
|
49
|
+
}
|
50
|
+
|
51
|
+
loaded() {
|
52
|
+
if (!(this.args.reflectionName in this.args.model.relationshipsCache)) {
|
53
|
+
throw new Error(`${this.args.reflectionName} hasnt been loaded yet`)
|
54
|
+
}
|
55
|
+
|
56
|
+
return this.args.model.relationshipsCache[this.args.reflectionName]
|
57
|
+
}
|
58
|
+
|
59
|
+
preload(preloadValue) {
|
60
|
+
return this._clone({preload: preloadValue})
|
61
|
+
}
|
62
|
+
|
63
|
+
page(pageNumber) {
|
64
|
+
if (!pageNumber)
|
65
|
+
pageNumber = 1
|
66
|
+
|
67
|
+
var changes = {page: pageNumber}
|
68
|
+
|
69
|
+
if (!this.queryArgs.ransack || !this.queryArgs.ransack.s)
|
70
|
+
changes.ransack = {s: `${this.args.modelClass.modelClassData().primaryKey} asc`}
|
71
|
+
|
72
|
+
return this._clone(changes)
|
73
|
+
}
|
74
|
+
|
75
|
+
pageKey(pageKeyValue) {
|
76
|
+
return this._clone({pageKey: pageKeyValue})
|
77
|
+
}
|
78
|
+
|
79
|
+
ransack(params) {
|
80
|
+
return this._clone({ransack: params})
|
81
|
+
}
|
82
|
+
|
83
|
+
async result() {
|
84
|
+
var response = await this._response()
|
85
|
+
var models = this._responseToModels(response)
|
86
|
+
var result = new Result({collection: this, models, response})
|
87
|
+
return result
|
88
|
+
}
|
89
|
+
|
90
|
+
searchKey(searchKey) {
|
91
|
+
return this._clone({searchKey: searchKey})
|
92
|
+
}
|
93
|
+
|
94
|
+
select(originalSelect) {
|
95
|
+
var newSelect = {}
|
96
|
+
|
97
|
+
for(var originalModelName in originalSelect) {
|
98
|
+
var newModalName = inflection.dasherize(inflection.underscore(originalModelName))
|
99
|
+
var newValues = []
|
100
|
+
var originalValues = originalSelect[originalModelName]
|
101
|
+
|
102
|
+
for(var originalAttributeName of originalValues) {
|
103
|
+
var newAttributeName = inflection.underscore(originalAttributeName)
|
104
|
+
newValues.push(newAttributeName)
|
105
|
+
}
|
106
|
+
|
107
|
+
newSelect[newModalName] = newValues
|
108
|
+
}
|
109
|
+
|
110
|
+
return this._clone({select: newSelect})
|
111
|
+
}
|
112
|
+
|
113
|
+
sort(sortBy) {
|
114
|
+
return this._clone({ransack: {s: sortBy}})
|
115
|
+
}
|
116
|
+
|
117
|
+
async toArray() {
|
118
|
+
var response = await this._response()
|
119
|
+
return this._responseToModels(response)
|
120
|
+
}
|
121
|
+
|
122
|
+
modelClass() {
|
123
|
+
return require(`api-maker/models/${inflection.dasherize(inflection.singularize(this.args.modelClass.modelClassData().collectionName))}`).default
|
124
|
+
}
|
125
|
+
|
126
|
+
_clone(args) {
|
127
|
+
return new Collection(this.args, this._merge(this.queryArgs, args))
|
128
|
+
}
|
129
|
+
|
130
|
+
_merge(object1, object2) {
|
131
|
+
return merge.recursive(true, object1, object2)
|
132
|
+
}
|
133
|
+
|
134
|
+
_response() {
|
135
|
+
var modelClassData = this.args.modelClass.modelClassData()
|
136
|
+
|
137
|
+
return CommandsPool.addCommand(
|
138
|
+
{
|
139
|
+
args: this._params(),
|
140
|
+
command: `${modelClassData.collectionName}-index`,
|
141
|
+
collectionName: modelClassData.collectionName,
|
142
|
+
type: "index"
|
143
|
+
},
|
144
|
+
{}
|
145
|
+
)
|
146
|
+
}
|
147
|
+
|
148
|
+
_responseToModels(response) {
|
149
|
+
var modelsResponseReader = new ModelsResponseReader({response: response})
|
150
|
+
return modelsResponseReader.models()
|
151
|
+
}
|
152
|
+
|
153
|
+
_params() {
|
154
|
+
var params = {}
|
155
|
+
|
156
|
+
if (this.queryArgs.params)
|
157
|
+
params = this._merge(params, this.queryArgs.params)
|
158
|
+
|
159
|
+
if (this.queryArgs.accessibleBy) {
|
160
|
+
params.accessible_by = inflection.underscore(this.queryArgs.accessibleBy)
|
161
|
+
}
|
162
|
+
|
163
|
+
if (this.queryArgs.count)
|
164
|
+
params.count = this.queryArgs.count
|
165
|
+
|
166
|
+
if (this.queryArgs.distinct)
|
167
|
+
params.distinct = this.queryArgs.distinct
|
168
|
+
|
169
|
+
if (this.queryArgs.ransack)
|
170
|
+
params.q = this.queryArgs.ransack
|
171
|
+
|
172
|
+
if (this.queryArgs.limit)
|
173
|
+
params.limit = this.queryArgs.limit
|
174
|
+
|
175
|
+
if (this.queryArgs.preload)
|
176
|
+
params.include = this.queryArgs.preload
|
177
|
+
|
178
|
+
if (this.queryArgs.page)
|
179
|
+
params.page = this.queryArgs.page
|
180
|
+
|
181
|
+
if (this.queryArgs.select)
|
182
|
+
params.select = this.queryArgs.select
|
183
|
+
|
184
|
+
return params
|
185
|
+
}
|
186
|
+
}
|