aws-record-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +4 -0
  3. data/LICENSE +175 -0
  4. data/VERSION +1 -0
  5. data/lib/aws-record-rails.rb +28 -0
  6. data/lib/generators/aws_record/active_model.rb +68 -0
  7. data/lib/generators/aws_record/base.rb +212 -0
  8. data/lib/generators/aws_record/erb/erb_generator.rb +55 -0
  9. data/lib/generators/aws_record/erb/templates/_form.html.erb.tt +34 -0
  10. data/lib/generators/aws_record/erb/templates/edit.html.erb.tt +6 -0
  11. data/lib/generators/aws_record/erb/templates/index.html.erb.tt +31 -0
  12. data/lib/generators/aws_record/erb/templates/new.html.erb.tt +5 -0
  13. data/lib/generators/aws_record/erb/templates/show.html.erb.tt +11 -0
  14. data/lib/generators/aws_record/model/USAGE +24 -0
  15. data/lib/generators/aws_record/model/model_generator.rb +25 -0
  16. data/lib/generators/aws_record/model/templates/model.rb.tt +76 -0
  17. data/lib/generators/aws_record/model/templates/table_config.rb.tt +20 -0
  18. data/lib/generators/aws_record/resource/USAGE +23 -0
  19. data/lib/generators/aws_record/resource/resource_generator.rb +21 -0
  20. data/lib/generators/aws_record/scaffold/USAGE +31 -0
  21. data/lib/generators/aws_record/scaffold/scaffold_generator.rb +46 -0
  22. data/lib/generators/aws_record/scaffold/templates/scaffold.css +80 -0
  23. data/lib/generators/aws_record/scaffold_controller/USAGE +15 -0
  24. data/lib/generators/aws_record/scaffold_controller/scaffold_controller_generator.rb +51 -0
  25. data/lib/generators/aws_record/scaffold_controller/templates/api_controller.rb.tt +63 -0
  26. data/lib/generators/aws_record/scaffold_controller/templates/controller.rb.tt +70 -0
  27. data/lib/generators/generated_attribute.rb +139 -0
  28. data/lib/generators/secondary_index.rb +67 -0
  29. data/lib/tasks/aws_record/migrate.rake +14 -0
  30. metadata +99 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5559912f4320e4963f91af1adb907079abd0c1983ab1a517bc5ccac1456c8b92
