Module: SampleModels::AttributeSequence

Defined in:
lib/sample_models/attribute_sequence.rb

Defined Under Namespace

Classes: AbstractSource, EmailSource, FirstPassBelongsToSource, InclusionSource, LengthSource, RequiredBelongsToSource, SecondPassBelongsToSource, SimpleSource, UniquenessFilter

Class Method Summary collapse

Class Method Details

.build(pass, model, column, force_unique, force_email_format) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/sample_models/attribute_sequence.rb', line 3

def self.build(pass, model, column, force_unique, force_email_format)
  sequence = source(pass, model, column, force_email_format)
  validations = model.validations(column.name)
  if (v = validations.detect(&:uniqueness?)) || force_unique
    v ||= Model::Validation.new(:validates_uniqueness_of) 
    sequence = UniquenessFilter.new(model, column, v, sequence)
  end 
  sequence
end

.source(pass, model, column, force_email_format) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sample_models/attribute_sequence.rb', line 13

def self.source(pass, model, column, force_email_format)
  validations = model.validations(column.name)
  belongs_to_assocs = model.belongs_to_associations
  if force_email_format
    EmailSource.new
  elsif assoc = belongs_to_assocs.detect { |a| a.foreign_key == column.name }
    if validations.any?(&:presence?)
      RequiredBelongsToSource.new(assoc)
    elsif pass == :first
      FirstPassBelongsToSource.new
    else
      SecondPassBelongsToSource.new(model, assoc)
    end
  elsif validations.any?(&:email_format?)
    EmailSource.new
  elsif v = validations.detect(&:inclusion?)
    InclusionSource.new(v)
  elsif v = validations.detect(&:length?)
    LengthSource.new(v)
  else
    SimpleSource.new(column)
  end
end