forest-rails icon indicating copy to clipboard operation
forest-rails copied to clipboard

Smart Action - Call action from a "smart action" example

Open daniel-gato opened this issue 7 years ago • 2 comments

While using smart actions (again) I'm trying to do something that seems could be integrated to forest and maybe even useful for other users as well.

I'm trying to do 2 different things. The second is workaround for the first one.

1. Update an object through a button from a Smart Action result.

Update an object through a button click from a smart action result view.

Practical example:

I've a Venue model with a google_api_place_id property. The way I get this dada is by calling Google Search Api with something like My venue name in Helsinki. I will then receive 0 or n results. Currently I'm displaying them in the smart action result view but the volume is consequent and I need to be able to have a button doing something like <%= forest_update_object "Save this place_id", object: my_object, data: { google_api_place_id: some_value } %>

2. Call a smart action from a smart action

While trying to come with a workaround for (1) I thought about this solution. Maybe it's already there or possible by using the right helpers.

The goal here is to be able from smart action result view to call another smart action with parameters.

<%= forest_smart_action_button 'name of smart action', ids: array_of_object_ids, fields: array_of_fields %>

NB: In my opinion the fields should be different for each id. But as Forest works already like that, I think we should keep this "incoherence" in my opinion

It is important in this scenario to keep the view open and do not replace the result with the result of the called action. We could have multiple actions to call from one view.

Let me know your thoughts on this matter.

daniel-gato avatar Dec 19 '17 13:12 daniel-gato

I really need this so I'm trying to find the "less bad" solution.

Attempt 1: Forest api

I tried to PUT on /forest/:collection/:id. This seems to me the one I should pursue as forest is supposed to handle this kind of work.

Problems I encountered:

  • Can't "properly" deduct the url we should call it in (multiple environment, multiple endpoints)
  • Can't generate a Authorization Bearer

This was just to ghetto so I stoped digging

Attempt 2: Using a business Controller

Problems I encountered

  • Can't "properly" deduct the base url, had to use some sketchy hardcoded id and jQuery
  • Use the inline onclick attribute as the view doesn't eval <script>
  • skip_before_action :verify_authenticity_tokenas I wasn't able to correctly send the token

I'm not proud of that method and it's really fucked up IMO, but hey... it works for now.

<button onclick="var u = $.find('a[href=&quot;/14626/dashboard&quot;]').length > 0 ? 'https://reshel.herokuapp.com' : 'http://localhost:3000'; $.ajax({url:u+'/apis/<%= query[:venue].try(query[:api]).id %>', type:'PUT',data:{api_venue_id:'<%= result[:api_venue_id] %>'}, success: function(data) { alert('saved') }})">Match</button>

Alternatives?

If you have any other workaround or alternative it will be nice.

daniel-gato avatar Dec 22 '17 08:12 daniel-gato

Concerning my Attempt 1 I decided to save it in in a ENV variable.

daniel-gato avatar Dec 27 '17 15:12 daniel-gato