rails_g_with_attributes 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8a9b29ae074f7e35598baa294f9248174f0c5dcf61cdfb7d669f01b86a0119bd
4
+ data.tar.gz: 27a9be246cf354c5d8b425eb2762f271295ae07ce5c2ce7b855bf4ee6dd61b8b
5
+ SHA512:
6
+ metadata.gz: caaff24517255c86c9f9b29e7e5275478f0e20842244cfeda59d68502c7bb1d41dcb278f9c469444f3d325698b77bae2b3ab4f41f5564caf130a5fad9eb6071b
7
+ data.tar.gz: f4cf61978349028fab1ec790f644d534c6506a8264477df0faf87f47a61e6ac324e0bb42402d29a896a4ae1cc1d5652edcdfc779abbabda5b3ea76c86386829f
@@ -0,0 +1 @@
1
+ require_relative 'with_attributes'
@@ -0,0 +1,48 @@
1
+ # @author: Ackshaey Singh
2
+ # @date: 2022-03-09
3
+ # @description: Patch to read model attributes if none specified on command line
4
+ # (and model exists) with the --with-attributes option.
5
+ # @usage: rails g scaffold_controller <MODEL> --with-attributes
6
+
7
+ require 'rails/generators'
8
+
9
+ Rails::Generators::NamedBase.class_eval do
10
+ protected
11
+
12
+ # Override to get model attributes if none specified on command line.
13
+ #
14
+ # Convert attributes array into GeneratedAttribute objects.
15
+ def parse_attributes!
16
+ if ARGV.include?('--with-attributes') && (!attributes || attributes.empty?)
17
+ self.attributes = gen_model_attributes
18
+ end
19
+ self.attributes = (attributes || []).map do |attr|
20
+ Rails::Generators::GeneratedAttribute.parse(attr)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ # Gets the attributes for a Rails model by querying the
27
+ # model's database table for column information. The method returns
28
+ # the attributes as an array of name:type strings, just like the
29
+ # `rails generate scaffold` command.
30
+ #
31
+ # @return [Array<String>] the attributes for the model
32
+ # @raise [StandardError] if there is a problem with the model
33
+ def gen_model_attributes
34
+ model = ARGV.find do |arg|
35
+ arg =~ /--model-name=/
36
+ end&.to_s&.split('=')&.last&.constantize || class_name.to_s.constantize
37
+
38
+ # Filter out the attributes we don't want to include
39
+ # (id and timestamps)
40
+ model.columns.reject { |a| a.name == 'id' || a.name.end_with?('_at') }.map do |a|
41
+ # Convert the attribute type to a string
42
+ type = model.type_for_attribute(a.name).to_s
43
+
44
+ # Return the attribute name and type as a string
45
+ "#{a.name}:#{type}"
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_g_with_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ackshaey Singh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ description: |-
28
+ code is a patch that modifies the rails g scaffold_controller generator to \
29
+ accept the --with_attributes option. The --with_attributes option is used to read model attributes if \
30
+ none are specified on the command line. This is a way to simplify the process of generating code by automatically \
31
+ including all of the attributes of the model.
32
+ email:
33
+ - ackshaey@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/rails_g_with_attributes.rb
39
+ - lib/with_attributes.rb
40
+ homepage: ''
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.2.15
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Patches rails g to accept the --with_attributes options, used for the \ `rails
63
+ g scaffold_controller` generator
64
+ test_files: []