Sha256: 7d4c9d37734840d8481884a2e6396b2c98cdd482038e6d569b09c7d8971a2154

Contents?: true

Size: 892 Bytes

Versions: 4

Compression:

Stored size: 892 Bytes

Contents

module ActiveHouse
  module Selectable
    extend ActiveSupport::Concern

    included do
      private

      def build_select_query_part
        if !@fields.empty?
          query_data = ['SELECT']
          query_data.concat @fields
        else
          query_data = ['SELECT *']
        end
        query_data.join("\n")
      end
    end

    def initialize(*)
      @fields = []
      super
    end

    def select(*fields)
      raise ArgumentError, 'wrong number of arguments' if fields.empty?
      formatted_fields = fields.map do |field|
        if field.is_a?(Symbol) && model_class._attribute_opts.key?(field)
          opts = model_class._attribute_opts.fetch(field)
          opts.key?(:select) ? "#{opts[:select]} AS #{field}" : field.to_s
        else
          field.to_s
        end
      end
      chain_query fields: (@fields + formatted_fields).uniq
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_house-0.1.3 lib/active_house/selectable.rb
active_house-0.1.2 lib/active_house/selectable.rb
active_house-0.1.1 lib/active_house/selectable.rb
active_house-0.1.0 lib/active_house/selectable.rb