<commit_before>Planify [![Build Status](https://secure.travis-ci.org/kdayton-/planify.png?branch=master)](http://travis-ci.org/kdayton-/planify) [![Code Climate](https://codeclimate.com/github/kdayton-/planify.png)](https://codeclimate.com/github/kdayton-/planify) [![Coverage Status](https://coveralls.io/repos/kdayton-/planify/badge.png)](https://coveralls.io/r/kdayton-/planify)
========

Make subscription plans and enforce their limits with Planify.

## Requirements

Ruby:
* 1.9.3
* 2.0.0
* JRuby (1.9 mode)
* Rubinius (1.9 mode)

Mongoid 3 (Support for other ORMs will be a future improvement)

## Installation

Add this line to your application's Gemfile:

    gem 'planify'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install planify

## Setup 

First, we'll define a `Limitable`. Limitables are classes which can be limited based on plan settings.

```ruby
# app/models/widget.rb
class Widget
  include Mongoid::Document
  include Planify::Limitable
  ...
end
```

Next we define a Plan. Plans hold information about how many instances of a Limitable can be created, as well as features which are available to users subscribed to this plan:

```ruby
# config/initializers/plans.rb
Planify::Plans.define :starter do
  name "Starter Plan"
  price 100 # $1 (Price in cents)

  max Widget, 100 # Can only create up to 100 widgets before needing to upgrade

  feature :ajax_search
end
```

Next, create a User class with the `Planify::User` mixin. This will add `Limitable` counting and plan storage.

```ruby
class User
  include Mongoid::Document
  include Planify::User
  ...
end
```

Then assign the user a plan:

```ruby
@user = User.create
@user.has_plan :starter
```


## Usage

After creating your Limitables, Plan, and User models, you are ready to start enforcing limits.

```ruby
# widgets_controller.rb

def create
  @user = current_user
  @widget = Widget.create(params[:widget])

  if @user.can_create? @widget # User has not hit their Widget cap
    @widget.save
    @user.created :widget
  end
end

def destroy
  @user = current_user
  @widget = Widget.find(params[:id])

  @widget.destroy
  @user.destroyed @widget
end
```

You can also test for features:

```ruby<commit_msg>Added more documentation to the README<commit_after>Planify [![Build Status](https://secure.travis-ci.org/kdayton-/planify.png?branch=master)](http://travis-ci.org/kdayton-/planify) [![Code Climate](https://codeclimate.com/github/kdayton-/planify.png)](https://codeclimate.com/github/kdayton-/planify) [![Coverage Status](https://coveralls.io/repos/kdayton-/planify/badge.png)](https://coveralls.io/r/kdayton-/planify)
========

Make subscription plans and enforce their limits with Planify.

## Requirements

Ruby:
* 1.9.3
* 2.0.0
* JRuby (1.9 mode)
* Rubinius (1.9 mode)

Mongoid 3 (Support for other ORMs will be a future improvement)

## Installation

Add this line to your application's Gemfile:

    gem 'planify'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install planify

## Setup 

First, we'll define a `Limitable`. Limitables are classes which can be limited based on plan settings.

```ruby
# app/models/widget.rb
class Widget
  include Mongoid::Document
  include Planify::Limitable
  ...
end
```

Next we define a Plan. Plans hold information about how many instances of a Limitable can be created, as well as features which are available to users subscribed to this plan:

```ruby
# config/initializers/plans.rb
Planify::Plans.define :starter do
  max Widget, 100 # Can only create up to 100 widgets before needing to upgrade

  feature :ajax_search
end
```

Next, create a User class with the `Planify::User` mixin. This will add `Limitable` counting and plan storage.

```ruby
class User
  include Mongoid::Document
  include Planify::User
  ...
end
```

Then assign the user a plan:

```ruby
@user = User.create
@user.has_plan :starter
```

You can also assign user-specific overrides to plan limits and features:

```ruby
# This user has half the widgets and no ajax-search
@user.has_plan :starter do
  max Widget, 50
  feature :ajax_search, false
end
```

## Usage

After creating your Limitables, Plan, and User models, you are ready to start enforcing limits.

```ruby
# widgets_controller.rb

def create
  @user = current_user
  @widget = Widget.create(params[:widget])

  if @user.can_create? @widget # User has not hit their Widget cap
    @widget.save
    @user.created :widget
  end
end

def destroy
  @user = current_user
  @widget = Widget.find(params[:id])

  @widget.destroy
  @user.destroyed @widget
end
```

You can also test for features:

```ruby
{'fraction_non_alphanumeric': 0.11518550474547024, 'fraction_numerical': 0.008628127696289905, 'mean_word_length': 4.095604395604395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2381040', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1467, 'n_words': 504}