Class: Egalite::FormHelper

Inherits:
Object show all
Includes:
HTMLTagBuilder
Defined in:
lib/egalite/helper.rb

Instance Method Summary collapse

Methods included from HTMLTagBuilder

#_tag, a, #escape_html, li, ol, tag, #tag_close, #tag_open, #tag_solo, ul

Constructor Details

#initialize(data = {}, param_name = nil, opts = {}) ⇒ FormHelper

Returns a new instance of FormHelper.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/egalite/helper.rb', line 121

def initialize(data = {}, param_name = nil, opts = {})
  @data = lambda { |k|
    if data.respond_to?(k)
      data.send(k)
    elsif data.respond_to?(:[])
      data[k]
    end
  }

  @param_name = param_name
  @form_opts = opts
end

Instance Method Details

#_text(value, name, opts) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/egalite/helper.rb', line 142

def _text(value, name, opts)
  attrs = opt_as_hash(opts)
  attrs[:value] = value if value
  attrs[:size] ||= 30
  attrs[:type] = 'text'
  attrs[:name] = expand_name(name)
  tag_solo(:input, attrs)
end

#checkbox(name, value = "true", opts = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/egalite/helper.rb', line 176

def checkbox(name, value="true", opts = {})
  checked = (@data[name] || opts[:default] || opts[:checked])
  checked = false if @data[name] == false
  
  attr_cb = opt_as_hash(opts)
  attr_cb[:type] = 'checkbox'
  attr_cb[:name] = expand_name(name)
  attr_cb[:value] = value
  attr_cb[:checked] = "checked" if checked

  ucv = opts[:uncheckedvalue] || 'false'
  attr_h = {:type => 'hidden', :name => expand_name(name), :value => ucv}
  hidden = opts[:nohidden] ? '' : tag_solo(:input, attr_h)
  
  raw "#{hidden}#{tag_solo(:input, attr_cb)}"
end

#closeObject



139
140
141
# File 'lib/egalite/helper.rb', line 139

def close
  tag_close(:form,nil)
end

#file(name, opts = {}) ⇒ Object



208
209
210
211
212
213
# File 'lib/egalite/helper.rb', line 208

def file(name, opts = {})
  attrs = opt_as_hash(opts)
  attrs[:name] = expand_name(name)
  attrs[:type] = 'file'
  tag_solo(:input, attrs)
end

#form(method, url = nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/egalite/helper.rb', line 133

def form(method, url=nil)
  attrs = opt_as_hash(@form_opts)
  attrs[:method] = method.to_s.upcase
  attrs[:action] = url if url
  tag_open(:form,attrs)
end

#hidden(name, opts = {}) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/egalite/helper.rb', line 168

def hidden(name, opts = {})
  value = @data[name] || opts[:default]
  attrs = opt_as_hash(opts)
  attrs[:value] = value if value
  attrs[:type] = "hidden"
  attrs[:name] = expand_name(name)
  tag_solo(:input,attrs)
end

#imageObject



221
222
# File 'lib/egalite/helper.rb', line 221

def image
end

#opt(opts) ⇒ Object



105
106
107
108
109
110
# File 'lib/egalite/helper.rb', line 105

def opt(opts)
  opts.map { |k,v|
    next "" if [:default,:checked,:selected, :nil].member?(k)
    " #{escape_html(k)}='#{escape_html(v)}'"
  }.join
end

#opt_as_hash(opts) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/egalite/helper.rb', line 111

def opt_as_hash(opts)
  o = opts.dup
  o.each_key { |k|
    o.delete(k) if [:default,:checked,:selected, :nil].member?(k)
  }
  o
end

#password(name, opts = {}) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/egalite/helper.rb', line 160

def password(name, opts = {})
  value = @data[name] || opts[:default]
  attrs = opt_as_hash(opts)
  attrs[:value] = value if value
  attrs[:type] = "password"
  attrs[:name] = expand_name(name)
  tag_solo(:input,attrs)
end

#radio(name, choice, opts = {}) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/egalite/helper.rb', line 192

def radio(name, choice, opts = {})
  selected = (@data[name] == choice)
  selected = (opts[:default] == choice) || opts[:selected] || opts[:checked] if @data[name] == nil
  
  attrs = opt_as_hash(opts)
  attrs[:checked] = 'checked' if selected
  attrs[:name] = expand_name(name)
  attrs[:value] = choice
  attrs[:type] = 'radio'
  
  tag_solo(:input, attrs)
end

#select_by_array(name, options, opts = {}) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/egalite/helper.rb', line 223

def select_by_array(name, options, opts = {})
  optionstr = options.map {|o|
    (value,optname) = if o.is_a?(Array)
      o
    else
      [o,o]
    end
    flag = value == @data[name]
    a = {:value => value}
    a[:selected] = 'selected' if flag
    "#{tag_open(:option, a)}#{escape_html(optname)}</option>"
  }.join('')
  
  raw "<select name='#{expand_name(name)}'#{opt(opts)}>#{optionstr}</select>"
end

#select_by_association(name, options, optname, opts = {}) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/egalite/helper.rb', line 238

def select_by_association(name, options, optname, opts = {})
  idname = (opts[:idname] || "id").to_sym
  optionstr = options.map {|o|
    flag = o[idname] == @data[name]
    a = {:value => o[idname]}
    a[:selected] = 'selected' if flag
    "#{tag_open(:option, a)}#{escape_html(o[optname])}</option>"
  }.join('')
  
  if opts[:nil]
    selected = (@data[name] == nil) ? "selected='selected'" : ''
    optionstr = "<option value='' #{selected}>#{escape_html(opts[:nil])}</option>" + optionstr
  end
  
  raw "<select name='#{expand_name(name)}'#{opt(opts)}>#{optionstr}</select>"
end

#submit(value = nil, name = nil, opts = {}) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/egalite/helper.rb', line 214

def submit(value = nil, name = nil, opts = {})
  attrs = opt_as_hash(opts)
  attrs[:name] = expand_name(name) if name
  attrs[:value] = value if value
  attrs[:type] = 'submit'
  tag_solo(:input, attrs)
end

#text(name, opts = {}) ⇒ Object



150
151
152
# File 'lib/egalite/helper.rb', line 150

def text(name, opts = {})
  _text(@data[name] || opts[:default], name, opts)
end

#textarea(name, opts = {}) ⇒ Object



204
205
206
207
# File 'lib/egalite/helper.rb', line 204

def textarea(name, opts = {})
  value = escape_html(@data[name] || opts[:default])
  raw "<textarea name='#{expand_name(name)}'#{opt(opts)}>#{value}</textarea>"
end

#timestamp_text(name, opts = {}) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/egalite/helper.rb', line 153

def timestamp_text(name, opts = {})
  # todo: enable locale
  # todo: could unify to text()
  value = @data[name] || opts[:default]
  value = value.strftime('%Y-%m-%d %H:%M:%S')
  _text(value,name,opts)
end