Sha256: 396c18fefacadf05310720bdd7c192a1abe8cbba12de8e4b653bef97b455e33b

Contents?: true

Size: 942 Bytes

Versions: 2

Compression:

Stored size: 942 Bytes

Contents

module Parentry
  module Strategy
    module Ltree
      module InstanceMethods
        def parse_parentry(input = parentry)
          input.to_s.split('.').map(&:to_i)
        end

        def compute_parentry
          parent.present? ? "#{parent.parentry}.#{id}" : id.to_s
        end

        def cascade_parentry
          old_path, new_path = saved_changes[parentry_column]
          parentry_scope.where(
            ["#{parentry_column} <@ :tree AND id != :id", { tree: old_path, id: id }]
          ).update_all(
            [
              "#{parentry_column} = :new_path || subpath(#{parentry_column}, nlevel(:old_path))",
              { new_path: new_path, old_path: old_path }
            ]
          )
        end

        def subtree_conditions
          ["#{parentry_column} <@ ?", parentry]
        end

        def ancestor_conditions
          ["#{parentry_column} @> ?", parentry]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
parentry-1.6.0 lib/parentry/strategy/ltree/instance_methods.rb
parentry-1.5.0 lib/parentry/strategy/ltree/instance_methods.rb