Sha256: 754f8cb03c9dec817b620ed0037558fb607871025e21c2524e95db05b7f10efb

Contents?: true

Size: 795 Bytes

Versions: 5

Compression:

Stored size: 795 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.node_types_from_list(@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

5 entries across 5 versions & 1 rubygems

Version Path
push_type_core-0.3.3 lib/push_type/tag_list_query.rb
push_type_core-0.3.1 lib/push_type/tag_list_query.rb
push_type_core-0.2.1 lib/push_type/tag_list_query.rb
push_type_core-0.2.0 lib/push_type/tag_list_query.rb
push_type_core-0.2.0.beta2 lib/push_type/tag_list_query.rb