Code Monkey home page Code Monkey logo

hide_ancestry's Introduction

Build Status

HideAncestry

This gem allows hide and restore nodes of ancestry

Usage

State before hiding:

$ User.first.subtree
$ #<ActiveRecord::Relation [
    <id: 1, name: 'Grandpa', hidden_status: false, ancestry: nil, hide_ancestry: 1>,
      <id: 2, name: 'Parent', hidden_status: false, ancestry: '1', hide_ancestry: '1/2'>,
        <id: 3, name: 'Child', hidden_status: false, ancestry: '1/2', hide_ancestry '1/2/3'> ]>

####Hiding

$ User.second.hide

Ancestry subtree became changed:

$ User.first.subtree
$ #<ActiveRecord::Relation [
    <id: 1, name: 'Grandpa', hidden_status: false, ancestry: nil, hide_ancestry: '1'>,
      <id: 3, name: 'Child', hidden_status: false, ancestry: '1', hide_ancestry: '1/2/3'> ]>

$ User.hidden
$ <ActiveRecord::Relation [
    <id: 2, name: 'Parent', hidden_status: true, ancestry: nil, hide_ancestry: '1/2'> ]>

####Restoring hidden node:

$ User.find(2).restore # restore hidden node to previous subtree

$ User.first.subtree
    <id: 1, name: 'Grandpa', hidden_status: false, ancestry: nil, hide_ancestry: '1'>,
      <id: 2, name: 'Parent', hidden_status: false, ancestry: '1', hide_ancestry: '1/2'>,
        <id: 3, name: 'Child', hidden_status: false, ancestry: '1/2', hide_ancestry: '1/2/3'>

User.hidden
$ #<ActiveRecord::Relation []>

####Hiding, updating subtree and restoring hidden node

$ User.find(2).hide
$ User.find_by(name: 'Grandpa').update parent_id: 4

# hide_ancestry of each node of subtree became updated
$ User.find(4).subtree
    <id: 4, name: 'Root User', hidden_status: false, ancestry: nil, hide_ancestry: '4'>,
       <id: 1, name: 'Grandpa', hidden_status: false, ancestry: '1', hide_ancestry: '4/1'>,
          <id: 3, name: 'Child', hidden_status: false, ancestry: '1/2', hide_ancestry: '4/1/2/3'>

# hidden node update its hide_ancestry too
$ User.hidden
$ <ActiveRecord::Relation [
   <id: 2, name: 'Parent', hidden_status: true, ancestry: nil, hide_ancestry: '4/1/2'> ]>

# You can look on subtree with hidden node
$ User.find(4).subtree_with_hidden
    <id: 4, name: 'Root User', hidden_status: false, ancestry: nil, hide_ancestry: '4'>,
       <id: 1, name: 'Grandpa', hidden_status: false, ancestry: '1', hide_ancestry: '4/1'>,
          <id: 2, name: 'Parent', hidden_status: true, ancestry: nil, hide_ancestry: '4/1/2'>,
             <id: 3, name: 'Child', hidden_status: false, ancestry: '1/2', hide_ancestry: '4/1/2/3'>

# Restoring still works correctly
$ User.find(2).restore
$ User.find(4).subtree
    <id: 4, name: 'Root User', hidden_status: false, ancestry: nil, hide_ancestry: '4'>,
       <id: 1, name: 'Grandpa', hidden_status: false, ancestry: '1', hide_ancestry: '4/1'>,
          <id: 2, name: 'Parent', hidden_status: false, ancestry: '1/2', hide_ancestry: '4/1/2'>,
            <id: 3, name: 'Child', hidden_status: false, ancestry: '1/2/3', hide_ancestry: '4/1/2/3'>
  • You can change ancestry subtree as you want after node hiding. hidden node still can be restored to previous parent and still will join correctly it old descendants (unless you changed descendant`s parent).
  • hidden nodes have nil ancestry columns - no any actual parents or descendants present.

Installation

Add to your Gemfile

gem 'ancestry'
gem 'hide_ancestry'

And run

bundle install

To install hide_ancestry migration:

# Set any table with ancestry.
# Type --no-hidden-status if you are going to use your own boolean column for hiding nodes
rails generate hide_ancestry_migration users [--no-hidden-status]
rake db:migrate

It will add to the specified table:

  • old_parent_id:integer
  • old_child_ids:text (will be array - thanks to rails serialize)
  • hide_ancestry:string
  • hidden_status:boolean (unless --no-hidden-status option)

Then add to your model:

class User < ActiveRecord::Base
  has_ancestry
  has_hide_ancestry
end

Now you are able to use hide_ancestry methods.

###Instance methods

hide                      # hide node
restore                   # restore hidden node

hidden?                    # check if node is hidden
hidden_parent_changed?     # check if regular node changed it hidden parent
hidden_children_present?   # check if regular node has hidden children

hidden_parent              # returns hidden parent of regular node (if present)
subtree_with_hidden        # returns subtree of regular node with hidden nodes

children_of_hidden         # returns children of hidden node
hidden_descendants_ids     # returns ids of hidden nodes in subtree of regular node 
hide_ancestry_ids         # old ancestors ids of hidden node
depth_with_hidden          # returns depth of node given hidden nodes

###Scopes

hidden            # returns nodes with hidden_status (or you custom column)
unhidden          # returns nodes without hidden_status
hidden_nodes(ids) # look for hidden nodes with ids
hidden_childs(id) # returns hidden children nodes of node#id

###Options for has_hide_ancestry

# You can delete hidden_status if you use this
use_column: :you_custom_boolean_column

hide_ancestry's People

Contributors

dimkarodinz avatar

Watchers

 avatar  avatar

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.