Skip to content

Commit ce80802

Browse files
improve fields
1 parent 2e29923 commit ce80802

20 files changed

+195
-130
lines changed

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PATH
33
specs:
44
plutonium (0.1.0)
55
active_interaction (~> 5.3)
6+
pagy (~> 6.2.0)
67
pundit (~> 2.3.1)
78
rails (>= 7.1.2)
89
ransack (~> 4.1.1)
@@ -138,6 +139,7 @@ GEM
138139
racc (~> 1.4)
139140
nokogiri (1.16.0-x86_64-darwin)
140141
racc (~> 1.4)
142+
pagy (6.2.0)
141143
parallel (1.24.0)
142144
parser (3.2.2.4)
143145
ast (~> 2.4.1)

app/views/resource/_form.html.erb

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<% end %>
2626
</div>
2727
<div class="form-actions">
28+
<%= f.button :button, class: "btn btn-outline-secondary", formaction: "", formmethod: :get %>
2829
<%= f.button :submit, class: "btn btn-outline-primary" %>
2930
</div>
3031
</div>

lib/plutonium/core/controllers/presentable.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def resource_presenter(resource_class)
1111
end
1212

1313
def current_presenter
14-
@current_presenter ||= resource_presenter resource_class
14+
# @current_presenter ||=
15+
resource_presenter resource_class
1516
end
1617

1718
def build_collection

lib/plutonium/core/fields.rb

-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ module Fields
44
extend ActiveSupport::Autoload
55

66
eager_autoload do
7-
autoload :Input
87
autoload :Inputs
9-
autoload :Renderer
108
autoload :Renderers
119
end
1210
end

lib/plutonium/core/fields/input.rb

-67
This file was deleted.

lib/plutonium/core/fields/inputs.rb

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,44 @@ module Fields
44
module Inputs
55
extend ActiveSupport::Autoload
66

7+
def self.build(name, type:, **)
8+
# type ||= :slim_select if options.key? :collection
9+
10+
case type
11+
when :belongs_to
12+
Plutonium::Core::Fields::Inputs::BelongsToInput.new(name, **)
13+
when :has_one
14+
Plutonium::Core::Fields::Inputs::NoopInput.new
15+
when :has_many
16+
Plutonium::Core::Fields::Inputs::HasManyInput.new(name, **)
17+
else
18+
Plutonium::Core::Fields::Inputs::BasicInput.new(name, **)
19+
end
20+
end
21+
22+
def self.infer_for_resource_attribute(resource_class, attr_name, **options)
23+
type = nil
24+
25+
if (reflection = resource_class.try(:reflect_on_association, attr_name))
26+
type = reflection.macro
27+
options[:reflection] = reflection
28+
elsif (attachment = resource_class.try(:reflect_on_association, :"#{attr_name}_attachment"))
29+
type = :attachment
30+
options[:multiple] = attachment.macro == :has_many if options[:multiple].nil?
31+
elsif (column = resource_class.try(:column_for_attribute, attr_name))
32+
type = column.type
33+
options[:multiple] = column.try(:array?) if options[:multiple].nil?
34+
end
35+
36+
build(attr_name, type:, **options)
37+
end
38+
739
eager_autoload do
8-
autoload :BasicInput
940
autoload :AssociationInput
41+
autoload :BasicInput
42+
autoload :BelongsToInput
43+
autoload :HasManyInput
44+
autoload :NoopInput
1045
end
1146
end
1247
end

lib/plutonium/core/fields/inputs/association_input.rb

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ module Plutonium
22
module Core
33
module Fields
44
module Inputs
5-
class AssociationInput < Plutonium::Core::Fields::Input
5+
class AssociationInput < BasicInput
6+
attr_reader :reflection
7+
8+
def initialize(name, reflection:, **user_options)
9+
@reflection = reflection
10+
super(name, **user_options)
11+
end
12+
613
def render(f, record)
714
f.association name, **options
815
end
916

1017
private
1118

12-
def reflection = resource_class.reflect_on_association(name)
13-
1419
def param
15-
case reflection.macro
16-
when :belongs_to
17-
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
18-
when :has_one
19-
raise ArgumentError, ":has_one associations are not currently supported"
20-
else
21-
:"#{reflection.name.to_s.singularize}_ids"
22-
end
20+
raise NotImplementedError
2321
end
2422
end
2523
end

lib/plutonium/core/fields/inputs/basic_input.rb

