Skip to content

Commit 573b684

Browse files
authored
refactor: http generator (#3779)
1 parent cc72125 commit 573b684

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Diff for: lib/generators/avo/concerns/parent_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module ParentController
1313

1414
def parent_controller
1515
return "Avo::ArrayController" if options["array"]
16+
return "Avo::Core::Controllers::Http" if options["http"]
1617

1718
options["parent-controller"] || ::Avo.configuration.resource_parent_controller
1819
end

Diff for: lib/generators/avo/resource_generator.rb

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class ResourceGenerator < NamedBaseGenerator
2222
type: :boolean,
2323
default: false
2424

25+
class_option "http",
26+
desc: "Indicates if the resource should be HTTP.",
27+
type: :boolean,
28+
default: false
29+
2530
def create
2631
return if override_controller?
2732

@@ -33,6 +38,8 @@ def create
3338
def parent_resource
3439
if options["array"]
3540
"Avo::Resources::ArrayResource"
41+
elsif options["http"]
42+
"Avo::Core::Resources::Http"
3643
else
3744
"Avo::BaseResource"
3845
end

Diff for: lib/generators/avo/templates/resource/resource.tt

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ class Avo::Resources::<%= resource_class %> < <%= parent_resource %><% if option
1111
}
1212
]
1313
end
14+
<% elsif options["http"] %>
15+
# self.endpoint = "https://api.openalex.org/"
16+
# self.endpoint_key = "authors"
17+
# self.parse_collection = -> {
18+
# raise Avo::HttpError.new response["message"] if response["error"].present?
19+
#
20+
# response["results"]
21+
# }
22+
# self.parse_record = -> { response }
23+
# self.parse_count = -> { response["meta"]["count"] }
24+
25+
## This is required only when the ID includes characters (e.g., ".") that could be improperly routed.
26+
# self.find_record_method = -> { query.find Base64.decode64(id) }
27+
## Encodes the ID for use in URL helpers. The `model_class_eval` block is executed when the model is defined in memory,
28+
## allowing us to override `to_param` and obfuscate the raw ID via Base64 encoding.
29+
# self.model_class_eval = -> {
30+
# define_method :to_param do
31+
# Base64.encode64(id)
32+
# end
33+
# }
1434
<% else %>
1535
# self.includes = []
1636
# self.attachments = []<%= model_class_from_args %>
@@ -21,6 +41,10 @@ class Avo::Resources::<%= resource_class %> < <%= parent_resource %><% if option
2141
def fields
2242
field :id, as: :id
2343
field :title
44+
end<% elsif options["http"] %>
45+
def fields
46+
field :id, as: :id
47+
# field :display_name
2448
end<% else %>
2549
def fields
2650
field :id, as: :id<%= generate_fields %>

0 commit comments

Comments
 (0)