Sha256: fb356ceb90569a3a069ee9b0057edb9e1ee1bfe08a8348cb45bcca925c076d1b

Contents?: true

Size: 820 Bytes

Versions: 9

Compression:

Stored size: 820 Bytes

Contents

module ActiveHouse
  module Selectable
    extend ActiveSupport::Concern

    included do
      private

      def build_select_query_part
        if !@fields.empty?
          "SELECT\n#{@fields.join(",\n")}"
        else
          'SELECT *'
        end
      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

9 entries across 9 versions & 1 rubygems

Version Path
active_house-0.4.0 lib/active_house/selectable.rb
active_house-0.3.2 lib/active_house/selectable.rb
active_house-0.3.1 lib/active_house/selectable.rb
active_house-0.3.0 lib/active_house/selectable.rb
active_house-0.2.2 lib/active_house/selectable.rb
active_house-0.2.1 lib/active_house/selectable.rb
active_house-0.2.0 lib/active_house/selectable.rb
active_house-0.1.5 lib/active_house/selectable.rb
active_house-0.1.4 lib/active_house/selectable.rb