Module: ModelManage::ClassMethods

Defined in:
lib/model_manage/base.rb

Instance Method Summary collapse

Instance Method Details

#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

#formsObject



12
13
14
# File 'lib/model_manage/base.rb', line 12

def forms
  const_get(:FORMS)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/model_manage/base.rb', line 16

def key?(key)
  const_get(:KEYS).member? key.to_s
rescue
  const_set :KEYS, []
  false
end

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



61
62
63
64
65
66
67
68
69
# File 'lib/model_manage/base.rb', line 61

def relation_form_set(name, options = {})
  relation_attributes = {
    owner:    self,
    name:     name.to_s,
    options:  options
  }.tap{|o| o[:data] = o.dup }
  relation = relations[name.to_s]
  relation.form = OpenStruct.new(relation_attributes)
end