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

Why does user_model have to be defined in every model? #22

Open
RodneyU215 opened this issue Oct 5, 2014 · 9 comments
Open

Why does user_model have to be defined in every model? #22

RodneyU215 opened this issue Oct 5, 2014 · 9 comments

Comments

@RodneyU215
Copy link

Reading through the docs on the Readme, it specifies that if your user model is something other than 'User' to define it using: mongoid_userstamp user_model: 'Admin'

However, it appears that even if I used a default 'User' model, I must still specify my mongoid_userstamp user_model: in every model that I want user stamped. I'm confused why this is necessary, It seems like duplication, can you help me understand? You even have it defined as so in your specs.

screen shot 2014-10-05 at 10 24 00 am

@johnnyshields
Copy link
Collaborator

Let me look into this and get back to you. The reason for setting :user_model is that for advanced usage the gem allows multiple user models to be tracked (this is actually what I'm doing in my app; I have CustomerUsers and MerchantUsers). But there should be a default so you don't have to specify this.

@johnnyshields
Copy link
Collaborator

Actually, looking at Mongoid::Userstamp::ModelConfig I see this line:

    def user_model
      @user_model || Mongoid::Userstamp.user_classes.first
    end

The intention here is that if you only have a single user model in your app then you don't need to explicitly specify this. Are you seeing otherwise?

@RodneyU215
Copy link
Author

Thanks for replying so quickly!

I'm seeing that unless I add this line " mongoid_userstamp user_model: 'User' " to my model. I constantly receive an undefined method error for created_by and updated_by always return nil.

In your repo spec/support/book.rb this test model defines the user_model as 'User', if User is used by default, I'm not seeing why this is needed, but removing it causes 20 tests to fail for the same reasons I mentioned above.

@johnnyshields
Copy link
Collaborator

@Rodney-QAGeek I'm pretty sure what's happening in this case is that Admin is being used as the :user_model instead of User, since it's alphabetically first and hence the default (see test models in /spec/support dir.) Can you double check this?

@johnnyshields
Copy link
Collaborator

(At one time the library gave preference to the model name User, now there is no such preference, it's just whichever model had Mongoid::Userstamp::User included first)

@RodneyU215
Copy link
Author

I believe in the case of your specs, you're right. Though, still puzzled why it's not working for projects with only one User class.

Given the below example...

class User
include Mongoid::Document
include Mongoid::Userstamp::User
end

class Account
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Userstamp
end

Account will not have created_by or updated_by methods unless I add the line

mongoid_userstamp user_model: 'User'

@johnnyshields
Copy link
Collaborator

Try just adding the line mongoid_userstamp (without user_model: 'User')

@RodneyU215
Copy link
Author

When I add just that line you mentioned above, I have the created_by and updated_by methods on my Account class, and Mongoid::Userstamp.current_user returns my user. Though when I create an Account, Updated by gets set correctly, but created by does not get set and it also returns the below error on save:

NoMethodError:
undefined method `sub' for User:Class

@johnnyshields
Copy link
Collaborator

Hmm strange. Would you be able to commit some failing test cases with the behavior you'd like to see, I can take a look at it a bit later?

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