action_form 0.5.3 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30972262846891f251c02bb292dd4ba8043210edfd6c629ebf944a60eff8ae96
4
- data.tar.gz: 4a28bdcb8db7cf0ceefb406b310f12dcf4e123ea686ff0d939a7204adc5d857c
3
+ metadata.gz: 90a17e324048d0b41d707a234c6c5d3fc02dea5026f38b33f618120f075b2bf0
4
+ data.tar.gz: f0a8318df1e2ff413f5a03eaa9b11435978148156aad86fd62f917b42ea6f26a
5
5
  SHA512:
6
- metadata.gz: 3f243e0ea21d55689b99cb73a320bf78ecb4dc57d97f896ee6a9d44991d9fb08bfefefeb84fa1df44be078bb6636be59116fd822e65282fe459535f203311762
7
- data.tar.gz: af7ca8c553f37de8ffdbdb4febcd4b65aee352b3b2a19ad29155f59b15f13e244ef3cf3d0b84bdc2d09b903f7bc5dbdc19d8eacd0aa34736f48e3be6c2fb3add
6
+ metadata.gz: ab9cbd073ac3c1bd2999c5cbcbb36ac5c21acd6264747ceabfc326a822e1d6d0184524abd03d49928e1a82e9886f96300760ffc0494932df6c15aeb6276892c8
7
+ data.tar.gz: d45d1545e8e90a78f833a019b5e3248680fa1fd736bae6b4d9e4f060b6ed9c702f7be3ac94914e67867fc36259760b351cdd8432974f48c6abaa4f04b06fc7d4
data/README.md CHANGED
@@ -404,7 +404,7 @@ class UsersController < ApplicationController
404
404
  @user = User.create!(user_params.to_h)
405
405
  redirect_to @user
406
406
  else
407
- @form = @form.with_params(user_params)
407
+ @form = user_params.creare_form
408
408
  render :new
409
409
  end
410
410
  end
@@ -848,7 +848,7 @@ end
848
848
 
849
849
  **When to use each approach:**
850
850
  - **`owner_method_name`**: Recommended for delegation - automatically searches the ownership chain
851
- - **`owner.method_name`**: Use when you need direct access and the owner is guaranteed to be set, or when chaining methods with safe navigation (`owner.current_user&.admin?`)
851
+ - **`owner.method_name`**: Useful when different owners in the chain define the same method. By default, the nearest owner will handle the call. If you want to customize which owner is used, consider using `alias_method` or explicitly referencing a higher owner (e.g., `owner.owner.some_method`).
852
852
 
853
853
  #### **Ownership Hierarchy**
854
854
 
@@ -1628,7 +1628,7 @@ class UsersController < ApplicationController
1628
1628
  redirect_to @user
1629
1629
  else
1630
1630
  # Custom validation errors are automatically available
1631
- @form = @form.with_params(user_params)
1631
+ @form = user_params.create_form(action: request.path, method: request.method)
1632
1632
  render :new
1633
1633
  end
1634
1634
  end
@@ -1646,7 +1646,7 @@ class UsersController < ApplicationController
1646
1646
  redirect_to @user
1647
1647
  else
1648
1648
  # Custom validation errors (like password confirmation) are displayed
1649
- @form = @form.with_params(user_params)
1649
+ @form = user_params.create_form(action: request.path, method: request.method)
1650
1650
  render :edit
1651
1651
  end
1652
1652
  end
@@ -1675,7 +1675,7 @@ class UserForm < ActionForm::Rails::Base
1675
1675
  end
1676
1676
 
1677
1677
  # When validation fails:
1678
- @form = @form.with_params(invalid_params)
1678
+ @form = user_params.create_form
1679
1679
  # The form will automatically display validation errors
