0.0
No commit activity in last 3 years
No release in over 3 years
Hierarchical ActiveRecord models. Inspired by acts_as_tree. Trees are shady.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.0.0
 Project Readme

ActsAsKing¶ ↑

Rails gem to provide self referencing hierarchy for ActiveRecord models.

Installation¶ ↑

gem install acts_as_king

More preferrably in your Gemfile

gem 'acts_as_king'

Usage¶ ↑

The target for ActsAsKing is to be an easy to use, self explanitory gem.

Create a migration to add parent_id to your model:

rails g migration AddParentIdToComments

Add a column to your comments table

class AddParentIdToComments < ActiveRecord::Migration
  def self.up
    add_column :comments, :parent_id, :integer
    add_index :comments, :parent_id
  end
end

In your model:

class Comment < ActiveRecord::Base
  acts_as_king
end

Create a couple of comments

comment_1 = Comment.create # id: 1
comment_2 = Comment.create(:parent => comment_1) # id: 2

Get all top level comments

comments = Comment.kings
=> [#<Comment id: 1, parent_id: nil>]

Get the first top level comment and display the immediate children

comment = Comment.kings.first
comment.children
=> [#<Comment id: 2, parent_id: 1>]

Contributing¶ ↑

Contributing is easy. Fork the repository. Hack away. Create tests. Send a pull request.