Giter VIP home page Giter VIP logo

Comments (13)

nviennot avatar nviennot commented on June 15, 2024 1

Sure, or you could use regular polymorphism:

class Right
  field :permissions, type: Array
end

class UserRight < Right
  belongs_to :user
end

class GroupRight < Right
  belongs_to :group
end

from nobrainer.

nviennot avatar nviennot commented on June 15, 2024 1

IMHO, simpler would be to store a field :permissions, type: Array in User and Group which would store a list of things someone can do. So no Right model. Maybe that logic (including the field declaration) could be encapsulated in a HasRights module or something.

module HasRights
  included do
    field :permissions, type: Array
  end

  def can_perform?(something)
    permissions.include?(stomething)
  end
end

class User
  include HasRights
end

class Group
  include HasRights
end

from nobrainer.

nviennot avatar nviennot commented on June 15, 2024

There is no support for "polymorphic" belongs_to in the rails sense (I don't like the "polymorphic" name so much because in OO, this is often used for a class and its subclasses -- a better name should be found for this feature.)

I've added this feature to the wish list: NoBrainerORM/nobrainer.io@559ab14

Note1: This feature is fairly low priority.
Note2: We won't need to change the has_many at all for this feature, just belongs_to.

from nobrainer.

nviennot avatar nviennot commented on June 15, 2024

Closing, the roadmap reflects the wishlist.

from nobrainer.

radicaled avatar radicaled commented on June 15, 2024

Did this ever land? I don't see it in the roadmap anymore and NoBrainer doesn't recognize the polymorphic symbol on a belongs_to -- but NoBrainer does and names stuff slightly differently, so I'm not entirely sure I'm using the right symbol.

from nobrainer.

nviennot avatar nviennot commented on June 15, 2024

I'm not sure why I removed it after: NoBrainerORM/nobrainer.io@490b635

Do you have a usecase where using a polymorphic belongs_to is better compared to one? If I'm convinced that they are useful, it would motivate me to get it done.

Thank you :)

from nobrainer.

radicaled avatar radicaled commented on June 15, 2024

Do you have a usecase where using a polymorphic belongs_to is better compared to one? If I'm convinced that they are useful, it would motivate me to get it done.

I'm not sure my use-case is any more special than the typical ActiveRecord case. I'm using composition over inheritance, so I've got data models that represent user generated content, and these various data models all implement certain patterns so that they can be combinations of the following: reportable, voteable, taggable, displayable, moderatable, that sort of thing. Most items are reportable, but not all items are voteable, etc etc.

I was planning on using polymorphism to avoid having to know about the internals and types of each data model for the most part. Single code path, less errors, that stuff.

from nobrainer.

nviennot avatar nviennot commented on June 15, 2024

I think there have been some confusion.

  1. This is currently supported: a belongs_to that points to a model that have a bunch of subclasses
  2. This is not supported: a belongs_to that could point to any model

I think your example is doing 1), right?

If not, feel free to show some code, so we can establish a common ground.

from nobrainer.

radicaled avatar radicaled commented on June 15, 2024

Ah, these aren't subclasses, I'm using modules to mix in behavior for certain classes.

I have code like this:

module Moderatable
   extends ActiveRecord::Concern
   included do
      field :removed_at, type: Time
      field :removed_reason, type: String
   end

   def remove!
     # ...
   end
end

# ... several other behaviors for various things

class Comment
   include NoBrainer::Document
   include Moderatable
   include Voteable
   include Reportable 
end

class AbuseReport
   include NoBrainer::Document

  field :reported_at, type: Time
  belongs_to :reporter, class_name: 'User'
  belongs_to :handler, class_name: 'AdminUser'
   # this is where I'd do a belongs_to :reportable, polymorphic: true to point at a Comment / Image / etc.
   # ...
end

class Vote
  field :score, type: Integer
  belongs_to :voter, class_name: 'User'
  # See above
  # ...
end

I avoided taking a subclassing based approach because my hierachy wouldn't make any sense, I think:

Post > Reportable > Voteable > ... ?!

Well, it may be possible to make that work but it seems rather... off.

from nobrainer.

nviennot avatar nviennot commented on June 15, 2024

That's a compelling example. I'll see about implementing this feature :)

However, this is quite substantial to implement, and I don't have much time these days, so I can only give you a promise that I'll implement it at this time. I'll try to get it done by the end of the month.

from nobrainer.

radicaled avatar radicaled commented on June 15, 2024

👍

Thanks. I'll manage a work around somehow until this feature lands.

from nobrainer.

dvergeylen avatar dvergeylen commented on June 15, 2024

Hi @nviennot,

Thank you for the work already done on this ORM.

I currently have another example under the hood. I use to give some Rights to Users and Groups (of users). This allows me to give general rights to group(s) and fine-tune some permissions for some users only (for admin purposes like unlock, edit posts, ...).

This way we have belongs_to :editor, :polymorphic => :true in my Right model. This illustrates your case '2)', doesn't it?

from nobrainer.

dvergeylen avatar dvergeylen commented on June 15, 2024

Thx! will give this a try 👍

from nobrainer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.