https://avo.cool logo
they just have the same parent model (
# general
m
they just have the same parent model (
SurveyQuestion
)
l
Lets continue this discussion in a thread
m
ofc, sorry
to be fair, I always get slightly dizzy contemplating the difference between polymorphic associations and STI πŸ˜„
what I want to do is, once the user clicks "create new survey question", offer a choice of type I guess that's best done with a
type
select/dropdown.
then once created in the case of a multiple choice question, for example, edit it and add options
l
Got it
Maybe transform the "Create question" into an action, in the action modal the user selects the type and when action is executed will redirect to the correct resource creation
Or, several actions: - "Create free form survey question" - "Create multiple choice survey question" And with
self.no_confirmation = true
on that actions the user will be automatically redirected to the question creation
WDYT?
m
ah, I see, because if I just choose a type in the
SurveyQuestion
resource, I won't be redirected to the child resource
it redirects me back to the survey, from where I can then go on and edit the question. I think that's good enough for now
l
Cool!
m
potentially a streamlined solution would make a nice plugin
a
@millions-exabyte-27721 you solved this? I need this exact thing now too πŸ™‚
m
Yeah kind of. I just made a select field for
type
with custom options. I can send you a snippet when I’m back at my computer
a
that'd be amazing, thanks
m
really nothing special:
Copy code
rb
class Avo::Resources::SurveysQuestion < Avo::BaseResource
  self.title = :question
  self.link_to_child_resource = true

  def fields
    field :id, as: :id
    field :question, as: :text
    field :type, as: :select, options: { "Freiform": "Surveys::FreeformQuestion", "Single Choice": "Surveys::SingleChoiceQuestion", "Multiple Choice": "Surveys::MultipleChoiceQuestion" }
  end
end
create a question, then, because
link_to_child_resource
is true, the
has_many
field in
SurveyResource
will link to the child records
and have the appropriate fields
l
@millions-exabyte-27721 do you think there is a elegant way to support this natively?
I see this more of an guide / tutorial
m
a guide would probably be great, yeah. I think the
options
could be more streamlined by rails-i18n probably.
but of course, it's an additional step. one could think about making a dynamic form dependent on the STI child type. i.e. change the type in the select -> dynamically update the form in a
<turbo-frame>
it's actually probably not that hard to do
l
Your resources for the STI childs are different right? i.e each resource have diferent fields?
m
Yep
That’s the point πŸ˜…
l
True, just want to confirm every detail before working on a PoC πŸ˜„
a
Julian, how are you handling the different fields? Just edit it later?
so you end up with the FreeformQuestion resource vs the SurveysQuestion resource?
m
exactly
since they are referenced in the has_many field it's just one more click
the
create
takes you back to the survey in this case, and there if you click on the new
SurveyQuestion
you are linked to the subtype
a
perfect, I'll just do it this way for now
maybe Paul will have a nice new feature for us soon πŸ˜‰
l
@millions-exabyte-27721 and @acceptable-cartoon-88076 what license are you using currently? I'm thinking about a custom implementation but would require advanced
a
whichever one is the highest, I forget
m
Pro
l
Advanced
https://www.loom.com/share/31c8999bce3640ed9c69558472a74d06 Check this out, WDYT about this custom solution? BTW you can do a drinking game each time that I spell vehicle wrong on this video =)))))
a
yes that looks awesome
l
Cool! You can implement it right away
a
do you have that code in a repo/gist or anything?
I can pause the video if you dont πŸ™‚
l
I don't, i can send you when i get on the PC again
Actually not sure if I reverted or not the changes =))
The important parts are on the action and on the vehicle resource
a
yah, I can just pause the video, no big deal
what was that command you ran on the error page?
l
Are you seeing that error?
You shouldn't =))
That's something on my side
a
oh ok
no, I haven't started yet
I was just wondering because it jumped out at me
l
It's something that I need to fix on local environment, but lwt me know if you get any error
a
willdo
So this initially appears to work on the parent class, but my child classes are namespaced and now I'm getting this error:
class Avo::Resources::StepValidationRequiredField < Avo::BaseResource self.model_class = "StepValidation::Validators::RequiredField"
l
Avo doesn't support namespaced resources for now
a
l
You can however use namespaced models on resources
a
which is what I did, as far as I can tell. I copied the example on that help section and updated the model_class path to match the namespaced path to the model
(and the resource name too)
but the resource is in the root level of the directory per the docs
l
How does your resource class looks like?
a
I simplified it a lot to test, so it is: class Avo::Resources::StepValidationRequiredField < Avo::BaseResource self.model_class = "StepValidation::Validators::RequiredField" self.title = :to_s def fields o.field :id, as: :id, visible: admin? o.field :name, as: :text, required: true end end
and it lives in app/avo/resources/step_validation_required_field.rb
l
What about the model?
a
class StepValidation < ApplicationRecord module Validators class RequiredField < Base def self.parameter_schema {"field_name" => "input_field"} end def event_metadata super.merge( field_name: validation.parameters["field_name"] ) end def validate field_to_check = validation.parameters["field_name"] value = extract_value(field_to_check) param_value = value.present? ? value.to_s : "empty" detail = "Validating required field: #{field_to_check}. Current value: #{param_value}" return success(detail) if value.present? failure("#{field_to_check} is required", detail) end end end end
Maybe I need to pull it completely out of the class namespace and just modularize it
I'll change that and test it. It sits under that class as a historical thing I can simplify
l
Most probably that's the case, let me know how it goes
a
hey @loud-jewelry-99127 following up on this, I have WorkflowStep, with polymorphic types .... is there a way to make the link-to-record in WorkflowStep#index link to the correct resource type instead of the base WorkflowStep?
ignore me, just found self.link_to_child_resource = true