1680
1680
  ```
1681
1681
 
@@ -91,7 +91,7 @@ module ActionForm
91
91
  end
92
92
 
93
93
  def subform_value(name)
94
- @object.public_send(name)
94
+ @object&.public_send(name) || @params&.public_send(:"#{name}_attributes")
95
95
  end
96
96
 
97
97
  def build_subform(name, form_definition, value: subform_value(name), index: nil)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionForm
4
+ # Base class for parameter validation that is associated with form classes.
5
+ # Provides functionality to create form instances from validated parameters.
6
+ class Params < EasyParams::Base
7
+ class << self
8
+ attr_accessor :form_class
9
+
10
+ def inherited(subclass)
11
+ super
12
+ subclass.form_class = form_class
13
+ end
14
+ end
15
+
16
+ def create_form(scope: self.class.form_class.scope, **html_options)
17
+ self.owner = self.class.form_class.new(params: self, scope: scope, **html_options)
18
+ end
19
+ end
20
+ end
@@ -40,6 +40,7 @@ module ActionForm
40
40
  Class.new(params_class) { has scope, klass }
41
41
  end
42
42
  end
43
+ @params_definitions[scope].form_class = self
43
44
  @params_definitions[scope]
44
45
  end
45
46
 
@@ -62,10 +63,6 @@ module ActionForm
62
63
  end
63
64
  end
64
65
 
65
- def with_params(form_params)
66
- self.class.new(model: @namespaced_model, scope: @scope, params: form_params, owner: self, **html_options)
67
- end
68
-
69
66
  def params_definition(scope: self.scope)
70
67
  return super unless scope
71
68
 
@@ -111,7 +108,7 @@ module ActionForm
111
108
  end
112
109
 
113
110
  def http_method
114
- return "get" if @object.nil?
111
+ return html_options[:method].to_s.downcase if @object.nil?
115
112
 
116
113
  @object.persisted? ? "patch" : "post"
117
114
  end
@@ -121,7 +118,7 @@ module ActionForm
121
118
  end
122
119
 
123
120
  def html_method
124
- html_options[:method] = html_options[:method].to_s.downcase == "get" ? "get" : "post"
121
+ html_options[:method].to_s.downcase == "get" ? "get" : "post"
125
122
  end
126
123
  end
127
124
  end
@@ -8,7 +8,7 @@ module ActionForm
8
8
  # submit buttons and form elements.
9
9
  module Rendering
10
10
  def render_form(&block)
11
- form(**{ method: html_method, action: html_action, "accept-charset" => "UTF-8" }, **@html_options) do
11
+ form(**@html_options, **{ method: html_method, action: html_action, "accept-charset" => "UTF-8" }) do
12
12
  render_utf8_input
13
13
  render_authenticity_token
14
14
  render_method_input
@@ -10,7 +10,7 @@ module ActionForm
10
10
 
11
11
  module ClassMethods # rubocop:disable Style/Documentation
12
12
  def params_class
13
- EasyParams::Base
13
+ ActionForm::Params
14
14
  end
15
15
 
16
16
  def params_definition(*)
@@ -24,6 +24,7 @@ module ActionForm
24
24
 
25
25
  def create_params_definition # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
26
26
  schema = Class.new(params_class)
27
+ schema.form_class = self
27
28
  elements.each do |name, element_definition|
28
29
  if element_definition < ActionForm::SubformsCollection
29
30
  # nested forms are passed as a hash that looks like this:
@@ -55,6 +56,7 @@ module ActionForm
55
56
 
56
57
  def create_params_definition # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
57
58
  schema = Class.new(self.class.params_class)
59
+ schema.form_class = self.class
58
60
  elements_instances.select(&:render?).each do |element|
59
61
  case element
60
62
  when ActionForm::SubformsCollection
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionForm
4
- VERSION = "0.5.3"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/action_form.rb CHANGED
@@ -5,6 +5,7 @@ require "easy_params"
5
5
  require "forwardable"
6
6
  require_relative "action_form/version"
7
7
  require_relative "action_form/composition"
8
+ require_relative "action_form/params"
8
9
  require_relative "action_form/schema_dsl"
9
10
  require_relative "action_form/elements_dsl"
10
11
  require_relative "action_form/input"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '0.8'
18
+ version: '0.9'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '0.8'
25
+ version: '0.9'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: phlex
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -71,6 +71,7 @@ files:
71
71
  - lib/action_form/element.rb
72
72
  - lib/action_form/elements_dsl.rb
73
73
  - lib/action_form/input.rb
74
+ - lib/action_form/params.rb
74
75
  - lib/action_form/rails/base.rb
75
76
  - lib/action_form/rails/rendering.rb
76
77
  - lib/action_form/rails/subform.rb