Method: ModelManage::ClassMethods#field

Defined in:
lib/model_manage/base.rb

#field(name, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/model_manage/base.rb', line 23

def field(name, options = {})
  super

  min =   1
  validates = {}
  validates[:in] = options.delete(:in)  || options.delete(:within)
  if options[:limit] && (! validates[:in])
    validates[:in] = (min..options.delete(:limit)) 
  end

  validates[:allow_nil  ] = options.delete(:allow_nil)   || false
  validates[:allow_blank] = options.delete(:allow_blank) || false

  null_ok = validates[:allow_nil] || validates[:allow_blank]
  form_attributes = {
    owner:   self,
    name:    name.to_s,
    type:    options[:type] || String,
    limit:   nil,
    null:    null_ok,
    primary: key?(name),
    scale:   nil
  }

  if validates[:in].present?
    validates_length_of name, validates
    form_attributes[:limit] = validates[:in].max
    form_attributes.merge!(validates)
  end


  if options[:hidden]
    forms.delete(name)
  else
    forms[name] = OpenStruct.new(form_attributes)
  end
end