Class: FormField

Inherits:
Object show all
Defined in:
lib/ramaze/contrib/sequel/form_field.rb

Overview

job = Job puts CGI::pretty(FormField.new(job, :contract, show_errors = true, Job::CONTRACTS).to_s)

Defined Under Namespace

Modules: Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(this, field, show_errors = true, hint = nil) ⇒ FormField

show_errors may be a number indicating how many errors it should display at maximum, 0 displays none, -1 all



12
13
14
15
16
17
18
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 12

def initialize(this, field, show_errors = true, hint = nil)
  @this, @field, @show_errors, @hint =
    this, field.to_sym, show_errors, hint
  @db_type = db_type
  @id = "form_#{@field}"
  @value = this.send(@field)
end

Instance Attribute Details

#db_typeObject

Returns the value of attribute db_type.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def db_type
  @db_type
end

#fieldObject

Returns the value of attribute field.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def field
  @field
end

#hintObject

Returns the value of attribute hint.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def hint
  @hint
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def id
  @id
end

#show_errorsObject

Returns the value of attribute show_errors.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def show_errors
  @show_errors
end

#thisObject

Returns the value of attribute this.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def this
  @this
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 7

def value
  @value
end

Instance Method Details

#build_errors(gestalt) ⇒ Object



109
110
111
112
113
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 109

def build_errors(gestalt)
  each_error do |error|
    gestalt.span(:class => :error){ error }
  end
end

#build_tag(gestalt) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 81

def build_tag(gestalt)
  @args = {:id => id, :name => field}

  case hint
  when :password
    password(gestalt)
  when :boolean, :checkbox
    checkbox(gestalt)
  when :textarea
    textarea(gestalt)
  when :file
    file(gestalt)
  when Array, Range
    select(gestalt)
  else
    case db_type
    when 'varchar', 'integer'
      input(gestalt)
    when 'boolean'
      checkbox(gestalt)
    when 'string'
      textarea(gestalt)
    else
      raise "Unsupported type: (#{db_type || hint} : #{field})"
    end
  end
end

#checkbox(gestalt) ⇒ Object



50
51
52
53
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 50

def checkbox(gestalt)
  @args[:checked] = :checked if value
  gestalt.input(@args.merge :type => :checkbox)
end

#each_error(&block) ⇒ Object

TODO: suggest ‘validated?’ feature to sequel



116
117
118
119
120
121
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 116

def each_error(&block)
  return unless show_errors

  this.validate if this.errors.empty?
  this.errors[field].first(show_errors).each(&block)
end

#file(gestalt) ⇒ Object



73
74
75
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 73

def file(gestalt)
  gestalt.input(@args.merge :type => :file)
end

#input(gestalt) ⇒ Object



69
70
71
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 69

def input(gestalt)
  gestalt.input(@args.merge :type => :text, :value => value)
end

#password(gestalt) ⇒ Object



46
47
48
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 46

def password(gestalt)
  gestalt.input(@args.merge :type => :password, :value => value)
end

#select(gestalt) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 55

def select(gestalt)
  gestalt.select @args do
    v = value.to_s
    hint.each do |h|
      h = h.to_s
      if h == v
        gestalt.option(:value => h, :selected => :selected){ h }
      else
        gestalt.option(:value => h){ h }
      end
    end
  end
end

#textarea(gestalt) ⇒ Object



77
78
79
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 77

def textarea(gestalt)
  gestalt.textarea(@args){ value.to_s }
end

#to_sObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ramaze/contrib/sequel/form_field.rb', line 28

def to_s
  unless label = @this.class::FORM_LABEL[field]
    raise("No FORM_LABEL for %p on %p" % [field, @this.class])
  end

  label += ':'
  id = @id
  _self = self

  Ramaze::Gestalt.build{
    div(:class => :pair){
      label(:for => id){ label }
      _self.build_tag(self)
      _self.build_errors(self)
    }
  }
end