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

Fix Alchemy.user_class_name constant conflict #1724

Merged
merged 5 commits into from
Feb 23, 2020

Conversation

mickenorlen
Copy link
Contributor

What is this pull request for?

After fresh installation in rails 6, after creating the index page, I got this error when trying to load page tree:

[active_model_serializers] Rendered ActiveModel::Serializer::Null with Alchemy::PageTreeSerializer (26.56ms)

NoMethodError undefined method `arel_table' for Alchemy::Admin:Module in /app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/table_metadata.rb:51:in `associated_table'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/predicate_builder.rb:77:in `block in expand_from_hash'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/predicate_builder.rb:68:in `each'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/predicate_builder.rb:68:in `flat_map'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/predicate_builder.rb:68:in `expand_from_hash'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/predicate_builder.rb:21:in `build_from_hash'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/where_clause_factory.rb:19:in `build'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/query_methods.rb:656:in `where!'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/relation/query_methods.rb:649:in `where'
/app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/querying.rb:21:in `where'
/app/_docker/data/bundle/ruby/2.7.0/gems/alchemy_cms-4.4.1/app/models/alchemy/folded_page.rb:20:in `folded_for_user'
/app/_docker/data/bundle/ruby/2.7.0/gems/alchemy_cms-4.4.1/app/serializers/alchemy/page_tree_serializer.rb:15:in `pages'
/app/_docker/data/bundle/ruby/2.7.0/gems/alchemy_cms-4.4.1/app/serializers/alchemy/base_serializer.rb:26:in `read_attribute_for_serialization'

Then I found this which seems to be a working solution:
https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/18329
https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/18329/diffs

Notable change

Only change made is that I replaced all instances of:
Alchemy.user_class_name
with:
"::#{Alchemy.user_class_name}"

I just started to test alchemy out so I can't tell if this has some other consequences as of yet. Also I'm not sure the stamper_class_name should be replaced in the same manner.

@tvdeyen
Copy link
Member

tvdeyen commented Feb 19, 2020

Thanks. I think that makes sense to avoid constant name conflicts.

But could you please make the change in the lib/alchemy/auth_accessors.rb file instead? The user_class_name method returns already a String and it will reduce the number of files that needs to be changed down to one.

@mickenorlen
Copy link
Contributor Author

Yeah, that makes more sense.

I made getter/setter methods for the user_class_name variable and removed it from mattr_accessor. Just tell me if there's something you want to change.

@mickenorlen
Copy link
Contributor Author

mickenorlen commented Feb 21, 2020

Maybe this might need a bit more testing.. I'm not sure if its related but now i'm seeing this when I'm trying to fold pages:

ActiveRecord::InverseOfAssociationNotFoundError Could not find the inverse association for user (:folded_pages in ::Admin) in /app/_docker/data/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/reflection.rb:240:in `check_validity_of_inverse!'

Removing inverse_of: :folded_pages in folded_page.rb Gets me past this for now locally at least..
Here they are saying something.. not sure if related...
railsadminteam/rails_admin#975

Tell me if you want me to create a new pr for that fix also.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this is very good. Please add a test as well.

lib/alchemy/auth_accessors.rb Show resolved Hide resolved
@tvdeyen
Copy link
Member

tvdeyen commented Feb 23, 2020

Unfortunately the Github CI is having some issues. I will look into this soon. Meanwhile please run tests locally (they are very fast) and add a test for the namespace fix. Thanks again for your contribution

@tvdeyen
Copy link
Member

tvdeyen commented Feb 23, 2020

For the changelog (that gets generated from PR titles) it would be better to rename this PR

@mickenorlen mickenorlen changed the title Fix undefined method arel_table Fix Alchemy.user_class_name constant conflict Feb 23, 2020
lib/alchemy/auth_accessors.rb Show resolved Hide resolved
lib/alchemy/auth_accessors.rb Outdated Show resolved Hide resolved
@mickenorlen
Copy link
Contributor Author

mickenorlen commented Feb 23, 2020

Ah, missed reading through the contribution notes. Is it possible to run Hound locally also?

Added a test for getting with prefix :: and moved the old user_class_name.is_a?(String) test from user_class to user_class_name method.

Still have 2 failures. Unrelated?

Also, what to do with inverse_of: :folded_pages in folded_page.rb as mentioned above?

Failures:

  1) Page request caching when caching is enabled in app but page should not be cached does not set etag header
     Failure/Error: expect(response.headers).to_not have_key('ETag')
       expected #has_key?("ETag") to return false, got true
     # ./spec/requests/alchemy/page_request_caching_spec.rb:103:in `block (4 levels) in <top (required)>'

  2) Page request caching when caching is enabled in app but a flash message is present does not set etag header
     Failure/Error: expect(response.headers).to_not have_key('ETag')
       expected #has_key?("ETag") to return false, got true
     # ./spec/requests/alchemy/page_request_caching_spec.rb:127:in `block (4 levels) in <top (required)>'

@tvdeyen
Copy link
Member

tvdeyen commented Feb 23, 2020

The two failing specs are unrelated.

@tvdeyen
Copy link
Member

tvdeyen commented Feb 23, 2020

@mickenorlen would you please rebase with latest master to fix the CI builds and squash the commits into one? Thanks

@tvdeyen tvdeyen merged commit aab53cb into AlchemyCMS:master Feb 23, 2020
tvdeyen added a commit that referenced this pull request Feb 25, 2020
- Do not use deprecated methods [#1737](#1737) ([tvdeyen](https://github.com/tvdeyen))
- Order contents by their position in its element [#1733](#1733) ([tvdeyen](https://github.com/tvdeyen))
- Eager load relations in elements trash [#1732](#1732) ([tvdeyen](https://github.com/tvdeyen))
- Run CI builds with Sprockets 3.7.2 [#1731](#1731) ([tvdeyen](https://github.com/tvdeyen))
- Re-organize development dependencies [#1730](#1730) ([tvdeyen](https://github.com/tvdeyen))
- Update pr template [#1729](#1729) ([tvdeyen](https://github.com/tvdeyen))
- Generate views without _view in the filename [#1728](#1728) ([tvdeyen](https://github.com/tvdeyen))
- Fix CI Builds [#1727](#1727) ([tvdeyen](https://github.com/tvdeyen))
- Fix page tagging condition: should_attach_to_menu?  [#1725](#1725) ([mickenorlen](https://github.com/mickenorlen))
- Fix Alchemy.user_class_name constant conflict [#1724](#1724) ([mickenorlen](https://github.com/mickenorlen))
tvdeyen added a commit to AlchemyCMS/alchemy-solidus that referenced this pull request Feb 25, 2020
Alchemy 4.4.2 has a fix for constant lookup issues
(AlchemyCMS/alchemy_cms#1724)

That prefixes the user class with `::`

In order to be compatible with older Alchemy versions we use
the class#name instead.
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

Successfully merging this pull request may close these issues.

3 participants