formify 0.22.1 → 0.22.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: 93fa0eb3fcb3e0606edd50934a3735d162c754a185e4c7370fd9cff62550557e
4
- data.tar.gz: 483bf4d0efe4fe84ac5e373440648593d0532abbeb2c21e8b2eee0c1e22944ad
3
+ metadata.gz: 8c9c91aa13403c65348f1652cbbc302b585217c07954344e62a59b8d312ad62c
4
+ data.tar.gz: 73c457fae48a9a848c6f830f247d26bf8ed37469aefb25b33153369f83f98c56
5
5
  SHA512:
6
- metadata.gz: 36eb88fae988cc25a0a49d1c810070efc63933260816d2d10c875b014e7e3d28f3d702aff819244d27cb22b7e83286edf0717ef5c8e0b8b405ccf0763f80005b
7
- data.tar.gz: 182fdd8af9058ee0625e0938477d58a9ae9d6d055f0b000c21f939059badfcc064e2213c20a18583eae4ea62bd3a0737e850c9e8a5c3b4b8dc5ef8dc317ceede
6
+ metadata.gz: 1adbc0904dda3e2275a67af0bd939232e0e23883c1d42d7097a3588c09af37532668b67128074da4a8c0b74b1b30ca2a2b32cea5a354c2eb3e82ea148817681d
7
+ data.tar.gz: f0eac5d0a2d4e2ed3eeea2ace303d9207f0da5ea31d551f66b38c092ff357b71a2b311d9010f4b77167fc87c62e30be479dffdfa561d8f2ddbc15893e1a56bdf
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- formify (0.22.1)
4
+ formify (0.22.2)
5
5
  rails
6
6
  resonad
7
7
  with_advisory_lock
@@ -1,3 +1,3 @@
1
1
  module Formify
2
- VERSION = '0.22.1'.freeze
2
+ VERSION = '0.22.2'.freeze
3
3
  end
@@ -7,9 +7,10 @@ class FormGenerator < Rails::Generators::NamedBase
7
7
  # check_class_collision
8
8
 
9
9
  argument :form_attributes,
10
+ default: [],
11
+ description: 'A list of attributes and their delegates: foo:bar,baz',
10
12
  type: :array,
11
- required: true,
12
- description: 'A list of attributes and their delegates: foo:bar,baz'
13
+ required: false
13
14
 
14
15
  class_option :pluralize_collection,
15
16
  default: true,
@@ -47,9 +48,11 @@ class FormGenerator < Rails::Generators::NamedBase
47
48
  private
48
49
 
49
50
  def all_attributes
50
- @all_attributes ||= flattened_form_attributes
51
- .map(&:strip)
52
- .sort
51
+ @all_attributes ||= begin
52
+ ret = flattened_form_attributes
53
+ .map(&:strip)
54
+ .sort
55
+ end
53
56
  end
54
57
 
55
58
  def attributes_with_delegates
@@ -76,14 +79,11 @@ class FormGenerator < Rails::Generators::NamedBase
76
79
  form == 'create'
77
80
  end
78
81
 
79
- def create?
80
- form == 'create'
81
- end
82
-
83
82
  def delegated_attributes
84
83
  @delegated_attributes ||= attributes_and_delegates
85
84
  .select { |_k, v| v.sort!.present? }
86
85
  end
86
+
87
87
  def destroy_attribute
88
88
  @destroy_attribute ||= begin
89
89
  if all_attributes.include?(collection.singularize)
@@ -113,7 +113,7 @@ class FormGenerator < Rails::Generators::NamedBase
113
113
 
114
114
  def factory_bot?
115
115
  @factory_bot ||= begin
116
- require 'factory-bot'
116
+ require 'factory_bot'
117
117
  FactoryBot.find_definitions
118
118
  Class.const_defined?('FactoryBot')
119
119
  end
@@ -181,10 +181,8 @@ class FormGenerator < Rails::Generators::NamedBase
181
181
  "update_#{collection.singularize}"
182
182
  elsif upsert?
183
183
  "upsert_#{collection.singularize}"
184
- elsif form.include?('_')
185
- form
186
184
  else
187
- "#{form}_#{collection.singularize}"
185
+ form
188
186
  end
189
187
  end
190
188
 
@@ -208,8 +206,8 @@ class FormGenerator < Rails::Generators::NamedBase
208
206
  @return_attribute ||= begin
209
207
  if all_attributes.include?(collection.singularize)
210
208
  collection.singularize
211
- else
212
- form_attributes.split(':').first
209
+ elsif !form_attributes.first.nil?
210
+ form_attributes.first.split(':').first
213
211
  end
214
212
  end
215
213
  end
@@ -3,29 +3,31 @@
3
3
  <% module_namespacing do -%>
4
4
  class <%= form_name %>
5
5
  include Formify::Form
6
-
6
+ <% if attributes_with_delegates.any? -%>
7
7
  attr_accessor :<%= attributes_with_delegates.first %><%= ',' if attributes_with_delegates.count > 1 %>
