Use the "concerns" pattern in any Ruby or Rails project.
This gem comes with the following three helpers, that are added to
To require concern modules (or classes)
And to include concern modules
concerned_with helper method expects to find a module or class matching the current namespace, fx the following will include
class FixtureUser include_concerns :scopes, :validations end
include_shared_concerns expects to find a module in a
shared folder somewhere in the load path that has a name of either
Shared::Caching for the following example:
class FixtureUser include_shared_concerns :caching end
shared_concerns methods are used simply to require shared files following the namespace convention, here:
class Project concerned_with :job_matches shared_concerns :associations end
Concerns currently included
You can now include the Concerned module in your class or module and get acces to the meta-info: which concerns are currently included
class FixtureUser include Concerned include_concerns :scopes, :validations include_shared_concerns :caching end
FixtureUser.my_concerns # => [:scopes, :validations] FixtureUser.my_shared_concerns # => [:caching] FixtureUser.all_my_shared_concerns # => [:scopes, :validations, :caching]
You can even include concerns from another scope
class FixtureUserFor include Concerned include_concerns :scopes, :validations, for: 'FixtureUser' include_shared_concerns :caching end
Will include the
You can use the
Concerned.extend_enable! to let the concern helpers also attempt to extend the host module/class with the ClassMethods module of the concerns module (if such exists). Disable it by using:
Concerned.extend_enabled? to see if it is currently enabled or not.
By default this feature is turned off.
It is usually better to use
See fx concerns
Contributing to concerned
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for further details.