Giter VIP home page Giter VIP logo

concerned's Introduction

Concerned

Use the "concerns" pattern in any Ruby or Rails project.

Rails 3

In Gemfile:

gem 'concerned'

Usage

This gem comes with the following three helpers, that are added to Module. To require concern modules (or classes)

  • concerned_with
  • shared_concerns

And to include concern modules

  • include_concerns
  • include_shared_concerns
class FixtureUser
  include_concerns :scopes, :validations  
end

The include_concerns helper method can be made to find a module or class matching the current namespace, using the ns option. The following will include FixtureUser::Scopes and FixtureUser::Validations into FixtureUser.

class FixtureUser
  include_concerns :scopes, :validations, ns: true
end

You can specify a specific namespace by using ns, for or from with a string indicicating the namespace (will be camelized).

class FixtureUser
  include_concerns :scopes, :validations, from: 'Fixture'
end

The include_shared_concerns expects to find a module in a shared folder somewhere in the load path that has a name of either Caching or Shared::Caching for the following example:

class FixtureUser
  include_shared_concerns :caching
end

The concerned_with and shared_concerns methods are used simply to require shared files following the namespace convention, here: project/job_matches and shared/associations.

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 FixtureUser::Scopes and FixtureUser::Validations into FixtureUserFor :)

Global config

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_disable!

Use 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 ActionSupport::Concern using extend ActiveSupport::Concern.

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

Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for further details.

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.