Sha256: 0943aee18b310b85d20190499f6678592fad6bdebc893d2c62be20cb4213ab64

Contents?: true

Size: 802 Bytes

Versions: 8

Compression:

Stored size: 802 Bytes

Contents

module PushType
  class TagListQuery

    def initialize(name, type)
      @name = name
      @type = type
    end

    def all(opts = {})
      @opts = { type: @type }.merge(opts)
      connection.select_all(query).rows.flatten
    end

    def node_types
      PushType.subclasses_from_list(:node, @opts[:type]).map { |n| "'#{ n.camelcase }'" }
    end

    private

    def connection
      PushType::Node.connection
    end

    def query
      [
        "SELECT DISTINCT jsonb_array_elements_text(field_store->'#{ @name }') AS _tag",
        "FROM push_type_nodes",
        where_sql,
        "ORDER BY _tag"
      ].compact.join(' ')
    end

    def where_sql
      "WHERE push_type_nodes.type IN (#{ node_types.join(', ') })" unless @opts[:type] == :all || node_types.blank?
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
push_type_core-0.5.0 lib/push_type/tag_list_query.rb
push_type_core-0.5.0.alpha.5 lib/push_type/tag_list_query.rb
push_type_core-0.5.0.alpha.4 lib/push_type/tag_list_query.rb
push_type_core-0.5.0.alpha.3 lib/push_type/tag_list_query.rb
push_type_core-0.5.0.alpha.2 lib/push_type/tag_list_query.rb
push_type_core-0.5.0.alpha.1 lib/push_type/tag_list_query.rb
push_type_core-0.4.0 lib/push_type/tag_list_query.rb
push_type_core-0.4.0.beta.3 lib/push_type/tag_list_query.rb