snfoil-rails 1.0.0.pre.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +74 -0
  4. data/Gemfile +17 -0
  5. data/LICENSE.txt +201 -0
  6. data/README.md +207 -0
  7. data/lib/generators/snfoil/all/USAGE +21 -0
  8. data/lib/generators/snfoil/all/all_generator.rb +64 -0
  9. data/lib/generators/snfoil/context/USAGE +14 -0
  10. data/lib/generators/snfoil/context/context_generator.rb +55 -0
  11. data/lib/generators/snfoil/context/templates/context.erb +9 -0
  12. data/lib/generators/snfoil/controller/USAGE +15 -0
  13. data/lib/generators/snfoil/controller/controller_generator.rb +66 -0
  14. data/lib/generators/snfoil/controller/templates/api_controller.erb +14 -0
  15. data/lib/generators/snfoil/controller/templates/controller.erb +14 -0
  16. data/lib/generators/snfoil/jsonapi_deserializer/USAGE +14 -0
  17. data/lib/generators/snfoil/jsonapi_deserializer/jsonapi_deserializer_generator.rb +55 -0
  18. data/lib/generators/snfoil/jsonapi_deserializer/templates/jsonapi_deserializer.erb +12 -0
  19. data/lib/generators/snfoil/jsonapi_serializer/USAGE +14 -0
  20. data/lib/generators/snfoil/jsonapi_serializer/jsonapi_serializer_generator.rb +55 -0
  21. data/lib/generators/snfoil/jsonapi_serializer/templates/jsonapi_serializer.erb +20 -0
  22. data/lib/generators/snfoil/policy/USAGE +14 -0
  23. data/lib/generators/snfoil/policy/policy_generator.rb +55 -0
  24. data/lib/generators/snfoil/policy/templates/policy.erb +22 -0
  25. data/lib/generators/snfoil/searcher/USAGE +14 -0
  26. data/lib/generators/snfoil/searcher/searcher_generator.rb +55 -0
  27. data/lib/generators/snfoil/searcher/templates/searcher.erb +7 -0
  28. data/lib/snfoil/rails/api/create.rb +56 -0
  29. data/lib/snfoil/rails/api/destroy.rb +54 -0
  30. data/lib/snfoil/rails/api/index.rb +53 -0
  31. data/lib/snfoil/rails/api/show.rb +53 -0
  32. data/lib/snfoil/rails/api/update.rb +60 -0
  33. data/lib/snfoil/rails/api_controller.rb +33 -0
  34. data/lib/snfoil/rails/concerns/inject_deserialized.rb +31 -0
  35. data/lib/snfoil/rails/concerns/inject_id.rb +33 -0
  36. data/lib/snfoil/rails/concerns/inject_include.rb +35 -0
  37. data/lib/snfoil/rails/concerns/inject_request_id.rb +31 -0
  38. data/lib/snfoil/rails/concerns/inject_request_params.rb +34 -0
  39. data/lib/snfoil/rails/concerns/process_pagination.rb +71 -0
  40. data/lib/snfoil/rails/engine.rb +38 -0
  41. data/lib/snfoil/rails/searcher.rb +127 -0
  42. data/lib/snfoil/rails/version.rb +21 -0
  43. data/lib/snfoil/rails.rb +7 -0
  44. data/lib/tasks/snfoil/rails_tasks.rake +5 -0
  45. data/snfoil-rails.gemspec +53 -0
  46. metadata +86 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de68b52a3ddf90c440b1d0601019937b12a7c16323269089e7af092fb43266be
4
- data.tar.gz: 7a209f3e62ecd18f1a8c33d730ea17313298a5231605a7dade3907ed49b78075
3
+ metadata.gz: b6d0a10406bfe73b91c6afde586fb98abe342912585e1f33a06e8371159fa36b
4
+ data.tar.gz: a47f6014595e2bdb69130216208b5aa3c6d84ae0749b9754adc35d0dc5c58bf6
5
5
  SHA512:
