Banyan

Installation

In Gemfile

gem 'banyan', :git => 'git://github.com/partnerpedia/banyan.git'

Then:

$ bundle install
$ rails generate banyan
$ rake db:migrate

This will install Banyan and create required database migration.

Usage of Categories

Each ActiveRecord can have assigned categories. In order to do that you can add this line to model:

class MyModel < ActiveRecord::Base
  acts_as_categorizable
end

This will create "categories" association, which will be able to assign multiple Banyan::Category objects.

my_model = MyModel.new
my_model.categories # => []
my_model.categories << Banyan::Category.first

You can also specify that you want only one Category per model instance:

class MyModel < ActiveRecord::Base
  acts_as_categorizable :single => true
end

In that case "category" association will be created:

my_model = MyModel.new
my_model.category # => nil
my_model.category = Banyan::Category.first

Each Banyan::Category object can have following attributes:

  • [string] tag: this field is indexed and is each to search by for specific Category
  • [string] name: this is translated(via Globalize3) field, used to describe Category for end user
  • [collection] categories: each Category can have children
  • [collection] category_groups: each Category can have multiple CategoryGroups(described later). This is many-to-many association

If you want to access your model list for specified category then special association is created using your model name and "_categorizations" suffix:

category = Banyan::Category.first
category.my_model_categorizations # => []

You can change this behavior by by providing "as" options:

class MyModel < ActiveRecord::Base
  acts_as_categorizable :as => "my_associations"
end

category = Banyan::Category.first
category.my_associations # => []

Usage of Category Groups

Category Groups works with similar way that Categories. The main difference is that each CategoryGroup can have only one object attached(instead of collection for Category)

class MyModel < ActiveRecord::Base
  acts_as_group_categorizable
end

my_model = MyModel.first

category_group = Banyan::CategoryGroup.first
category_group.group_categorizable # => nil
category_group.group_categorizable = my_model

my_model.category_groups # => [category_group]

Banyan::CategoryGroup have exactly the same attributes as Banyan::Category(see earlier).

License

Banyan is released under the MIT license.