rails-code-generator 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1787e88d10c2e47cbe9fdafa9ec47b61cb8dde03907191fcc5344f186f69cf9c
4
- data.tar.gz: 8bdfd7f36fd83cffdf6c57d7ecf35f345f7bac31434528a50b4935338b8ae2cb
3
+ metadata.gz: 3e0a15856855079249ca288614458381ad6f14bf2a82da4a993fbf77a0581562
4
+ data.tar.gz: 5d2c2227de1cc9a03d4eb5de84a378f0dd4579433dde1cb78186481af6bcc807
5
5
  SHA512:
6
- metadata.gz: a0181136e1aa0097fe93fdcc0c54931dd0b058f2a8e264184e6b877c9714d2cec6650231756daebfffe2a2a912b62aaeabc508782f550972e57856d906b15c02
7
- data.tar.gz: f1036a08400cfe4b7d522dd9fd4bf600d5f783b120c04306e0b75b7ddeea8482652ee72f49afaaf60033491f508ae5a0e9bb7bb1570fd76bdc0bbdb1b9546afa
6
+ metadata.gz: 1f05b2b9aad986e3762bd1bdd5b10ff1189ca9dbb0677703f3c8aa24bdd5d788ad03a721a54bb420b9d8a8b580888f768f6ff8562789f0b40b49851f2ca4fbd1
7
+ data.tar.gz: 62c9137dc2f4b00eaea6b56fa6d04feba68b7e153aa7f845b096e562d6c2d175bc167f9077d14fc0859e74f2caf24381bbe02188e8f51e66e2c92bdd814d85b5
@@ -10,7 +10,7 @@ RSpec.describe "[POST] /<%= route_path %>", type: :request do
10
10
  Given(:params) {
11
11
  {
12
12
  <%= table_name %>: {
13
- # TODO: add attributes
13
+ <%= request_params_attributes_list %>
14
14
  },
15
15
  }.to_json
16
16
  }
@@ -11,7 +11,7 @@ RSpec.describe "[PATCH] /<%= route_path %>/:id", type: :request do
11
11
  Given(:params) {
12
12
  {
13
13
  <%= table_name %>: {
14
- name: "Changed <%= singular_name %> name",
14
+ <%= request_params_attributes_list %>
15
15
  },
16
16
  }.to_json
17
17
  }
@@ -21,6 +21,5 @@ RSpec.describe "[PATCH] /<%= route_path %>/:id", type: :request do
21
21
  Then { expect(response).to be_successful }
22
22
  And { expect(response_body[:data]).not_to be_empty }
23
23
  And { expect(response_body[:errors]).to be_empty }
24
- And { expect(response_body.dig(:data, :name)).to eq "Changed <%= singular_name %> name" }
25
24
  end
26
25
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  class <%= resource_normalizer_class %> < BaseResponseNormalizer
4
4
  attributes(
5
- <%= normalizer_attributes_list %>
5
+ <%= normalizer_attributes_list(include_id: true) %>
6
6
  )
7
7
  end
@@ -10,7 +10,7 @@ RSpec.describe "[POST] /<%= route_path %>", type: :request do
10
10
  Given(:params) {
11
11
  {
12
12
  <%= singular_name %>: {
13
- # TODO: add attributes
13
+ <%= request_params_attributes_list %>
14
14
  },
15
15
  }.to_json
16
16
  }
@@ -11,7 +11,7 @@ RSpec.describe "[PATCH] /<%= route_path %>/:id", type: :request do
11
11
  Given(:params) {
12
12
  {
13
13
  <%= singular_name %>: {
14
- name: "Changed <%= singular_name %> name",
14
+ <%= request_params_attributes_list %>
15
15
  },
16
16
  }.to_json
17
17
  }
@@ -21,6 +21,5 @@ RSpec.describe "[PATCH] /<%= route_path %>/:id", type: :request do
21
21
  Then { expect(response).to be_successful }
22
22
  And { expect(response_body[:data]).not_to be_empty }
23
23
  And { expect(response_body[:errors]).to be_empty }
24
- And { expect(response_body.dig(:data, :name)).to eq "Changed <%= singular_name %> name" }
25
24
  end
26
25
  end
@@ -24,6 +24,24 @@ module Rails
24
24
  "uuid" => :string,
25
25
  }.freeze
26
26
 
27
+ FAKER_EXPRESSION_MAP = {
28
+ "string" => "Faker::Lorem.word",
29
+ "text" => "Faker::Lorem.paragraph",
30
+ "citext" => "Faker::Lorem.word",
31
+ "integer" => "Faker::Number.number(digits: 5)",
32
+ "bigint" => "Faker::Number.number(digits: 5)",
33
+ "float" => "Faker::Number.decimal(l_digits: 2).to_f",
34
+ "decimal" => "Faker::Number.decimal(l_digits: 2)",
35
+ "boolean" => "Faker::Boolean.boolean",
36
+ "date" => "Faker::Date.forward(days: 30)",
37
+ "datetime" => "Faker::Time.forward(days: 30)",
38
+ "timestamp" => "Faker::Time.forward(days: 30)",
39
+ "timestamptz" => "Faker::Time.forward(days: 30)",
40
+ "json" => "{}",
41
+ "jsonb" => "{}",
42
+ "uuid" => "Faker::Internet.uuid",
43
+ }.freeze
44
+
27
45
  def model_attributes
28
46
  @model_attributes ||= fetch_model_attributes
29
47
  end
@@ -34,8 +52,9 @@ module Rails
34
52
  format_symbol_list(names)
35
53
  end
36
54
 
37
- def normalizer_attributes_list
55
+ def normalizer_attributes_list(include_id: false)
38
56
  names = model_attribute_names
57
+ names = [:id, *names] if include_id
39
58
  return " # TODO: add attributes" if names.empty?
40
59
 
41
60
  format_symbol_list(names, indent: 4)
@@ -47,6 +66,13 @@ module Rails
47
66
  model_attributes.map { |attribute| validator_line_for(attribute) }.join("\n")
48
67
  end
49
68
 
69
+ def request_params_attributes_list(indent: 10)
70
+ padding = " " * indent
71
+ return "#{padding}# TODO: add attributes" if model_attributes.empty?
72
+
73
+ model_attributes.map { |attribute| "#{padding}#{attribute[:name]}: #{faker_expression_for(attribute)}," }.join("\n")
74
+ end
75
+
50
76
  private
51
77
 
52
78
  def model_attribute_names
@@ -69,6 +95,10 @@ module Rails
69
95
  end
70
96
  end
71
97
 
98
+ def faker_expression_for(attribute)
99
+ FAKER_EXPRESSION_MAP.fetch(attribute[:type], "Faker::Lorem.word")
100
+ end
101
+
72
102
  def fetch_model_attributes
73
103
  klass = class_name.constantize
74
104
  return [] unless klass.respond_to?(:columns)
@@ -76,9 +106,12 @@ module Rails
76
106
  klass.columns.filter_map do |column|
77
107
  next if EXCLUDED_COLUMNS.include?(column.name)
78
108
 
109
+ type = column.type.to_s
110
+
79
111
  {
80
112
  name: column.name,
81
- dry_type: DRY_TYPE_MAP[column.type.to_s],
113
+ type: type,
114
+ dry_type: DRY_TYPE_MAP[type],
82
115
  }
83
116
  end
84
117
  rescue StandardError
@@ -3,7 +3,7 @@
3
3
  module Rails
4
4
  module Code
5
5
  module Generator
6
- VERSION = "0.1.1"
6
+ VERSION = "0.1.2"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-code-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sweat