8
- <% attributes_with_delegates[1..-1].each_with_index do |attr, i| -%>
8
+ <% (attributes_with_delegates[1..-1] || []).each_with_index do |attr, i| -%>
9
9
  :<%= attr %><%= ',' unless attributes_with_delegates.count - 2 == i %>
10
10
  <% end -%>
11
+ <% end -%>
11
12
 
12
13
  <% delegated_attributes.each do |to_attr, delegates| -%>
13
14
  delegate_accessor :<%= delegates.first %>,
14
- <% delegates[1..-1].each_with_index do |attr, i| -%>
15
+ <% (delegates[1..-1] || []).each_with_index do |attr, i| -%>
15
16
  :<%= attr %>,
16
17
  <% end -%>
17
18
  to: :<%= to_attr %>
18
- <% end -%>
19
19
 
20
+ <% end -%>
20
21
  # before_validation :before_validation_do_something
21
-
22
+ <% if all_attributes.any? %>
22
23
  validates_presence_of :<%= all_attributes.first %><%= ',' if all_attributes.count > 1 %>
23
- <% all_attributes[1..-1].each_with_index do |attr, i| -%>
24
+ <% (all_attributes[1..-1] || []).each_with_index do |attr, i| -%>
24
25
  :<%= attr %><%= ',' unless all_attributes.count - 2 == i %>
26
+ <% end -%>
25
27
  <% end -%>
26
28
 
27
29
  # validate :validate_something
28
-
30
+ <% if all_attributes.any? %>
29
31
  initialize_with :<%= attributes_with_delegates.join(', :') %> do |attributes|
30
32
  <% if create? -%>
31
33
  self.<%= method_attribute %> ||= <%= inferred_model_name %>.new
@@ -39,12 +41,15 @@ class <%= form_name %>
39
41
  puts attributes
40
42
  <% end -%>
41
43
  end
44
+ <% end -%>
42
45
 
43
46
  def save
44
47
  with_advisory_lock_transaction(<%= lock_key.join(', ') %>) do
45
48
  validate_or_fail
46
49
  .and_then { <%= method_name %> }
50
+ <% unless return_attribute.nil? -%>
47
51
  .and_then { success(<%= return_attribute %>) }
52
+ <% end -%>
48
53
  end
49
54
  end
50
55
 
@@ -59,7 +64,11 @@ class <%= form_name %>
59
64
  raise NotImplementedError
60
65
 
61
66
  <% end -%>
67
+ <% if return_attribute.nil? -%>
68
+ success
69
+ <% else -%>
62
70
  success(<%= return_attribute %>)
71
+ <% end -%>
63
72
  end
64
73
 
65
74
  # def before_validation_do_something
@@ -6,6 +6,7 @@ require 'formify/spec_helpers'
6
6
  describe Forms::<%= class_name %>, type: :form do
7
7
  include Formify::SpecHelpers
8
8
 
9
+ <% if all_attributes.any? %>
9
10
  <% all_attributes.each do |attr| -%>
10
11
  <% if factory_bot? && factory?(attr) -%>
11
12
  let(:<%= attr %>) { FactoryBot.create(:<%= attr %>) }
@@ -14,11 +15,13 @@ describe Forms::<%= class_name %>, type: :form do
14
15
  <% end -%>
15
16
  <% end -%>
16
17
 
18
+ <% end -%>
17
19
  <% if spec_comments? -%>
18
20
  # :attributes is used to initialize the form.
19
21
  # These values should result in a valid form.
20
22
  # You can override these in blocks or use let(:attributes_override) { { foo: bar } }
21
23
  <% end -%>
24
+ <% if all_attributes.any? %>
22
25
  let(:attributes) do
23
26
  {
24
27
  <% all_attributes.each_with_index do |attr, i| -%>
@@ -27,11 +30,12 @@ describe Forms::<%= class_name %>, type: :form do
27
30
  }
28
31
  end
29
32
 
33
+ <% end -%>
30
34
  it { expect_valid }<% if spec_comments? -%> # Expect the form to be valid<% end %>
31
35
  it { expect(result).to be_success }
32
36
  it { expect(value).to be_a(<%= inferred_model_name %>) }<% if spec_comments? -%> # Model name inferred<% end %>
33
-
34
37
  <% all_attributes.each do |attr| -%>
38
+
35
39
  describe '#<%= attr %>' do
36
40
  it { expect_error_with_missing_attribute(:<%= attr %>) }
37
41
  xit { expect_error_with_attribute_value(:<%= attr %>, <%= attr.upcase %>_BAD_VALUE, message: nil) } # :message is optional
@@ -40,6 +44,7 @@ describe Forms::<%= class_name %>, type: :form do
40
44
 
41
45
  <% end -%>
42
46
  <% if spec_comments? -%>
47
+
43
48
  # Other Expectation Helpers
44
49
  # xit { expect_error_message(message) }
45
50
  # xit { expect_error_with_attribute(attribute) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.1
4
+ version: 0.22.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Jackson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-18 00:00:00.000000000 Z
11
+ date: 2019-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler