Module: SnapshotTree::ActsAsTree::ClassMethods
- Defined in:
- lib/snapshot_tree/acts_as_tree.rb
Instance Method Summary collapse
- #acts_as_tree(opts = {}) ⇒ Object
- #ancestor_nodes(*args) ⇒ Object
- #descendent_nodes(*args) ⇒ Object (also: #descendant_nodes)
- #leaf_nodes(*args) ⇒ Object
- #root_nodes(*args) ⇒ Object
Instance Method Details
#acts_as_tree(opts = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/snapshot_tree/acts_as_tree.rb', line 43 def acts_as_tree(opts = {}) = { parent_key: :parent_id, child_key: :child_id, snapshot_field: :effective_on, is_active_field: :is_active, node_prefix: :node, dependent: :nullify }.merge(opts) [:model_class] = self.name [:model_table] = [:model_class].tableize [:join_class] = "#{self.name}Tree" unless [:join_class] [:join_class] = [:join_class].to_s [:join_table] = [:join_class].tableize instance_variable_set :@tree_helper, TreeHelper.new() has_many :parent_tree_nodes, -> { where([:is_active_field].to_sym => true) if [:is_active_field] }, class_name: [:join_class], foreign_key: [:child_key], autosave: true, dependent: [:dependent] has_many :child_tree_nodes, -> { where([:is_active_field].to_sym => true) if [:is_active_field] }, class_name: [:join_class], foreign_key: [:parent_key], autosave: true, dependent: [:dependent] "#{[:node_prefix]}_depth".to_sym.tap do |field| define_method(field) do read_attribute(field).try(:to_i) end end "#{[:node_prefix]}_path".to_sym.tap do |field| define_method(field) do read_attribute(field).gsub(/[{}]/, '').split(',').map(&:to_i) if read_attribute(field) end end end |
#ancestor_nodes(*args) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/snapshot_tree/acts_as_tree.rb', line 120 def ancestor_nodes(*args) opts = @tree_helper.parse_args(*args) sql = @tree_helper.nodes_query(:ancestor) sql = sql.gsub(/__model_id__/, opts[:model_id].to_s) sql = sql.gsub(/__snapshot_value__/, opts[:as_of].to_s(format: :db)) if @tree_helper.snapshot_field? query = self.unscoped.from(sql) query = query.where("#{@tree_helper.node_field(:depth)} <= #{opts[:depth]}") if opts[:depth] > 0 query = query.order("#{@tree_helper.node_field(:depth)} DESC") end |
#descendent_nodes(*args) ⇒ Object Also known as: descendant_nodes
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/snapshot_tree/acts_as_tree.rb', line 106 def descendent_nodes(*args) opts = @tree_helper.parse_args(*args) sql = @tree_helper.nodes_query(:descendent) sql = sql.gsub(/__model_id__/, opts[:model_id].to_s) sql = sql.gsub(/__snapshot_value__/, opts[:as_of].to_s(format: :db)) if @tree_helper.snapshot_field? query = self.unscoped.from(sql) query = query.where("#{@tree_helper.node_field(:depth)} <= #{opts[:depth]}") if opts[:depth] > 0 query = query.order("#{@tree_helper.node_field(:path)}") end |
#leaf_nodes(*args) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/snapshot_tree/acts_as_tree.rb', line 97 def leaf_nodes(*args) opts = @tree_helper.parse_args(*args) sql = @tree_helper.nodes_query(:leaf) sql = sql.gsub(/__snapshot_value__/, opts[:as_of].to_s(format: :db)) if @tree_helper.snapshot_field? self.unscoped.from(sql).order('1') end |
#root_nodes(*args) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/snapshot_tree/acts_as_tree.rb', line 88 def root_nodes(*args) opts = @tree_helper.parse_args(*args) sql = @tree_helper.nodes_query(:root) sql = sql.gsub(/__snapshot_value__/, opts[:as_of].to_s(format: :db)) if @tree_helper.snapshot_field? self.unscoped.from(sql).order('1') end |