Hello, I just started playing with Avo today, and ...
# avo-2
l
Hello, I just started playing with Avo today, and so far it's amazing. One question though, how would I go about managing state transitions? Eg we use AASM, ActiveAdmin and Administrate both have plugins, in Avo do I need to build some kind of Resource tool? I didn't see anything yet that describes that kind of functionality. Can someone please point me in the right direction? Thanks
l
hey there! thanks for trying it out
an you share a bit more about the state transitions you want to make?
or maybe point us towards the active admin plugin that does that?
I mean, I kow what state transitions are, but not sure how they apply to Avo
l
Sure. state machine: https://github.com/aasm/aasm
We use it for payments, eg "pending, paid, refunded, etc.
l
perf!
so you'd like to, for example, trigger a change for one payment from
pending
to
paid
?
l
Yup, exactly. I just need to know how to trigger the
pay!
action in Avo, and which actions are available given the current state, etc.
l
use Avo actions 🙌 https://docs.avohq.io/2.0/actions.html
l
Cool, I'll give that a try.
l
sure. let me know if you hit any snags or if the docs aren't clear enough
l
It seems to make sense, I'll just need to see what happens after I try to dynamically loop over the available actions. I'll let you know what happens. ;-). Thanks!
l
hmm. maybe I can help a bit more. why would you need to loop over the actions?
do you want to conditionally show actions based on the resource?
l
Yes, because the available actions depends on the current state. Eg, if the current state is "pending" the possible transitions could be either "confirm" or "cancel".
l
you'll be able to do that on the show page of each record
l
Yeah i wouldn't want to do it any where else.
l
perf!
you can use the
visible
block on every action
l
Yeah, i got that already
self.visible = -> (resource:, view:) { view == :show }
l
you have access to the
resource
and you can access the actual record with
resource.model
l
yes, this looks like it's built for one action for the class, in most cases there could be two or more actions possible. How can I define mutiple actions at once?
l
multiple actions per resource?
l
Yeah, well, we have a resource event which is currently "pending" so then it would have two actions. Do I need to make a BaseAction for each possible action? Or can I somehow just dynamically create the BaseActions?
l
you can add the same action on more than one resource
l
Yeah, sure but for one resource, I have probably 5-6 different actions to build. I'm just trying to decide if I should make them manually, eg StatusComplete < Avo::BaseAction, StatusCancel, etc.
l
I think you're better off creating each one
each one will have it's own unique
handle
method,
name
,
visible
block, etc., right?
l
Yep, but you pull these from AASM asking the current available transitions. I guess I can build a couple and then see how I can refactor.
Administrate has a plugin that does this: https://github.com/pondpaun7z/administrate-field-aasm
l
sure. I'm not oposed to any approach. I probably don't have the whole context
l
I might see how they do it dynamically, as it doesn't matter what states you have it just builds them based on your model.
l
another aproach is to create a custom field
that would match to what administrate has here
in the field you have access to the record, resource and a view component which you can model to your liking
l
Ok, I'll have a look at that, thanks.
l
sure!
anytime
l
Hey, me again, in the end I just decided to use the actions as it was both a nicer UX and easier to get up and running. My only question: is there an easy way I could dynamically show/hide them? I tried in the resource using the way fields seem to accept arguments but it didn't work and in the action itself. eg I need to be able to do
if resource.may_complete?
which is true/false. Any ideas?
l
> I could dynamically show/hide them?
you mean the actions?
l
Yeah sorry the actions
action StatusForward do |model| model.can_complete? end
l
use the
visible
block on the action
you have access to the
resource
the
resource.model
holds the state
you could do
resource.model.may_complete?
l
i tried something like this but i only seem to have access to the resource in the handle method. I'll try again thanks
at the bottom
Copy code
ruby
self.visible = -> (resource:, view:) { resource.model.may_complete? }
on the action class
l
ah, ok. That might do it.
l
😉
l
perfect. I had to change it slightly to not do that on the index but it works
self.visible = -> (resource:, view:) { resource.model.try(:may_complete?) ? view == :show : false }
Thanks
l
looks amazing!
l
Hello again, I have what is probably a very simple question but one which I haven't been able to solved. I'm on a show page of a resource which has_many memberships, how can I tweak the menu so that it hides some of the buttons? I tried customizing the controls a bit on both the resources, eg
field :memberships, as: :has_many, detach_button: false
but it doesn't appear to do anything.
l
hey
that is the authorization feature
using pundit you can toggle on/off
can_attach?
,
can_edit?
and others
l
Ah, so I can try to restrict it that way. Its not really about who is authorized but rather a through model which either exists or it doesn't, but I'll try this. Thanks,
Also, today somewhat randomly I started getting translations errors. Do you know how I could debug this.
translation missing: en.avo.x_records_selected_from_a_total_of_x_html
It seems to affect some resources and not others. Some of which I haven't modified.
l
as a good practice, for every Avo update you should check the upgrade guide
we post there all the things that might have changed and if you need to do something
for that you should re-export the locales
bin/rails generate avo:locales
l
Ah, ok, sorry I didn't intend to upgrade. Thanks,
l
no worries
I think you should upgrade
like keep Avo as much as you can up-to-date
that's what I do with most of my dependencies
2 Views