jason-rails 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/client/lib/JasonContext.d.ts +2 -0
- data/client/lib/JasonContext.js +5 -0
- data/client/lib/JasonProvider.d.ts +7 -0
- data/client/lib/JasonProvider.js +109 -0
- data/client/lib/actionFactory.d.ts +5 -0
- data/client/lib/actionFactory.js +33 -0
- data/client/lib/createActions.d.ts +2 -0
- data/client/lib/createActions.js +46 -0
- data/client/lib/createJasonReducers.d.ts +1 -0
- data/client/lib/createJasonReducers.js +36 -0
- data/client/lib/createPayloadHandler.d.ts +1 -0
- data/client/lib/createPayloadHandler.js +87 -0
- data/client/lib/index.d.ts +10 -0
- data/client/lib/index.js +12 -0
- data/client/lib/makeEager.d.ts +1 -0
- data/client/lib/makeEager.js +51 -0
- data/client/lib/useAct.d.ts +1 -0
- data/client/lib/useAct.js +12 -0
- data/client/lib/useSub.d.ts +1 -0
- data/client/lib/useSub.js +14 -0
- data/client/package.json +27 -0
- data/client/src/JasonContext.ts +5 -0
- data/client/src/JasonProvider.tsx +108 -0
- data/client/src/actionFactory.ts +34 -0
- data/client/src/createActions.ts +50 -0
- data/client/src/createJasonReducers.ts +34 -0
- data/client/src/createPayloadHandler.ts +95 -0
- data/client/src/index.ts +7 -0
- data/client/src/makeEager.ts +46 -0
- data/client/src/useAct.ts +9 -0
- data/client/src/useSub.ts +10 -0
- data/client/tsconfig.json +15 -0
- data/client/yarn.lock +140 -0
- data/jason-rails.gemspec +25 -0
- data/lib/jason.rb +10 -0
- data/lib/jason/api_model.rb +47 -0
- data/lib/jason/channel.rb +37 -0
- data/lib/jason/publisher.rb +79 -0
- data/lib/jason/subscription.rb +172 -0
- data/lib/jason/version.rb +3 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2883964fb830dbb506a0e845caa3398436d72262f72cc6b295efce14dcc96346
|
4
|
+
data.tar.gz: 631d00d04195b1e1f48111ca5a6cc02775488e48f94ab5d8d29783a66a6c91a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6de81fcdba79fc067fe7515ad83be895e30d19686e548bfb774026993ab965cf6f0aaa9cebcd4807448e5218c8c9609f414e6ec05d5eb717aef880e3f2e012ea
|
7
|
+
data.tar.gz: adb6476af69fe904588f55214b930959a0b0d1d2357c3edafca77cc4f8d528a72cb0f72cde6dbec4e097e58d6e5758a2c429419cd9ad60f02d0352dd9abe385b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jarees@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 James Rees
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Jason
|
2
|
+
|
3
|
+
## The goal
|
4
|
+
|
5
|
+
I wanted:
|
6
|
+
- Automatic updates to client state based on database state
|
7
|
+
- Automatic persistence to database
|
8
|
+
- Redux for awesome state management
|
9
|
+
- Optimistic updates
|
10
|
+
|
11
|
+
I also wanted to avoid writing essentially the same code multiple times in different places to handle common CRUD-like operations. Combine Rails schema definition files, REST endpoints, Redux actions, stores, reducers, handlers for websocket payloads and the translations between them, and it adds up to tons of repetitive boilerplate. Every change to the data schema requires updates in five or six files. This inhibits refactoring and makes mistakes more likely.
|
12
|
+
|
13
|
+
Jason attempts to minimize this repitition by auto-generating API endpoints, redux stores and actions from a single schema definition. Further it adds listeners to ActiveRecord models allowing the redux store to be subscribed to updates from a model or set of models.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'jason-rails'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle install
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install jason
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
TODO: Write usage instructions here
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jason. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/jason/blob/master/CODE_OF_CONDUCT.md).
|
44
|
+
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
49
|
+
|
50
|
+
## Code of Conduct
|
51
|
+
|
52
|
+
Everyone interacting in the Jason project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jason/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jason"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
+
}) : (function(o, m, k, k2) {
|
6
|
+
if (k2 === undefined) k2 = k;
|
7
|
+
o[k2] = m[k];
|
8
|
+
}));
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11
|
+
}) : function(o, v) {
|
12
|
+
o["default"] = v;
|
13
|
+
});
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
15
|
+
if (mod && mod.__esModule) return mod;
|
16
|
+
var result = {};
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18
|
+
__setModuleDefault(result, mod);
|
19
|
+
return result;
|
20
|
+
};
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
23
|
+
};
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
25
|
+
const createActions_1 = __importDefault(require("./createActions"));
|
26
|
+
const actioncable_1 = require("@rails/actioncable");
|
27
|
+
const JasonContext_1 = __importDefault(require("./JasonContext"));
|
28
|
+
const axios_1 = __importDefault(require("axios"));
|
29
|
+
const axios_case_converter_1 = __importDefault(require("axios-case-converter"));
|
30
|
+
const react_redux_1 = require("react-redux");
|
31
|
+
const toolkit_1 = require("@reduxjs/toolkit");
|
32
|
+
const createJasonReducers_1 = __importDefault(require("./createJasonReducers"));
|
33
|
+
const createPayloadHandler_1 = __importDefault(require("./createPayloadHandler"));
|
34
|
+
const makeEager_1 = __importDefault(require("./makeEager"));
|
35
|
+
const humps_1 = require("humps");
|
36
|
+
const blueimp_md5_1 = __importDefault(require("blueimp-md5"));
|
37
|
+
const lodash_1 = __importDefault(require("lodash"));
|
38
|
+
const react_1 = __importStar(require("react"));
|
39
|
+
const JasonProvider = ({ reducers, middleware, extraActions, children }) => {
|
40
|
+
const [store, setStore] = react_1.useState(null);
|
41
|
+
const [value, setValue] = react_1.useState(null);
|
42
|
+
const [connected, setConnected] = react_1.useState(false);
|
43
|
+
const csrfToken = document.querySelector("meta[name=csrf-token]").content;
|
44
|
+
axios_1.default.defaults.headers.common['X-CSRF-Token'] = csrfToken;
|
45
|
+
const restClient = axios_case_converter_1.default(axios_1.default.create());
|
46
|
+
react_1.useEffect(() => {
|
47
|
+
restClient.get('/jason/api/schema')
|
48
|
+
.then(({ data: snakey_schema }) => {
|
49
|
+
const schema = humps_1.camelizeKeys(snakey_schema);
|
50
|
+
const consumer = actioncable_1.createConsumer();
|
51
|
+
const allReducers = Object.assign(Object.assign({}, reducers), createJasonReducers_1.default(schema));
|
52
|
+
console.log({ schema, allReducers });
|
53
|
+
const store = toolkit_1.configureStore({ reducer: allReducers, middleware });
|
54
|
+
let payloadHandlers = {};
|
55
|
+
function handlePayload(payload) {
|
56
|
+
const { model, md5Hash } = payload;
|
57
|
+
console.log({ md5Hash, fn: `${model}:${md5Hash}`, payloadHandlers, model: lodash_1.default.camelCase(model), payload });
|
58
|
+
const handler = payloadHandlers[`${lodash_1.default.camelCase(model)}:${md5Hash}`];
|
59
|
+
if (handler) {
|
60
|
+
handler(Object.assign(Object.assign({}, payload), { model: lodash_1.default.camelCase(model) }));
|
61
|
+
}
|
62
|
+
}
|
63
|
+
const subscription = (consumer.subscriptions.create({
|
64
|
+
channel: 'Jason::Channel'
|
65
|
+
}, {
|
66
|
+
connected: () => {
|
67
|
+
setConnected(true);
|
68
|
+
},
|
69
|
+
received: payload => {
|
70
|
+
console.log("Payload received", payload);
|
71
|
+
handlePayload(payload);
|
72
|
+
},
|
73
|
+
disconnected: () => console.warn('Disconnected from ActionCable')
|
74
|
+
}));
|
75
|
+
console.log('sending message');
|
76
|
+
subscription.send({ message: 'test' });
|
77
|
+
function createSubscription(config) {
|
78
|
+
const md5Hash = blueimp_md5_1.default(JSON.stringify(config));
|
79
|
+
console.log('Subscribe with', config, md5Hash);
|
80
|
+
lodash_1.default.map(config, (v, model) => {
|
81
|
+
payloadHandlers[`${model}:${md5Hash}`] = createPayloadHandler_1.default(store.dispatch, subscription, model, schema[model]);
|
82
|
+
});
|
83
|
+
subscription.send({ createSubscription: config });
|
84
|
+
return () => removeSubscription(config);
|
85
|
+
}
|
86
|
+
function removeSubscription(config) {
|
87
|
+
subscription.send({ removeSubscription: config });
|
88
|
+
const md5Hash = blueimp_md5_1.default(JSON.stringify(config));
|
89
|
+
lodash_1.default.map(config, (v, model) => {
|
90
|
+
delete payloadHandlers[`${model}:${md5Hash}`];
|
91
|
+
});
|
92
|
+
}
|
93
|
+
const actions = createActions_1.default(schema, store, restClient, extraActions);
|
94
|
+
const eager = makeEager_1.default(schema);
|
95
|
+
console.log({ actions });
|
96
|
+
setValue({
|
97
|
+
actions: actions,
|
98
|
+
subscribe: (config) => createSubscription(config),
|
99
|
+
eager
|
100
|
+
});
|
101
|
+
setStore(store);
|
102
|
+
});
|
103
|
+
}, []);
|
104
|
+
if (!(store && value && connected))
|
105
|
+
return react_1.default.createElement("div", null); // Wait for async fetch of schema to complete
|
106
|
+
return react_1.default.createElement(react_redux_1.Provider, { store: store },
|
107
|
+
react_1.default.createElement(JasonContext_1.default.Provider, { value: value }, children));
|
108
|
+
};
|
109
|
+
exports.default = JasonProvider;
|
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const pluralize_1 = __importDefault(require("pluralize"));
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
8
|
+
const uuid_1 = require("uuid");
|
9
|
+
exports.default = (dis, store, entity, { extraActions = {}, hasPriority = false } = {}) => {
|
10
|
+
function add(data = {}) {
|
11
|
+
const id = uuid_1.v4();
|
12
|
+
return dis({ type: `${pluralize_1.default(entity)}/add`, payload: Object.assign({ id }, data) });
|
13
|
+
}
|
14
|
+
function upsert(id, data) {
|
15
|
+
return dis({ type: `${pluralize_1.default(entity)}/upsert`, payload: Object.assign({ id }, data) });
|
16
|
+
}
|
17
|
+
function movePriority(id, priority, parentFilter = {}) {
|
18
|
+
return dis({ type: `${pluralize_1.default(entity)}/movePriority`, payload: { id, priority, parentFilter } });
|
19
|
+
}
|
20
|
+
function setAll(data) {
|
21
|
+
return dis({ type: `${pluralize_1.default(entity)}/setAll`, payload: data });
|
22
|
+
}
|
23
|
+
function remove(id) {
|
24
|
+
return dis({ type: `${pluralize_1.default(entity)}/remove`, payload: id });
|
25
|
+
}
|
26
|
+
const extraActionsResolved = lodash_1.default.mapValues(extraActions, v => v(dis, store, entity));
|
27
|
+
if (hasPriority) {
|
28
|
+
return Object.assign({ add, upsert, setAll, remove, movePriority }, extraActionsResolved);
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
return Object.assign({ add, upsert, setAll, remove }, extraActionsResolved);
|
32
|
+
}
|
33
|
+
};
|
@@ -0,0 +1,46 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const actionFactory_1 = __importDefault(require("./actionFactory"));
|
7
|
+
const pluralize_1 = __importDefault(require("pluralize"));
|
8
|
+
const lodash_1 = __importDefault(require("lodash"));
|
9
|
+
const uuid_1 = require("uuid");
|
10
|
+
function enrich(type, payload) {
|
11
|
+
if (type.split('/')[1] === 'upsert' && !(type.split('/')[0] === 'session')) {
|
12
|
+
if (!payload.id) {
|
13
|
+
return Object.assign(Object.assign({}, payload), { id: uuid_1.v4() });
|
14
|
+
}
|
15
|
+
}
|
16
|
+
return payload;
|
17
|
+
}
|
18
|
+
function makeOptDis(schema, dispatch, restClient) {
|
19
|
+
const plurals = lodash_1.default.keys(schema).map(k => pluralize_1.default(k));
|
20
|
+
return function (action) {
|
21
|
+
const { type, payload } = action;
|
22
|
+
const data = enrich(type, payload);
|
23
|
+
dispatch(action);
|
24
|
+
if (plurals.indexOf(type.split('/')[0]) > -1) {
|
25
|
+
return restClient.post('/jason/api/action', { type, payload: data })
|
26
|
+
.catch(e => {
|
27
|
+
dispatch({ type: 'upsertLocalUi', data: { error: JSON.stringify(e) } });
|
28
|
+
});
|
29
|
+
}
|
30
|
+
};
|
31
|
+
}
|
32
|
+
function createActions(schema, store, restClient, extraActions) {
|
33
|
+
const dis = store.dispatch;
|
34
|
+
const optDis = makeOptDis(schema, dis, restClient);
|
35
|
+
const actions = lodash_1.default.fromPairs(lodash_1.default.map(schema, (config, model) => {
|
36
|
+
if (config.priorityScope) {
|
37
|
+
return [pluralize_1.default(model), actionFactory_1.default(optDis, store, model, { hasPriority: true })];
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
return [pluralize_1.default(model), actionFactory_1.default(optDis, store, model)];
|
41
|
+
}
|
42
|
+
}));
|
43
|
+
const extraActionsResolved = extraActions(optDis, store, restClient);
|
44
|
+
return lodash_1.default.merge(actions, extraActionsResolved);
|
45
|
+
}
|
46
|
+
exports.default = createActions;
|