6
- metadata.gz: 2e95ed3b2a1e7ccaa32515d954786b49365a17cfa1de8957102538d5e7a2313800bbaed6690382deeeae903cdf1a9e43c55182046ef4d011490b93547e953aa0
7
- data.tar.gz: 38a06c487c245f368e09973af4bfa105071c7f9fa8bde2b4372f9879aa77a146f23f026a852098e8a0b5995a06ad1b4e43bd79285692c2344c10dc23d2abcb0c
6
+ metadata.gz: 4bd799a4121f59c51994181369fe925a3fbda54a6b384a05690f06c3d1eac9c0c6f6b64067467581fb07acbfc769646c98d14d5b7e43da9503575fb5db10f5df
7
+ data.tar.gz: 347df9b4acbd0af0354a8d951af0c3e8e2eec3734bc91693e1bf0a22c74d498db276239b9a454568307290caaf9f7e182e9311b5a92d8d965d094855e954d5bf
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-26
4
+
5
+ - Initial release
@@ -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 howeszy@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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
5
+
6
+ # Declare your gem's dependencies in sn-foil-rails.gemspec.
7
+ # Bundler will treat runtime dependencies like base dependencies, and
8
+ # development dependencies will be added by default to the :development group.
9
+ gemspec
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use a debugger
17
+ # gem 'byebug', group: [:development, :test]
data/LICENSE.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ https://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ https://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,207 @@
1
+ # Sn::Foil::Rails
2
+ ![build](https://github.com/limited-effort/snfoil-rails/actions/workflows/main.yml/badge.svg)
3
+ [![maintainability](https://api.codeclimate.com/v1/badges/e6db54dbd56e5cb226f1/maintainability)](https://codeclimate.com/github/limited-effort/snfoil-rails/maintainability)
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'snfoil-rails'
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle
15
+ ```
16
+
17
+ Or install it yourself as:
18
+ ```bash
19
+ $ gem install snfoil-rails
20
+ ```
21
+ ## Usage
22
+ SnFoil Rails mostly just ties together a bunch of the other SnFoil Repos together. To get the most out of this project we highly recommend you read the docs for SnFoil [Contexts](https://github.com/limited-effort/snfoil-context/blob/main/README.md), [Controllers](https://github.com/limited-effort/snfoil-controller/blob/main/README.md), [Searchers](https://github.com/limited-effort/snfoil-searcher/blob/main/README.md), and [Policies](https://github.com/limited-effort/snfoil-policy/blob/main/README.md).
23
+
24
+ What it does add is a few rails friend steps for you:
25
+ - Basic CRUD controllers
26
+ - JSONAPI Include Parser
27
+ - Serialize and Deserialize Helpers
28
+ - Pagination Helpers
29
+ - Additional Searcher Functionality
30
+
31
+ ### CRUD Controllers
32
+
33
+ To use setup a crud controller you just need to inherit from the `SnFoil::Rails::APIController`
34
+
35
+ ```ruby
36
+ class PeopleController < SnFoil::Rails::APIController
37
+ ...
38
+ end
39
+ ```
40
+
41
+ This will setup `create`, `show`, `update`, `destroy`, and `index` for you.
42
+
43
+ If you only want one or a few of these methods you can include just that functionality using the appropraite concerns.
44
+
45
+ ```ruby
46
+ class PeopleController < ActionController::API
47
+ # Just include Create and Destroy
48
+ include SnFoil::Rails::API::Create
49
+ include SnFoil::Rails::API::Destroy
50
+ ...
51
+ end
52
+ ```
53
+
54
+ ### Helpers
55
+
56
+ <table>
57
+ <thead>
58
+ <th>Name</th>
59
+ <th>Arguments</th>
60
+ <th>Description</th>
61
+ </thead>
62
+ <tbody>
63
+ <tr>
64
+ <td>
65
+ inject_deserialized
66
+ </td>
67
+ <td>
68
+ options - hash
69
+ </td>
70
+ <td>
71
+ calls the deserialize method using the current option `:params` and injects it into the options hash as `:params`
72
+ </td>
73
+ </tr>
74
+ <tr>
75
+ <td>
76
+ inject_id
77
+ </td>
78
+ <td>
79
+ options - hash
80
+ </td>
81
+ <td>
82
+ attempts to find the appropriate id of the current request model and injects it into the options hash as `:id`
83
+ </td>
84
+ </tr>
85
+ <tr>
86
+ <td>
87
+ inject_include
88
+ </td>
89
+ <td>
90
+ options - hash
91
+ </td>
92
+ <td>
93
+ takes include from the params object, parses it by common, and injects it as a symbolized array into the options hash as ':include'
94
+ </td>
95
+ </tr>
96
+ <tr>
97
+ <td>
98
+ inject_request_id
99
+ </td>
100
+ <td>
101
+ options - hash
102
+ </td>
103
+ <td>
104
+ puts the current request's request_id into the options hash as `:request_id`
105
+ </td>
106
+ </tr>
107
+ <tr>
108
+ <td>
109
+ inject_request_params
110
+ </td>
111
+ <td>
112
+ options - hash
113
+ </td>
114
+ <td>
115
+ calls the current controller's params, symbolizes the keys, and injects it into the options hash as `:params` and `:request_params`. Since `:params` is expected to be overridden by `#inject_deserialized` a copy of the original params is store in `:request_params`
116
+ </td>
117
+ </tr>
118
+ <tr>
119
+ <td>
120
+ process_pagination
121
+ </td>
122
+ <td>
123
+ options - hash
124
+ </td>
125
+ <td>
126
+ uses the `:request_params` to discern how to paginate
127
+ </td>
128
+ </tr>
129
+ </tbody>
130
+ </table>
131
+
132
+ ### Pagination
133
+
134
+ While most of the helpers are pretty simple pagination require a little more detail.
135
+
136
+ `#process_pagination` uses parameters from `:request_params` in the options hash.
137
+
138
+ - per_page - The amount of results to return per page. If `0` then it will return the page_maximum. Default `10`.
139
+ - page - The page to return.
140
+
141
+ ### Searchers
142
+
143
+ In addition to the base [searcher](https://github.com/limited-effort/snfoil-searcher) functionality a SnFoil::Rails::Searcher includes the following
144
+
145
+ #### order
146
+ The default order direction you want the results to be return in. This can be overridden by including `order` in the request params. Defaults to `ASC`.
147
+
148
+ ```ruby
149
+ class PeopleSearcher
150
+ include SnFoil::Searcher
151
+
152
+ model Person
153
+ order :ASC
154
+ end
155
+ ```
156
+
157
+
158
+ #### order_by
159
+ The model attribute you want to order by. This can be overridden by including `order_by` in the request params. Defaults to `:id`.
160
+
161
+ ```ruby
162
+ class PeopleSearcher
163
+ include SnFoil::Searcher
164
+
165
+ model Person
166
+ order_by :last_name
167
+ end
168
+ ```
169
+
170
+ #### distinct
171
+ Whether or not to call ActiveRecord's `.distinct` on the results of the query. Defaults to `true`.
172
+
173
+ ```ruby
174
+ class PeopleSearcher
175
+ include SnFoil::Searcher
176
+
177
+ model Person
178
+ distinct false
179
+ end
180
+ ```
181
+
182
+
183
+ #### includes
184
+ A shorthand for adding ActiveRecord's `.include` to the to the query.
185
+
186
+ ```ruby
187
+ class PeopleSearcher
188
+ include SnFoil::Searcher
189
+
190
+ model Person
191
+ includes :address,
192
+ { occupation: :employer }
193
+ end
194
+ ```
195
+
196
+
197
+ ## Author Notes
198
+
199
+ ### What happened to SSR controllers?
200
+
201
+ Honestly I never used them myself and never heard of anyone else using them. This framework was born out of frustration with REST APIs and the only reason SSR was implemented in the first place was a CYA move. Rather than keeping something in that I wasn't actively using, I decided to focus on what I was. If you are interested in bring SSR controllers back feel free to reach out and I can walk you through it!
202
+
203
+ ## Contributing
204
+ Contribution directions go here.
205
+
206
+ ## License
207
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,21 @@
1
+ Description:
2
+ Creates a Scaffold for SnFoil based around a model
3
+
4
+ Arguments:
5
+ model: *required* The name of the model
6
+
7
+ Options:
8
+ skip_model: Skip the creation of a model. default: false
9
+ type: The type of controller to create (base, api). default: base
10
+
11
+ Example:
12
+ rails generate snfoil:all User
13
+
14
+ This will create:
15
+ app/models/user.rb
16
+ app/policies/user_policy.rb
17
+ app/searchers/users_searcher.rb
18
+ app/contexts/user_context.rb
19
+ app/jsonapi_serializers/user_jsonapi_serializer.rb
20
+ app/jsonapi_deserializers/user_jsonapi_deserializer.rb
21
+ app/controllers/users_controller.rb
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2021 Matthew Howes
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module SnFoil
18
+ class AllGenerator < Rails::Generators::Base
19
+ source_root File.expand_path('templates', __dir__)
20
+
21
+ argument :model, type: :string
22
+
23
+ class_option(:type, desc: 'Generate Base or API', type: :string, default: 'base')
24
+ class_option(:skip_model, desc: 'Skip Model Creation', type: :boolean, default: false)
25
+
26
+ def add_model
27
+ rails_command "generate model #{call_args.join(' ')}", call_options
28
+ end
29
+
30
+ def add_policy
31
+ rails_command "generate snfoil:policy #{call_args.join(' ')}", call_options
32
+ end
33
+
34
+ def add_searcher
35
+ rails_command "generate snfoil:searcher #{call_args.join(' ')}", call_options
36
+ end
37
+
38
+ def add_context
39
+ rails_command "generate snfoil:context #{call_args.join(' ')}", call_options
40
+ end
41
+
42
+ def add_jsonapi_serializer
43
+ rails_command "generate snfoil:jsonapi_serializer #{call_args.join(' ')}", call_options
44
+ end
45
+
46
+ def add_jsonapi_deserializer
47
+ rails_command "generate snfoil:jsonapi_deserializer #{call_args.join(' ')}", call_options
48
+ end
49
+
50
+ def add_controller
51
+ rails_command "generate snfoil:controller #{call_args.join(' ')}", call_options
52
+ end
53
+
54
+ private
55
+
56
+ def call_args
57
+ @call_args ||= [model].concat(args)
58
+ end
59
+
60
+ def call_options
61
+ @call_options ||= options.deep_symbolize_keys
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,14 @@
1
+ Description:
2
+ Creates a SnFoil::Context Scaffold
3
+
4
+ Arguments:
5
+ model: *required* The name of the model
6
+
7
+ Options:
8
+ path: The path the files will be created at. default: app/contexts
9
+
10
+ Example:
11
+ rails generate snfoil:context User
12
+
13
+ This will create:
14
+ app/contexts/user_context
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2021 Matthew Howes
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module SnFoil
18
+ class ContextGenerator < Rails::Generators::Base
19
+ source_root File.expand_path('templates', __dir__)
20
+
21
+ argument :model, type: :string
22
+
23
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/contexts'
24
+
25
+ def add_app_file
26
+ file_name = if modules.length.zero?
27
+ name
28
+ else
29
+ "#{modules.join('/')}/#{name}"
30
+ end
31
+
32
+ template('context.erb', "#{options[:path]}/#{file_name}_context.rb")
33
+ end
34
+
35
+ private
36
+
37
+ def name
38
+ @name ||= model.split('/').last.underscore.singularize
39
+ end
40
+
41
+ def class_name
42
+ @class_name ||= name.camelize
43
+ end
44
+
45
+ def modules
46
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
47
+ end
48
+
49
+ def class_modules
50
+ return if modules.length.zero?
51
+
52
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
53
+ end
54
+ end
55
+ end