Calling smart actions from smart views

Using forest_liana gem in a rails 6 app we have created a smart action that excepts an array of integers as a parameter.

We need to call this from javascript code in a smart view, and the current implementation overrides the triggerSmartAction function, e.g:

  @action
  triggerSmartAction(...args) {
    args[2] = [1,2,3,4];
    return triggerSmartAction(this, ...args);
  }

The rails controller action will then receive that data in the parameters:

Parameters: {"data"=>{"attributes"=>{"collection_name"=>"MyCollection", "values"=>{}, "ids"=>[1,2,3,4], etc...}

This is a hack though. I have not seen any other valid alternatives??
Anyway, this does work, but only if the smart view is accessed directly from that collection.

If it is accessed through a parent collection, the parameters received become e.g:

Parameters: {"data"=>{"attributes"=>{"collection_name"=>"MyCollection", "values"=>{}, "ids"=>[], "parent_collection_name"=>"MyCollectionParentCollection", "parent_collection_id"=>"1", etc...}

And the data we need from the ids param is no longer present.

Is there a workaround for this?

Many thanks

Context

Rails 6.1.3.1
Forest liana gem 6.3.1

Hi @veritas1,

Thank you for reaching us with your concern.

Helping you crafting a more suited solution might require a bit more of context. What kind of parameters will replace the [1,2,3,4] in your example? Is that a static value? If so you could just provide it to the action directly in its definition on the rails implementation of the smart action to have it anyhow.

Hi @anon37102731

No, the values are dynamically generated within the forest admin client.

The hardcoded values in the example are just for illustration purposes.

@veritas1, could you explain to me what kind of dynamic data it is?

So maybe I can come up with an idea on how to make it work :slight_smile:

It’s an array of resource IDs e.g [“246”, “534”, “102”]

Hi @veritas1,

Where do those IDs come from? Are those the IDs of the selected records on which you apply the smart action?

@anon90145840 They are IDs iof the records. They are not selected though. The smart view has a drag n drop ui to sort the records, creating an array of resource IDs in their new order.

Hi @veritas1,

Can you share you’re code that dynamically generated the values ? It would be appreciated as the answer can be there. :slight_smile:

Thanks in advance.
Morgan

Hi @morganperre

I don’t think so to be honest. Essentially, the question is this:

If I have a smart action that has fields, defined as e.g:

action 'My Smart Action', type: 'global', fields: [
  {
    field: 'My Field',
    type: ['String']
  }
]

Then how do I call this smart action (passing values for the field My Field) from a smart view javascript component?

I was overriding triggerSmartAction as I did not find any other alternative. Is there one that I have missed/maybe not documented?

Thanks

Ok, I didn’t get your problem at first glandes.

I will let an other member of our team give you a right answer on the subject.

Regards,
Morgan

Hello @veritas1,

I know the array of resource Ids is dynamic in your case, but I’d like we test something.
Could you try with static values, ex args[2] = [1,2,3,4], save your smart view. Then refresh your browser, and execute your smart action from your smart view.
Do you correctly see "ids"=>[1,2,3,4] in both case?

I’m suspecting a cache issue, but I need you to help me on this one :wink:

Hi @Guillaume_Cisco

I experience the same still.

If the smart view is accessed on the owning collection, I get "ids"=>[1,2,3,4]

If the smart view is accessed through a relationship, e.g a parent collection, I get "ids"=>[]

Thank you @veritas1,

I’m experiencing the same behaviour by setting args[2] = [1,2,3,4].
What is going on, is that you should not modify the args[2], this way as for parent_collection, it will behave differently.
If you keep records as your third parameter for args:

  • when accessing the owning collection, you will have a list of ids “ids”=>[1,2,3,4]
  • from a relationship, with a parent collection, you will have an empty list of id, BUT a parent_collection_id set to the id related to the initial record. You will also have parent_collection_name and parent_association_name filled.

In definitive, you should not override the args[2] parameter as it is strongly binded to your real records.

Thanks @Guillaume_Cisco

Makes sense.

But, any solution? Is there no native support within Forest Admin for calling smart actions in this way?

Right now, it will be impossible for you to pass record ids in the case of a parent collection as it is not yet supported.
Only selected related records from the table view would be sent, but in the case of a smart view, you “cannot select records” as the smart view overrides the default table view.

We will see with the product team if we allow to support this case (I have a good feeling we will).
I’m sorry to tell you there is no workaround yet.

I will keep you updated :slight_smile:

Hello @veritas1,

We released a new version of our front application.
You should now be able to override the records directly from your smart view, it will be available in related data too :tada:

Let me know if it solves your issue :wink:

Hi @Guillaume_Cisco

Works! Many thanks :+1: