bullet-train-pro / bullet_train-action_models

Other
4 stars 1 forks source link

`action-model:targets-one-parent` succeeds, but generates runtime error? #93

Closed andrewculver closed 10 months ago

andrewculver commented 10 months ago

To recreate this issue:

Add contacts:

bin/super-scaffold crud Contact Team email:text_field first_name:text_field last_name:text_field

Add newsletters:

bin/super-scaffold crud Newsletter Team name:text_field

Let contacts subscribe to newsletters:

bin/super-scaffold join-model Newsletters::Subscription contact_id{class_name=Contact} newsletter_id{class_name=Newsletter}
bin/super-scaffold crud-field Contact newsletter_ids:super_select{class_name=Newsletter}
bin/super-scaffold crud Newsletters::Issue Newsletter,Team name:text_field body:text_area

You'll need to implement the valid_newsletters method of Contact in ./app/models/contact.rb:

def valid_newsletters
  team.newsletters
end

Finally, create a send action for the issue:

bin/super-scaffold action-model:targets-one-parent Send Newsletters::Issue Newsletter,Team

This appears to work, but when I visit the issue index page, I see this:

Screenshot 2023-10-25 at 10 19 11 PM
gazayas commented 10 months ago

The link to the new send action has this:

<% if can? :create, Newsletters::Issues::SendAction.new(newsletter: newsletter) %>
  <%= button_to "Send", [:new, :account, newsletter, :issues_send_action], method: :get, class: 'button-secondary', form: {class: 'button_to'} %>
<% end %>

So, we aren't currently getting these routes below, but I'm assuming this is what we want because newsletter above is an object (currently, we get another newsletter namespace nested inside the newsletter resource block). Also, :issues needs to be a namespace because we pass :issues_send_action above.

resources :newsletters do
  scope module: 'newsletters' do
    resources :issues, only: collection_actions
  end

  namespace :issues do
    resources :send_actions do
      member do
        post 'approve'
      end
    end
  end
end

namespace :newsletters do
  resources :issues, except: collection_actions
end

This setup provides a route that matches the Send button so we can access the index page, but we ultimately can't perform the Send action, and this shows up in the logs:

web              | Started GET "/account/newsletters/jNeWJL/issues/send_actions/new" for ::1 at 2023-10-26 19:12:58 +0900
web              |   
web              | ActionController::RoutingError (uninitialized constant Account::Issues):
web              |   
web              | Started GET "/account/newsletters/jNeWJL/issues" for ::1 at 2023-10-26 19:12:58 +0900

I can go ahead and make a fix for the routing if what I wrote above is correct, but I still want to make sure this Send action is working, and I'm currently stuck on that... Will post again if I make more progress.

gazayas commented 10 months ago

Can't seem to track down the uninitialized constant Account::Issues problem. Will try to swing back to this one soon.

gazayas commented 10 months ago

@andrewculver Okay, I think I figured out the routing for this one.

resources :newsletters do
  scope module: 'newsletters' do
    resources :issues, only: collection_actions
    namespace :issues do
      resources :send_actions, shallow: false do
        member do
          post 'approve'
        end
      end
    end
  end
end

namespace :newsletters do
  resources :issues, except: collection_actions
end

This makes sense with the routing that the MarkAllAsReadActions test produces, which is also a targets-one-parent action:

resources :customers do
  resources :notifications
  namespace :notifications do
    resources :mark_all_as_read_actions do
      member do
        post 'approve'
      end
    end
  end
end

It's just a little more convoluted because of the deep namespacing we have going on with the targets-one-parent Newsletters::Issues::SendAction. Next will be to apply a proper fix to the RoutesFileManipulator and/or the Transformer.

Remaining issues

  1. Breadcrumbs
  2. Show page links

The breadcrumbs partial isn't working, but I haven't looked too much into it yet.

As for as the show page links, this is what we get for the edit page link:

<%= link_to t('.buttons.edit'), [:edit, :account, @send_action], class: first_button_primary if can? :edit, @send_action %>

However, we want something like this:

[:edit, :account, :@send_action.targeted, @send_action]

This is giving problems because @send_action is namespaced with the targeted object, producing something like this:

edit_account_newsletter_newsletters_issues_send_action

If we can manage to scaffold these properly based on the action type, then we can make this new combination a reality.

In Closing

I don't want to make too much clutter on this issue, so I'll try to start implementing the logic for this model combination to work and make any additional comments in a pull request.