Sha256: 66b64407bbe8be5b1b8388d512a1ec9bcb814b081764e06b1f175800bcf3e884

Contents?: true

Size: 971 Bytes

Versions: 2

Compression:

Stored size: 971 Bytes

Contents

module Parentry
  module Strategy
    module Array
      module InstanceMethods
        def parse_parentry(input = parentry)
          input
        end

        def compute_parentry
          return [] unless persisted?

          parent.present? ? parent.parentry + [id] : [id]
        end

        def cascade_parentry
          old_path, new_path = saved_changes[parentry_column]
          parentry_scope.where(
            ["#{parentry_column} @> ARRAY[:tree] AND id != :id", { tree: old_path, id: id }]
          ).update_all(
            [
              "#{parentry_column} = array_cat(ARRAY[?], #{parentry_column}[?:array_length(#{parentry_column}, 1)])",
              new_path, old_path.size + 1
            ]
          )
        end

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

        def ancestor_conditions
          ["#{parentry_column} <@ ARRAY[?]", 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/array/instance_methods.rb
parentry-1.5.0 lib/parentry/strategy/array/instance_methods.rb