dwbutler / groupify

Add group and membership functionality to your Rails models
MIT License
194 stars 42 forks source link

Either enable custom association options or set inverse_of for the relations #50

Open juhazi opened 8 years ago

juhazi commented 8 years ago

The associations generated by groupify and has_members methods lack the inverse_of option.

It should be possible to enable this on a per-call basis to keep backwards compatibility.

Example usage:

class Organization < Group
  has_members :managers,
    inverse_of: true
end

Which would generate

class Organization < ActiveRecord::Base
  has_many :managers,
    through: :group_memberships_as_group,
    inverse_of: :organizations,
    ... # The rest of the options 
end

Alternatively allow the developer to merge in a (whitelisted?) set of extra keys to the association options hash.

Example of that:

class Organization < Group
  has_members :managers,
    inverse_of: :organizations
end

For more info on inverse_of specifically: https://www.viget.com/articles/exploring-the-inverse-of-option-on-rails-model-associations

dwbutler commented 8 years ago

I would be in favor of passing in association options.

For inverse_of specifically, in Rails 4 it is usually inferred automatically, so it doesn't need to be specified unless the names don't match up. So association options would allow that configuration to be put in place.

juhazi commented 8 years ago

I started work in 3179b3e

If it looks ok I'll replicate it for all relations and merge with #49 for a PR.

joelvh commented 6 years ago

@juhazi Please check out #61 - it allows you to specify custom association options using the has_group or has_member helpers for each association you want to create rather than using has_groups or has_members. Let me know if this does what you need. Thanks.