Sha256: eaf67cfc46ba4a47a2c122d84e7e1dbcd332ab25eb57b39df3e9f3ec6831a390

Contents?: true

Size: 771 Bytes

Versions: 2

Compression:

Stored size: 771 Bytes

Contents

module PushType
  module Fields
    module Relations
      
      def json_key
        suffix = multiple? ? '_ids' : '_id'
        (name.singularize + suffix).to_sym
      end

      def relation_option
        @opts[:relation_class]
      end

      def relation_root
        (relation_option || name.singularize).to_s.classify.constantize
      end

      def relation_tree
        flatten_tree relation_root.hash_tree
      end

      private

      def flatten_tree(hash_tree, d = 0)
        hash_tree.flat_map do |parent, children|
          [
            {
              value:  parent.id,
              text:   parent.title,
              depth:  d
            },
            flatten_tree(children, d+1)
          ]
        end.flatten
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
push_type_core-0.5.0.alpha.5 lib/push_type/fields/relations.rb
push_type_core-0.5.0.alpha.4 lib/push_type/fields/relations.rb