Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflicting Scopes? #520

Open
bugzbrown opened this issue Nov 8, 2024 · 1 comment
Open

Conflicting Scopes? #520

bugzbrown opened this issue Nov 8, 2024 · 1 comment

Comments

@bugzbrown
Copy link

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
Copy link
Member

spohlenz commented Nov 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants