module Pluckers::Features::Base::SimpleAttributes

This module implements fetching simple attributes (columns) from the database through the AR's pluck method.

The options used in this feature are:

* attributes: Names of attributes of the objects to be plucked. This
  attributes should be the names of the columns in the database.

Public Instance Methods

build_results() click to toggle source

We don't need to perform any extra operation as the pluck is executed in the Pluckers::Base class. We could omit this definition, but leave it for example purposes.

Calls superclass method
# File lib/pluckers/features/base/simple_attributes.rb, line 52
def build_results
  super
end
configure_query() click to toggle source

Here we initialize the simple attributes to be retrieved and checks that those attributes exists in the current scope.

Calls superclass method
# File lib/pluckers/features/base/simple_attributes.rb, line 27
def configure_query
  super

  attributes = @options[:attributes]
  attributes ||= default_attributes

  plucker_attributes = attributes.map(&:to_sym)

  klass_attributes = @records.attribute_names.map(&:to_sym)

  # Validate that all attributes exists in the model
  if (missing_attributes = plucker_attributes - klass_attributes).any?
    raise ArgumentError.new("Plucker attributes '#{missing_attributes.to_sentence}', are missing in #{@records.klass}")
  end

  simple_attributes = plucker_attributes & klass_attributes

  @attributes_to_pluck += simple_attributes.map {|f| { name: f, sql: "#{@records.klass.connection.quote_table_name @records.table_name}.#{f}" }}

end

Private Instance Methods

default_attributes(records = @records) click to toggle source

This private method returns the default attributes that must be retrieved

# File lib/pluckers/features/base/simple_attributes.rb, line 58
        def default_attributes records = @records
  records.attribute_names
end