Module: Snails::ViewHelpers
- Defined in:
- lib/snails.rb
Class Method Summary collapse
Instance Method Summary collapse
- #action ⇒ Object
- #delete_link(options = {}) ⇒ Object
- #form_checkbox(object, field, options = {}) ⇒ Object
-
#form_input(object, field, options = {}) ⇒ Object
forms.
- #form_password(object, field, options = {}) ⇒ Object
- #form_put ⇒ Object
- #form_select_options(list, selected = nil) ⇒ Object
- #form_submit(text = 'Actualizar', classes = '') ⇒ Object
- #form_textarea(object, field, options = {}) ⇒ Object
-
#get_page(counter) ⇒ Object
pagination.
- #partial(name, opts = {}) ⇒ Object
- #post_button(text, path, opts = {}) ⇒ Object
- #show_pager(array, path) ⇒ Object
- #simple_format(text, options = {}) ⇒ Object
-
#tag(name, options = nil, open = false) ⇒ Object
formatting.
- #tag_attributes(options) ⇒ Object
- #view(view_name, opts = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
284 285 286 |
# File 'lib/snails.rb', line 284 def self.included(base) Time.include(RelativeTime) unless Time.instance_methods.include?(:relative) end |
Instance Method Details
#action ⇒ Object
288 289 290 |
# File 'lib/snails.rb', line 288 def action request.path_info.gsub('/','').blank? ? 'home' : request.path_info.gsub('/',' ') end |
#delete_link(options = {}) ⇒ Object
422 423 424 425 426 427 |
# File 'lib/snails.rb', line 422 def delete_link( = {}) ([:text], [:path], .merge({ input_html: "<input type='hidden' name='_method' value='delete' />", css_class: 'danger' })) end |
#form_checkbox(object, field, options = {}) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/snails.rb', line 370 def form_checkbox(object, field, = {}) id, name, index, label = input_base(object, field, ) type = [:type] || :checkbox value = [:value] ? "value='#{[:value]}'" : '' if type.to_sym == :radio checked = object.send(field) == [:value] value += checked ? " selected='selected'" : '' else checked = object.send(field) value += checked ? " checked='true'" : '' end label + raw_input(index, type, id, name, [:placeholder], value) end |
#form_input(object, field, options = {}) ⇒ Object
forms
357 358 359 360 361 362 363 364 |
# File 'lib/snails.rb', line 357 def form_input(object, field, = {}) id, name, index, label = input_base(object, field, ) type = ([:type] || :text).to_sym value = [:value] ? "value='#{[:value]}'" : (type == :password ? '' : "value='#{object.send(field)}'") classes = object.errors[field].any? ? 'has-errors' : '' label + raw_input(index, type, id, name, value, [:placeholder], classes, [:required]) end |
#form_password(object, field, options = {}) ⇒ Object
366 367 368 |
# File 'lib/snails.rb', line 366 def form_password(object, field, = {}) form_input(object, field, {:type => 'password'}.merge()) end |
#form_put ⇒ Object
401 402 403 |
# File 'lib/snails.rb', line 401 def form_put '<input type="hidden" name="_method" value="put" />' end |
#form_select_options(list, selected = nil) ⇒ Object
393 394 395 396 397 398 399 |
# File 'lib/snails.rb', line 393 def (list, selected = nil) list.map do |name, val| val = name if val.nil? sel = val == selected ? 'selected="selected"' : '' "<option #{sel} value='#{val}'>#{name}</option>" end.join("\n") end |
#form_submit(text = 'Actualizar', classes = '') ⇒ Object
405 406 407 408 |
# File 'lib/snails.rb', line 405 def form_submit(text = 'Actualizar', classes = '') @tabindex = @tabindex ? @tabindex + 1 : 1 "<button tabindex='#{@tabindex}' class='primary #{classes}' type='submit'>#{text}</button>" end |
#form_textarea(object, field, options = {}) ⇒ Object
387 388 389 390 391 |
# File 'lib/snails.rb', line 387 def form_textarea(object, field, = {}) id, name, index, label = input_base(object, field, ) style = [:style] ? "style='#{[:style]}'" : '' label + "<textarea #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{object.send(field)}</textarea>" end |
#get_page(counter) ⇒ Object
pagination
305 306 307 308 309 310 311 |
# File 'lib/snails.rb', line 305 def get_page(counter) curr = params[:page].to_i i = (curr == 0 && counter == 1) ? 2 : (curr == 2 && counter == -1) ? 0 : curr + counter i == 0 ? "" : "/page/#{i}" end |
#partial(name, opts = {}) ⇒ Object
292 293 294 295 |
# File 'lib/snails.rb', line 292 def partial(name, opts = {}) partial_name = name.to_s["/"] ? name.to_s.reverse.sub("/", "_/").reverse : "_#{name}" erb(partial_name.to_sym, { layout: false }.merge(opts)) end |
#post_button(text, path, opts = {}) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/snails.rb', line 410 def (text, path, opts = {}) form_id = opts.delete(:form_id) || path.gsub(/[^a-z0-9]/, '_').gsub(/_+/, '_') css_class = opts.delete(:css_class) || '' input_html = opts.delete(:input_html) || '' confirm_text = opts.delete(:confirm_text) || 'Seguro?' submit_val = opts.delete(:value) || text '<form id="' + form_id + '" style="display:inline" method="post" action="' + url(path) + '" onsubmit="return confirm(\'' + confirm_text + '\');"> ' + input_html + ' <button name="submit" type="submit" class="' + css_class + ' button" value="' + submit_val + '">' + text + '</button> </form>' end |
#show_pager(array, path) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/snails.rb', line 313 def show_pager(array, path) # remove page from path path = (env['SCRIPT_NAME'] + path.gsub(/[?|&|\/]page[=|\/]\d+/,'')) prevlink = '<li>' + link_to("#{path}#{get_page(-1)}", '← Prev').sub('//', '/') + '</li>' nextlink = array.count != Routes::PER_PAGE ? "" : '<li>' + link_to("#{path}#{get_page(1)}", 'Next →').sub('//', '/') + '</li>' str = params[:page] ? prevlink + nextlink : nextlink str != "" ? "<ul class='pager'>" + str + "</ul>" : '' end |
#simple_format(text, options = {}) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/snails.rb', line 342 def simple_format(text, = {}) t = .delete(:tag) || :p start_tag = tag(t, , true) text = text.to_s.dup text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline -> paragraph text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br text.insert 0, start_tag text << "</#{t}>" text end |
#tag(name, options = nil, open = false) ⇒ Object
formatting
328 329 330 331 |
# File 'lib/snails.rb', line 328 def tag(name, = nil, open = false) attributes = tag_attributes() "<#{name}#{attributes}#{open ? '>' : ' />'}" end |
#tag_attributes(options) ⇒ Object
333 334 335 336 337 338 339 340 |
# File 'lib/snails.rb', line 333 def tag_attributes() return '' unless .inject('') do |all,(key,value)| next all unless value all << ' ' if all.empty? all << %(#{key}="#{value}" ) end.chomp!(' ') end |
#view(view_name, opts = {}) ⇒ Object
297 298 299 300 |
# File 'lib/snails.rb', line 297 def view(view_name, opts = {}) layout = request.xhr? ? false : true erb(view_name.to_sym, { layout: layout }.merge(opts)) end |