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

resourcifying an STI table at the base class level #376

Open
AlexeyMK opened this issue Dec 20, 2015 · 2 comments
Open

resourcifying an STI table at the base class level #376

AlexeyMK opened this issue Dec 20, 2015 · 2 comments

Comments

@AlexeyMK
Copy link

My application has Users and many kinds of Applications (with STI). Users can have roles on Applications. I was hoping to use STI with resourcify, only applying resourcify on the base Application class.

Currently, if I add resourcify to the base class, the Role gets created, but when I try User.first.roles, the ApplicationSubType resources aren't found.

For now, I'm just going to add resourcify to every Application sub-type, and it's fine. However, it would be nice to have more native support from Resourcify.

Looking at https://github.com/RolifyCommunity/rolify/blob/99d886e8c5ba2f554f1c3573fcd91575a16dd3e5/lib/rolify.rb,

  def resourcify(association_name = :roles, options = {})
    include Resource

    options.reverse_merge!({ :role_cname => 'Role', :dependent => :destroy })
    resourcify_options = { :class_name => options[:role_cname].camelize, :as => :resource, :dependent => options[:dependent] }
    self.role_cname = options[:role_cname]
    self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")

    has_many association_name, resourcify_options

    self.resource_adapter = Rolify::Adapter::Base.create("resource_adapter", self.role_cname, self.name)
    @@resource_types << self.name
  end

it seems like simply allowing an option to override self.name to always use Application as the resource name would be a simple-enough fix; that said, I haven't looked into the issue deeply.

Let me know if this seems at all feasible - happy to send in a PR.

@darkporpoise
Copy link

I ran into something similar and found this issue as well as #203. I think I have resolved it by modifying the resource type within the Roles class to make sure Rolify is always using the base class for STI associations.

#app/models/role.rb
class Role < ApplicationRecord
...
  def resource_type=(type)
    super(type.to_s.classify.constantize.base_class.to_s)
  end
...
end

As mentioned here under Polymorphic Associations

@doits
Copy link

doits commented Mar 12, 2020

#517 might fix this. You can give it a shot if you are still interested in this issue and see if it helps.

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

No branches or pull requests

4 participants