Module: ActiveRecord::WithFilledFieldScope::Base

Defined in:
lib/with_filled_field_scope.rb

Constant Summary collapse

FIELD_TYPES_SUPPORTING_ONLY_NOT_NULL =
[:date, :decimal, :boolean]

Instance Method Summary collapse

Instance Method Details

#field_column_type(field, model = self) ⇒ Object



25
26
27
# File 'lib/with_filled_field_scope.rb', line 25

def field_column_type(field, model = self)
  model.columns_hash[field.to_s].type
end

#field_support_not_blank?(field, model = self) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/with_filled_field_scope.rb', line 21

def field_support_not_blank?(field, model = self)
  !FIELD_TYPES_SUPPORTING_ONLY_NOT_NULL.include?(field_column_type(field))
end

#has_with_filled_field_scope(model = self) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/with_filled_field_scope.rb', line 11

def has_with_filled_field_scope(model = self)
  alias_scope :with_filled_field, lambda { |field|
    if field_support_not_blank?(field, model)
      send("#{field.to_s}_not_blank")
    else
      send("#{field.to_s}_not_null")
    end
  }
end