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

How does klass work for dynamoid? #469

Open
olubiyiontheweb opened this issue Sep 11, 2020 · 3 comments
Open

How does klass work for dynamoid? #469

olubiyiontheweb opened this issue Sep 11, 2020 · 3 comments

Comments

@olubiyiontheweb
Copy link

Please I'm trying to integrate dynamoid into this gem, https://github.com/adamniedzielski/tiddle

But it's giving errors.

How can I get the class of the association?

elsif resource.respond_to?(:associations) # Dynamoid
resource.associations[:authentication_tokens]
else

def authentication_token_class(resource)
if resource.respond_to?(:association) # ActiveRecord
resource.association(:authentication_tokens).klass
elsif resource.respond_to?(:relations) # Mongoid
resource.relations['authentication_tokens'].klass
elsif resource.respond_to?(:associations) # Dynamoid
resource.associations[:authentication_tokens]
else
raise 'Cannot determine authentication token class, unsupported ORM/ODM?'
end
end

Thanks

@andrykonchin
Copy link
Member

andrykonchin commented Sep 11, 2020

Could you describe in more details the use case and what class you need to get?

@olubiyiontheweb
Copy link
Author

olubiyiontheweb commented Sep 11, 2020

Thanks for your reply,

I'm looking to use a token for my devise authentication on rails,

We use dynamoid ORM on the API, so I tried using the Tiddle gem which supports multiple devices per user with it.

It turned out that it doesn't have support for dynamoid, as it showed

"Cannot determine authentication token class, unsupported ORM/ODM?"

So I tried to trace to the error,

it was here https://github.com/adamniedzielski/tiddle/blob/master/lib/tiddle/token_issuer.rb#L55

I tried getting the association class for Dynamoid from rails console, it doesn't work

if resource.respond_to?(:associations) # this returns true/false for Dynamoid, It returned true in rails console
resource.associations[:authentication_tokens] # for this klass didn't work, but it returned {type: has_many} in rails console
end

Here is what my model looks like

class User
include Dynamoid::Document

devise :database_authenticatable,
:registerable,
:recoverable,
:token_authenticatable

has_many :authentication_tokens

end

class AuthenticationToken
include Dynamoid::Document

belongs_to :user

end

Here is an example of an active_record implementation.

https://apidock.com/rails/ActiveRecord/Reflection/AssociationReflection/klass

class Author < ActiveRecord::Base
has_many :books
end

Author.reflect_on_association(:books).klass

=> Book # it's expected to return the target association's class

How can I achieve this in dynamoid?

Here is a fork of the gem with my adaptations for dynamoid.

https://github.com/querldox5/tiddle

@andrykonchin
Copy link
Member

andrykonchin commented Sep 13, 2020

There is no public interface to get associated model class but such logic is implemented here:

      def target_class
        options[:class] || target_class_name.constantize
      end

You can find examples of usage in specs:

  it 'determins target class correctly' do
    expect(magazine.subscriptions.send(:target_class)).to eq Subscription
    expect(user.books.send(:target_class)).to eq Magazine
  end

To use this private method you need to have an instance of User class. There is an option to avoid it and instantiate association object itself and call target_class method on it (source):

Dynamoid::Associations.const_get(type.to_s.camelcase).new(nil, name, options)

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