4
+ data.tar.gz: 58507fa8d3d7ec5671765dbcb962f9d45cfc3181601214786b2d1d7f56a47a2f
5
+ SHA512:
6
+ metadata.gz: ff8d42eebfd7b9e309f6809c6c50989ba92ef93cf8f33012b9135e151956eaa85f5783c865707530f365a60bca4560dc5de95ab8fdc9310138d61c68de60027e
7
+ data.tar.gz: 84d2e7097f486cca737fe4fb7584010fabe3103da33726c4120dedb29bb0469567ddbe32032f35bce379cefd40966a059d95929eb8b1d4fa93cbdccf32db9c55
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ 0.1.0 (2024-11-16)
2
+ ------------------
3
+
4
+ * Feature - Initial version of this gem.
data/LICENSE ADDED
@@ -0,0 +1,175 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'generators/generated_attribute'
4
+ require_relative 'generators/secondary_index'
5
+ require_relative 'generators/aws_record/base'
6
+
7
+ require_relative 'generators/aws_record/model/model_generator'
8
+ require_relative 'generators/aws_record/resource/resource_generator'
9
+ require_relative 'generators/aws_record/erb/erb_generator'
10
+ require_relative 'generators/aws_record/scaffold/scaffold_generator'
11
+ require_relative 'generators/aws_record/scaffold_controller/scaffold_controller_generator'
12
+
13
+ require 'aws-record'
14
+
15
+ module Aws
16
+ module Record
17
+ module Rails
18
+ VERSION = File.read(File.expand_path('../VERSION', __dir__)).strip
19
+
20
+ # @api private
21
+ class Railtie < ::Rails::Railtie
22
+ rake_tasks do
23
+ load 'tasks/aws_record/migrate.rake'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsRecord
4
+ module Generators
5
+ class ActiveModel
6
+ attr_reader :name
7
+
8
+ def initialize(name)
9
+ @name = name
10
+ end
11
+
12
+ # GET index
13
+ def self.all(klass)
14
+ "#{klass}.scan"
15
+ end
16
+
17
+ # GET show
18
+ # GET edit
19
+ # PATCH/PUT update
20
+ # DELETE destroy
21
+ def self.find(klass, attrs)
22
+ hkey = attrs.select { |attr| attr.options[:hash_key] }[0]
23
+ rkey = attrs.select { |attr| attr.options[:range_key] }
24
+ rkey = rkey.empty? ? nil : rkey[0]
25
+
26
+ if rkey
27
+ "lambda {
28
+ id = params[:id].split('&').map{ |param| CGI.unescape(param) }
29
+ #{klass}.find(#{hkey.name}: id[0], #{rkey.name}: id[1])
30
+ }.call()"
31
+ else
32
+ "#{klass}.find(#{hkey.name}: CGI.unescape(params[:id]))"
33
+ end
34
+ end
35
+
36
+ # GET new
37
+ # POST create
38
+ def self.build(klass, params = nil)
39
+ if params
40
+ "#{klass}.new(#{params})"
41
+ else
42
+ "#{klass}.new"
43
+ end
44
+ end
45
+
46
+ # POST create
47
+ def save
48
+ "#{name}.save"
49
+ end
50
+
51
+ # PATCH/PUT update
52
+ def update(params = nil)
53
+ "#{name}.update(#{params})"
54
+ end
55
+
56
+ # POST create
57
+ # PATCH/PUT update
58
+ def errors
59
+ "#{name}.errors"
60
+ end
61
+
62
+ # DELETE destroy
63
+ def destroy
64
+ "#{name}.delete!"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,212 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module AwsRecord
6
+ module Generators
7
+ class Base < Rails::Generators::NamedBase
8
+ argument :attributes, type: :array, default: [], banner: 'field[:type][:opts]...',
9
+ desc: 'Describes the fields in the model'
10
+ check_class_collision
11
+
12
+ class_option :disable_mutation_tracking, type: :boolean, desc: 'Disables dirty tracking'
13
+ class_option :timestamps, type: :boolean, desc: 'Adds created, updated timestamps to the model'
14
+ class_option :table_config, type: :hash, default: {}, banner: 'primary:R-W [SecondaryIndex1:R-W]...',
15
+ desc: 'Declares the r/w units for the model as well as any secondary indexes', required: true
16
+ class_option :gsi, type: :array, default: [],
17
+ banner: 'name:hkey{field_name}[,rkey{field_name},proj_type{ALL|KEYS_ONLY|INCLUDE}]...', desc: 'Allows for the declaration of secondary indexes'
18
+ class_option :table_name, type: :string, banner: 'model_table_name'
19
+ class_option :password_digest, type: :boolean, desc: 'Whether to add a password_digest field to the model'
20
+
21
+ class_option :required, type: :array, default: [], banner: 'field1...',
22
+ desc: 'A list of attributes that are required for an instance of the model'
23
+ class_option :length_validations, type: :hash, default: {}, banner: 'field1:MIN-MAX...',
24
+ desc: 'Validations on the length of attributes in a model'
25
+ class_option :scaffold, type: :boolean, desc: 'Adds helper methods that scaffolding uses'
26
+
27
+ attr_accessor :primary_read_units, :primary_write_units, :gsi_rw_units, :gsis, :required_attrs,
28
+ :length_validations
29
+
30
+ private
31
+
32
+ def initialize(args, *options)
33
+ options[0] << '--skip-table-config' if options[1][:behavior] == :revoke
34
+ @parse_errors = []
35
+
36
+ super
37
+ ensure_unique_fields
38
+ ensure_hkey
39
+ parse_gsis!
40
+ parse_table_config!
41
+ parse_validations!
42
+
43
+ return if @parse_errors.empty?
44
+
45
+ warn 'The following errors were encountered while trying to parse the given attributes'
46
+ $stderr.puts
47
+ warn @parse_errors
48
+ $stderr.puts
49
+
50
+ abort('Please fix the errors before proceeding.')
51
+ end
52
+
53
+ def parse_attributes!
54
+ self.attributes = (attributes || []).map do |attr|
55
+ GeneratedAttribute.parse(attr)
56
+ rescue ArgumentError => e
57
+ @parse_errors << e
58
+ next
59
+ end
60
+ self.attributes = attributes.compact
61
+
62
+ if options['password_digest']
63
+ attributes << GeneratedAttribute.new('password_digest', :string_attr, digest: true)
64
+ end
65
+
66
+ return unless options['timestamps']
67
+
68
+ attributes << GeneratedAttribute.parse('created:datetime:default_value{Time.now}')
69
+ attributes << GeneratedAttribute.parse('updated:datetime:default_value{Time.now}')
70
+ end
71
+
72
+ def ensure_unique_fields
73
+ used_names = Set.new
74
+ duplicate_fields = []
75
+
76
+ attributes.each do |attr|
77
+ duplicate_fields << [:attribute, attr.name] if used_names.include? attr.name
78
+ used_names.add attr.name
79
+
80
+ next unless attr.options.key? :database_attribute_name
81
+
82
+ raw_db_attr_name = attr.options[:database_attribute_name].delete('"') # db attribute names are wrapped with " to make template generation easier
83
+
84
+ duplicate_fields << [:database_attribute_name, raw_db_attr_name] if used_names.include? raw_db_attr_name
85
+
86
+ used_names.add raw_db_attr_name
87
+ end
88
+
89
+ return if duplicate_fields.empty?
90
+
91
+ duplicate_fields.each do |invalid_attr|
92
+ @parse_errors << ArgumentError.new("Found duplicated field name: #{invalid_attr[1]}, in attribute#{invalid_attr[0]}")
93
+ end
94
+ end
95
+
96
+ def ensure_hkey
97
+ uuid_member = nil
98
+ hkey_member = nil
99
+ rkey_member = nil
100
+
101
+ attributes.each do |attr|
102
+ if attr.options.key? :hash_key
103
+ if hkey_member
104
+ @parse_errors << ArgumentError.new("Redefinition of hash_key attr: #{attr.name}, original declaration of hash_key on: #{hkey_member.name}")
105
+ next
106
+ end
107
+
108
+ hkey_member = attr
109
+ elsif attr.options.key? :range_key
110
+ if rkey_member
111
+ @parse_errors << ArgumentError.new("Redefinition of range_key attr: #{attr.name}, original declaration of range_key on: #{hkey_member.name}")
112
+ next
113
+ end
114
+
115
+ rkey_member = attr
116
+ end
117
+
118
+ uuid_member = attr if attr.name.include? 'uuid'
119
+ end
120
+
121
+ return if hkey_member
122
+
123
+ if uuid_member
124
+ uuid_member.options[:hash_key] = true
125
+ else
126
+ attributes.unshift GeneratedAttribute.parse('uuid:hkey')
127
+ end
128
+ end
129
+
130
+ def mutation_tracking_disabled?
131
+ options['disable_mutation_tracking']
132
+ end
133
+
134
+ def has_validations?
135
+ !@required_attrs.empty? || !@length_validations.empty?
136
+ end
137
+
138
+ def parse_table_config!
139
+ return unless options['table_config']
140
+
141
+ @primary_read_units, @primary_write_units = parse_rw_units('primary')
142
+
143
+ @gsi_rw_units = @gsis.to_h do |idx|
144
+ [idx.name, parse_rw_units(idx.name)]
145
+ end
146
+
147
+ options['table_config'].each_key do |config|
148
+ next if config == 'primary'
149
+
150
+ gsi = @gsis.select { |idx| idx.name == config }
151
+
152
+ @parse_errors << ArgumentError.new("Could not find a gsi declaration for #{config}") if gsi.empty?
153
+ end
154
+ end
155
+
156
+ def parse_rw_units(name)
157
+ if options['table_config'].key? name
158
+ rw_units = options['table_config'][name]
159
+ rw_units.gsub(/[,.-]/, ':').split(':').reject(&:empty?)
160
+ else
161
+ @parse_errors << ArgumentError.new("Please provide a table_config definition for #{name}")
162
+ end
163
+ end
164
+
165
+ def parse_gsis!
166
+ @gsis = (options['gsi'] || []).map do |raw_idx|
167
+ idx = SecondaryIndex.parse(raw_idx)
168
+
169
+ attributes = self.attributes.select { |attr| attr.name == idx.hash_key }
170
+ if attributes.empty?
171
+ @parse_errors << ArgumentError.new("Could not find attribute #{idx.hash_key} for gsi #{idx.name} hkey")
172
+ next
173
+ end
174
+
175
+ if idx.range_key
176
+ attributes = self.attributes.select { |attr| attr.name == idx.range_key }
177
+ if attributes.empty?
178
+ @parse_errors << ArgumentError.new("Could not find attribute #{idx.range_key} for gsi #{idx.name} rkey")
179
+ next
180
+ end
181
+ end
182
+
183
+ idx
184
+ rescue ArgumentError => e
185
+ @parse_errors << e
186
+ next
187
+ end
188
+
189
+ @gsis = @gsis.compact
190
+ end
191
+
192
+ def parse_validations!
193
+ @required_attrs = options['required']
194
+ @required_attrs.each do |val_attr|
195
+ @parse_errors << ArgumentError.new("No such field #{val_attr} in required validations") if attributes.none? do |attr|
196
+ attr.name == val_attr
197
+ end
198
+ end
199
+
200
+ @length_validations = options['length_validations'].map do |val_attr, bounds|
201
+ @parse_errors << ArgumentError.new("No such field #{val_attr} in required validations") if attributes.none? do |attr|
202
+ attr.name == val_attr
203
+ end
204
+
205
+ bounds = bounds.gsub(/[,.-]/, ':').split(':').reject(&:empty?)
206
+ [val_attr, "#{bounds[0]}..#{bounds[1]}"]
207
+ end
208
+ @length_validations = @length_validations.to_h
209
+ end
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/erb'
4
+ require 'rails/generators/resource_helpers'
5
+
6
+ module AwsRecord
7
+ module Generators
8
+ class ErbGenerator < Base
9
+ include Rails::Generators::ResourceHelpers
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ argument :attributes, type: :array, default: [], banner: 'field:type field:type'
13
+
14
+ def initialize(args, *options)
15
+ options[0] << '--skip-table-config'
16
+ super
17
+ end
18
+
19
+ def create_root_folder
20
+ empty_directory File.join('app/views', controller_file_path)
21
+ end
22
+
23
+ def copy_view_files
24
+ available_views.each do |view|
25
+ formats.each do |format|
26
+ filename = filename_with_extensions(view, format)
27
+ template filename, File.join('app/views', controller_file_path, filename)
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def available_views
35
+ %w[index edit show new _form]
36
+ end
37
+
38
+ def formats
39
+ [format]
40
+ end
41
+
42
+ def format
43
+ :html
44
+ end
45
+
46
+ def handler
47
+ :erb
48
+ end
49
+
50
+ def filename_with_extensions(name, file_format = format)
51
+ [name, file_format, handler].compact.join('.')
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,34 @@
1
+ <%%= form_with(model: <%= model_resource_name %>, local: true) do |form| %>
2
+ <%% if <%= singular_table_name %>.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
5
+
6
+ <ul>
7
+ <%% <%= singular_table_name %>.errors.full_messages.each do |message| %>
8
+ <li><%%= message %></li>
9
+ <%% end %>
10
+ </ul>
11
+ </div>
12
+ <%% end %>
13
+
14
+ <% attributes.each do |attribute| -%>
15
+ <div class="field">
16
+ <% if attribute.password_digest? -%>
17
+ <%%= form.label :password %>
18
+ <%%= form.password_field :password %>
19
+ </div>
20
+
21
+ <div class="field">
22
+ <%%= form.label :password_confirmation %>
23
+ <%%= form.password_field :password_confirmation %>
24
+ <% else -%>
25
+ <%%= form.label :<%= attribute.column_name %> %>
26
+ <%%= form.<%= attribute.field_type %> :<%= attribute.column_name %> %>
27
+ <% end -%>
28
+ </div>
29
+
30
+ <% end -%>
31
+ <div class="actions">
32
+ <%%= form.submit %>
33
+ </div>
34
+ <%% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing <%= singular_table_name.titleize %></h1>
2
+
3
+ <%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
4
+
5
+ <%%= link_to 'Show', @<%= singular_table_name %> %> |
6
+ <%%= link_to 'Back', <%= index_helper %>_path %>
@@ -0,0 +1,31 @@
1
+ <p id="notice"><%%= notice %></p>
2
+
3
+ <h1><%= plural_table_name.titleize %></h1>
4
+
5
+ <table>
6
+ <thead>
7
+ <tr>
8
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
9
+ <th><%= attribute.human_name %></th>
10
+ <% end -%>
11
+ <th colspan="3"></th>
12
+ </tr>
13
+ </thead>
14
+
15
+ <tbody>
16
+ <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
17
+ <tr>
18
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
19
+ <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
20
+ <% end -%>
21
+ <td><%%= link_to 'Show', <%= model_resource_name %> %></td>
22
+ <td><%%= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>) %></td>
23
+ <td><%%= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
24
+ </tr>
25
+ <%% end %>
26
+ </tbody>
27
+ </table>
28
+
29
+ <br>
30
+
31
+ <%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New <%= singular_table_name.titleize %></h1>
2
+
3
+ <%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
4
+
5
+ <%%= link_to 'Back', <%= index_helper %>_path %>
@@ -0,0 +1,11 @@
1
+ <p id="notice"><%%= notice %></p>
2
+
3
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
4
+ <p>
5
+ <strong><%= attribute.human_name %>:</strong>
6
+ <%%= @<%= singular_table_name %>.<%= attribute.name %> %>
7
+ </p>
8
+
9
+ <% end -%>
10
+ <%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> |
11
+ <%%= link_to 'Back', <%= index_helper %>_path %>
@@ -0,0 +1,24 @@
1
+ Description:
2
+ rails generator for aws-record models
3
+
4
+ Pass the name of the model (preferably in singular form), and an optional list of attributes
5
+
6
+ Attributes are declarations of the fields that you wish to store within a model. You can pass
7
+ a type and list of options for each attribtue in the form: `name:type:options` if you do not provide
8
+ a type, it is assumed that the attribute is of type `string_attr`
9
+
10
+ Each model should have an hkey, if one is not present a `uuid:hkey` will be created for you.
11
+
12
+ Timestamps are not added by default but you can add them using the `--timestamps` flag
13
+ More information can be found at: https://github.com/awslabs/aws-record-generator/blob/master/README.md
14
+
15
+ You don't have to think up every attribute up front, but it helps to
16
+ sketch out a few so you can start working with the resource immediately.
17
+
18
+ Example:
19
+ rails generate aws_record:model Forum forum_uuid:hkey post_id:rkey post_title post_body tags:sset:default_value{Set.new} created_at:datetime:d_attr_name{PostCreatedAtTime} moderation:boolean:default_value{false}
20
+
21
+ This will create:
22
+ app/models/forum.rb
23
+ db/table_config/forum_config.rb
24
+ lib/tasks/table_config_migrate_task.rake # This is created once the first time the generator is run