+32-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,39 @@ module Plutonium
22
module Core
33
module Fields
44
module Inputs
5-
class BasicInput < Plutonium::Core::Fields::Input
6-
def render(f, record)
7-
f.input name, **options
5+
class BasicInput
6+
attr_reader :name, :user_options
7+
8+
def initialize(name, **user_options)
9+
@name = name
10+
@user_options = user_options
811
end
12+
13+
def render(f, record) = f.input name, **options
14+
15+
def collect(params)
16+
# Handles multi parameter attributes
17+
# https://www.cookieshq.co.uk/posts/rails-spelunking-date-select
18+
# https://www.cookieshq.co.uk/posts/multiparameter-attributes
19+
20+
# Matches
21+
# - parameter
22+
# - parameter(1)
23+
# - parameter(2)
24+
# - parameter(1i)
25+
# - parameter(2f)
26+
regex = /^#{param}(\(\d+[if]?\))?$/
27+
28+
params.select { |key| regex.match? key }
29+
end
30+
31+
protected
32+
33+
def input_options = {}
34+
35+
def param = name
36+
37+
def options = @options ||= input_options.deep_merge(@user_options)
938
end
1039
end
1140
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Plutonium
2+
module Core
3+
module Fields
4+
module Inputs
5+
class BelongsToInput < AssociationInput
6+
private
7+
8+
def param
9+
(reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id"
10+
end
11+
end
12+
end
13+
end
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Plutonium
2+
module Core
3+
module Fields
4+
module Inputs
5+
class HasManyInput < AssociationInput
6+
private
7+
8+
def param
9+
:"#{reflection.name.to_s.singularize}_ids"
10+
end
11+
end
12+
end
13+
end
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Plutonium
2+
module Core
3+
module Fields
4+
module Inputs
5+
class NoopInput
6+
def render(f, record)
7+
end
8+
9+
def collect(params)
10+
{}
11+
end
12+
end
13+
end
14+
end
15+
end
16+
end

lib/plutonium/core/fields/renderer.rb

-28
This file was deleted.

lib/plutonium/core/fields/renderers.rb

+29
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,37 @@ module Fields
44
module Renderers
55
extend ActiveSupport::Autoload
66

7+
def self.build(name, type:, **)
8+
# type ||= :slim_select if options.key? :collection
9+
10+
case type
11+
when :belongs_to
12+
when :has_one
13+
when :has_many
14+
Plutonium::Core::Fields::Renderers::AssociationRenderer.new(name, **)
15+
else
16+
Plutonium::Core::Fields::Renderers::BasicRenderer.new(name, **)
17+
end
18+
end
19+
20+
def self.infer_for_resource_attribute(resource_class, attr_name, **options)
21+
type = nil
22+
options[:label] ||= resource_class.human_attribute_name(attr_name)
23+
24+
if (reflection = resource_class.try(:reflect_on_association, attr_name))
25+
type = reflection.macro
26+
elsif (attachment = resource_class.try(:reflect_on_association, :"#{attr_name}_attachment"))
27+
type = :attachment
28+
elsif (column = resource_class.try(:column_for_attribute, attr_name))
29+
type = column.type
30+
end
31+
32+
build(attr_name, type:, **options)
33+
end
34+
735
eager_autoload do
836
autoload :BasicRenderer
37+
autoload :AssociationRenderer
938
end
1039
end
1140
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Plutonium
2+
module Core
3+
module Fields
4+
module Renderers
5+
class AssociationRenderer < BasicRenderer
6+
private
7+
8+
def renderer_options = {helper: :display_association_value}.freeze
9+
end
10+
end
11+
end
12+
end
13+
end

lib/plutonium/core/fields/renderers/basic_renderer.rb

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ module Plutonium
22
module Core
33
module Fields
44
module Renderers
5-
class BasicRenderer < Plutonium::Core::Fields::Renderer
6-
attr_reader :helper
5+
class BasicRenderer
6+
attr_reader :name, :label, :user_options
77

8-
def initialize(*args, helper: nil, **options)
9-
super(*args, **options)
10-
11-
@helper = helper
8+
def initialize(name, label:, **user_options)
9+
@name = name
10+
@label = label
11+
@user_options = user_options
1212
end
1313

1414
def render(view_context, record)
15-
view_context.display_field value: record.send(name), helper:, **options
15+
view_context.display_field value: record.send(name), **options
1616
end
17+
18+
def options = @options ||= renderer_options.deep_merge(@user_options)
19+
20+
private
21+
22+
def renderer_options = {}.freeze
23+
1724
end
1825
end
1926
end

0 commit comments

Comments
 (0)