Comments (13)
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.
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.
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.
Closing, the roadmap reflects the wishlist.
from nobrainer.
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.
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.
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.
I think there have been some confusion.
- This is currently supported: a belongs_to that points to a model that have a bunch of subclasses
- 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.
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.
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.
Thanks. I'll manage a work around somehow until this feature lands.
from nobrainer.
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.
Thx! will give this a try
from nobrainer.
Related Issues (20)
- Issue with the command to sync the indexes HOT 13
- How to get list of databases (r.dbList) HOT 1
- DEPRECATION WARNING: `Module#parent` has been renamed to `module_parent`. `parent` is deprecated and will be removed in Rails 6.1. HOT 2
- Declare nested fields HOT 2
- unable to upsert HOT 2
- new rails 6 app Can't create a scaffold HOT 1
- try to use carrierwave-base64 with nobrainer HOT 2
- Index for multi tenancy HOT 5
- "warning: connection.rb:22: warning: URI.unescape is obsolete" with Ruby 2.7
- "undefined method `<<' for false:FalseClass" on `machine_id` from `NoBrainer::Document::PrimaryKey::Generator._generate` with Rspec HOT 1
- Prepare for Ruby 3 HOT 1
- Not working with activesupport - 6.1.0 HOT 6
- Support for range query using compound index?
- Fix the CI not running the tests anymore HOT 1
- Add support for the `store_accessor` method
- Dear Contributors, we'd like to discuss purchasing the domain name "nobrainer.io" HOT 5
- upsert won't work with polymorphic association
- Issue with the polymorphism association
- Deprecate Ruby 1.9 HOT 1
- Querying a polymorphic association doesn't work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nobrainer.