TrestleAdmin / trestle

A modern, responsive admin framework for Ruby on Rails
https://trestle.io
GNU Lesser General Public License v3.0
1.97k stars 177 forks source link

Conflicting Scopes? #520

Open bugzbrown opened 2 weeks ago

bugzbrown commented 2 weeks ago

I am really not sure what exactly is going on here, and I am pretty sure this is the edge case of the edge case scenarios.

I am implementing trestle in a legacy project here. I have dozens of models that are pretty much all related. We are adopting the use of Trestle focused on behavior rather than on "entities". What do I mean by that? We have several "admins" focused on specific actions we want users to take. (Example: I have an action which is "edit the user's address" - this links to customer/address_admin.rb and my form only allows users to alter address details)

OK so everything was going fine... My resources are all working beautifully with initializing them with Trestle.resource(:block_name, [scope: SomeScope], model: SomeModel) I mean, until I ran into a problem with a specific Model.

Trestle.resource(:date_bulk_update, model: InvoiceGroup) do
   menu do
      item :date_bulk_update,
   end
   # everything else is commented
end

Causes my server to crash. In the logs, the crash seems to be in app/models/invoice_group.rb and the next couple of lines suggest it has something to do with scopes in ActiveRecord

these are my scopes

  scope :find_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  scope :find_by_business_and_bank, lambda { |business, bank|
                                      joins(:businesses)
                                        .where(businesses: { document: business.document })
                                        .where(bank: bank)
                                        .order(created_at: :desc)
                                    }
  scope :monitored, lambda { |bank|
    if bank.slug == 'bank_slug'
      where('invoice_groups.created_at >= ?', 6.months.ago)
    else
      all
    end
  }

Funny/Odd things:

Depending on how I instance InvoiceGroup:

This Crashes:

Trestle.resource(:date_bulk_update, model: InvoiceGroup) do ...

This Works:

Trestle.resource(:invoice_groups) do ...

If I comment or rename my scopes, my server spins up correctly

THIS CRASHES MY SERVER

  # scopes edited to make it easier to read
  scope :find_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  scope :find_by_business_and_bank, lambda { |business, bank| ... }
  scope :monitored, lambda { |bank| ... }

EITHER WORKS

  # scopes edited to make it easier to read
  scope :afind_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  scope :afind_by_business_and_bank, lambda { |business, bank| ... }
  scope :amonitored, lambda { |bank| ... }

  ### OR

  # scopes edited to make it easier to read
  #scope :find_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  #scope :find_by_business_and_bank, lambda { |business, bank| ... }
  #scope :monitored, lambda { |bank| ... }

Any idea of what may be going on?

spohlenz commented 2 weeks ago

I've just tried reproducing this in a new Rails (8.0) app and wasn't able to. The only thing that I notice in your code that could be a cause is the trailing comma on your item line:

Trestle.resource(:date_bulk_update, model: InvoiceGroup) do
   menu do
-      item :date_bulk_update,
+      item :date_bulk_update
   end
   # everything else is commented
end

If that doesn't help, could you please post the full error trace and your Trestle/Rails versions.