`
taito
  • 浏览: 96798 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

acts_as_nested_set的增强版BetterNestedSet

阅读更多
BetterNestedSet 插件是nested_set的增加版。不仅多了一些方法,而且结构更加精简,查询更方便。

==安装

script/plugin install svn://rubyforge.org/var/svn/betternestedset/trunk


==数据结构

例子:
root
    |_ Child 1
      |_ Child 1.1
      |_ Child 1.2
    |_ Child 2
      |_ Child 2.1
      |_ Child 2.2

形象的表示如下图所示:
    ___________________________________________________________________
   |  Root                                                             |
   |    ____________________________    ____________________________   |
   |   |  Child 1                  |   |  Child 2                  |   |
   |   |   __________   _________  |   |   __________   _________  |   |
   |   |  |  C 1.1  |  |  C 1.2 |  |   |  |  C 2.1  |  |  C 2.2 |  |   |
   1   2  3_________4  5________6  7   8  9_________10 11_______12 13  14
   |   |___________________________|   |___________________________|   |
   |___________________________________________________________________|

数据表:

  id | parent_id | lft  | rgt  | data
    1 |           |    1 |   14 | root
    2 |         1 |    2 |    7 | Child 1
    3 |         2 |    3 |    4 | Child 1.1
    4 |         2 |    5 |    6 | Child 1.2
    5 |         1 |    8 |   13 | Child 2
    6 |         5 |    9 |   10 | Child 2.1
    7 |         5 |   11 |   12 | Child 2.2

必要的字段:parent_id,lft,rgt
数据迁移脚本如下:
# TAITO BetterNestedSet Mig
class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :categories do |t|
      t.column :name, :string, :null=>false
      t.column :parent_id, :integer
      t.column :lft,       :integer
      t.column :rgt,       :integer
    end
  end

  def self.down
    drop_table :categories
  end
end


=Model中
class Category < ActiveRecord::Base
  acts_as_nested_set
end


数据的建立和acts_as_nested_set差不多。唯一的区别是添加子级的方法不一样:

如:
root = Category.create(:name => "root")
child = Category.create(:name => "child")

child.move_to_child_of root  # 移动过去



==更多实用方法

root - root item of the tree (the one that has a nil parent; should have left_column = 1 too)
roots - root items, in case of multiple roots (the ones that have a nil parent)
level - number indicating the level, a root being level 0
ancestors - array of all parents, with root as first item
self_and_ancestors - array of all parents and self
siblings - array of all siblings, that are the items sharing the same parent and level
self_and_siblings - array of itself and all siblings
children_count - count of all immediate children
children - array of all immediate childrens
all_children - array of all children and nested children
full_set - array of itself and all children and nested children

==原创的辅助方法
module NestedSetList  
  def first?    
      parent.nil? or parent.lft==self.lft-1 
  end
  def last?
    parent.nil? or parent.rgt==self.rgt+1
  end
  def higher_item
    list = self_and_siblings
    i = list.index(self)
    i==0 ? self : list[ i-1 ]
  end
  def lower_item
    list = self_and_siblings
    i = list.index(self)
    i==list.size-1 ? self : list[ i+1 ]
  end
  def move_higher
    move_to_left_of( higher_item ) if higher_item
  end
  def move_lower
    move_to_right_of( lower_item ) if lower_item
  end
  def move_to_top
    move_to_left_of( self_and_siblings.first )
  end
  def move_to_bottom
    move_to_right_of( self_and_siblings.last )  
  end
end


在model中调用:
  include NestedSetList

==注意

Rails 2.1版本下使用,会有错误,这是因为2.1对于 attributes_with_quotes ,发生了很大变化。

BetterNestedSet Rails2.1 ArgumentError

请修改插件lib/better_nested_set.rb 文件的这一段
......
def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
  left_and_right_column = [acts_as_nested_set_options[:left_column], acts_as_nested_set_options[:right_column]]
  quoted = {}
  connection = self.class.connection
  attribute_names.each do |name|
    if column = column_for_attribute(name)
      quoted[name] = connection.quote(read_attribute(name), column) unless !include_primary_key && (column.primary || left_and_right_column.include?(column.name))
    end
  end
  include_readonly_attributes ? quoted : remove_readonly_attributes(quoted)
end
......

==其它

acts as section map 是一个针对acts树结构的一个扩展插件。可以生成表格形式的结构图.

=安装方法
./script/plugin install http://xibbar.net/svn/rails/plugins/trunk/acts_as_section_map/
4
0
分享到:
评论

相关推荐

    acts_as_list.rar

    acts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_...

    acts_as_authenticated

    "acts_as_authenticated" 是一个经典的Ruby on Rails插件,它为Rails应用提供了用户认证功能。在Rails框架中,用户认证通常涉及验证用户身份、管理会话以及处理登录和登出等操作。acts_as_authenticated插件简化了这...

    acts_as_list, 用于管理列表的ActiveRecord插件.zip

    acts_as_list, 用于管理列表的ActiveRecord插件 ActsAsList描述这个 acts_as 扩展提供了对列表中的多个对象进行排序和重新排序的功能。 具有指定的类的类需要在映射的数据库表上定义为整数的position 列。 0.8.0升级...

    awesome_nested_set:替代acts_as_nested_set和better_nested_set的绝佳选择

    它替代了act_as_nested_set和BetterNestedSet,但功能更强大。 版本3.2支持Rails 6、3.1支持Rails 5和4。版本2支持Rails3。2.0之前的Gem版本支持Rails 2。 是什么让它如此出色? 这是基于BetterNestedSet的嵌套集...

    acts_as_paranoid

    "acts_as_paranoid" 是一个在Ruby on Rails框架中常用的gem,用于处理数据库记录的软删除(soft delete)。在数据库操作中,通常我们会遇到删除数据的需求,但直接删除可能会导致数据丢失,尤其是在生产环境中。...

    用acts_as_rateable实现简单的打分功能

    在Ruby on Rails框架中,`acts_as_rateable`是一个非常实用的插件,它允许用户对模型进行评分,从而实现简单的打分功能。这个插件是Rails社区中的一个开源项目,旨在简化应用中的评分系统集成。在本篇讨论中,我们将...

    acts_as_category:想想acts_as_tree +权限

    #ActsAsCategory acts_as_category (Version 2.0 beta)acts_as_category,是acts_as插件在acts_as_tree风格的Ruby on Rails的ActiveRecord的模式,但有一些额外的功能,以及多种便捷视图助手。例子(有关实例方法和...

    acts_as_commentable_with_threading:类似于acts_as_commentable; 然而,利用 awesome_nested_set 提供线程评论

    与acts_as_commentable 兼容(但需要更改数据库架构) 要求 此 gem 的 2.x 版本仅适用于 Rails 4 及更高版本。 对于此 gem 的 Rails 3.x 兼容版本,请使用版本 1.2.0。 这个宝石取决于 CollectiveIdea 的 Awesome ...

    rails_admin_acts_as_list:rails_admin插件以对记录进行排序

    介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...

    acts_as_restful_list:就像acts_as_list一样,但很安静

    acts_as_restful_list 就像acts_as_list 一样,但不必使用非标准方法调用(如insert_at)来弄乱您的代码,acts_as_restful_list 使管理列表变得简单。 您可以像更新其他任何内容一样更新 position 属性,其余的都...

    active_record-acts_as, 模拟activerecord模型的多表继承.zip

    active_record-acts_as, 模拟activerecord模型的多表继承 插件开发已经移动这个项目的积极发展已经转移到了的krautcomputing fork 。 请在那里报告问题并打开 PRs ! ActiveRecord::ActsAs这是对 acts_as_relation的...

    acts_as_paranoid:ActiveRecord插件可让您隐藏和还原记录,而无需实际删除它们

    支持该分支仅针对Rails 5.2+和Ruby 2.4+ 如果您使用的是Rails 5.1或更早的版本,或者Ruby 2.3或更早的版本,请切换到相应的分支,或者需要一个更旧版本的acts_as_paranoid gem。已知的问题在同一模型上使用acts_as_...

    Api-acts_as_api.zip

    Api-acts_as_api.zip,使在rails中创建api响应变得简单和有趣,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到对象中,api简化了编程...

    acts_as_liked:向任何 Active Record 模型添加类似功能

    gem 'acts_as_liked' 然后执行: $ bundle 运行生成器: $ rails generate acts_as_liked 并且不要忘记迁移您的数据库 $ rake db:migrate 用法 可爱的模特 将acts_as_likeable添加到任何模型,它的实例可以被...

    actions_as_commentable:ActiveRecord acts_as_commentable插件

    gem 'acts_as_commentable' Rails gem 'acts_as_commentable' , '3.0.1' Rails gem 'acts_as_commentable' , git: 'git@github.com:jackdempsey/acts_as_commentable.git' , branch: '2.x' 生成器 Rails 3+ ...

    acts_as_aliased:扩展 ActiveRecord

    gem 'acts_as_aliased' 更新包后,运行 rails generate acts_as_aliased:install rake db:migrate 这将创建一个新表aliases 。 用法 假设您有一个需要别名的模型Company ,因为公司名称有不同的版本。 使用acts_as...

    acts_as_nested_interval:将树保存在表中,速度快,并且可以在数据库级别进行预排序

    ActsAsNestedInterval 关于 该行为实现了嵌套间隔树。 只需一个选择查询,您就可以找到所有后代或所有祖先。 您可以在没有完整表更新的情况下插入和删除记录(与...gem 'acts_as_nested_interval' , '~&gt; 0.1.1' # This

    acts_as_xapian:Xapian全文搜索插件,适用于Ruby on Rails

    《acts_as_xapian:为Ruby on Rails打造的Xapian全文搜索引擎插件》 在Web开发领域,全文搜索引擎能够极大地提升用户对信息的检索效率,对于内容丰富的应用尤其重要。Ruby on Rails作为流行的Web框架,有着众多的...

    acts_as_shopping_cart:简单的购物车实施

    acts_as_shopping_cart 一个简单的购物车实现。 您可以找到示例应用程序。 安装 滑轨3 从0.2.0版开始,不再支持Rails 3。 如果您仍需要在Rails 3应用程序中实现此gem,请使用0-1-x分支 将其包含在您的Gemfile中 ...

Global site tag (gtag.js) - Google Analytics