Module: GiftWrap::ActiveRecordPresenter::ClassMethods

Defined in:
lib/gift_wrap/active_record_presenter.rb

Instance Method Summary collapse

Instance Method Details

#unwrap_columns_for(active_record_model, attribute: true, **options) ⇒ Object

Sets delegate methods via ::unwrap_for en masse for all columns. The :attribute keyword argument can specify which columns are attributes in one of three ways from different values:

  • true: all columns will be considered attributes

  • false: no columns will be considered attributes

  • A hash with the key :only, whose value specifies which columns to consider as attributes, either singular or as an array of column names.

  • A hash with the key :except, whose value specifies which columns to consider as attributes, either singular or as an array of column names.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gift_wrap/active_record_presenter.rb', line 22

def unwrap_columns_for(active_record_model, attribute: true, **options)
  columns = active_record_model.columns.map { |col| col.name.to_sym }
  if true == attribute || false == attribute
    unwrap_for(*columns, attribute: attribute, **options)
  elsif Hash === attribute
    as_attributes, not_attributes = partition_columns_for_attributes(columns, attribute)
    unwrap_for(*as_attributes,  attribute: true,  **options)
    unwrap_for(*not_attributes, attribute: false, **options)
  else
    unwrap_for(*columns, attribute: attribute, **options)
  end
end