Sha256: 4e8edd7d665fc09b7eee619e125eed2dd1a86dfa8af96369b8fc79e9627a8b9d

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Ditty
  module Helpers
    module Views
      def form_control(name, model, opts = {})
        label = opts.delete(:label) || name.to_s.titlecase
        klass = opts.delete(:class) || 'form-control' unless opts[:type] == 'file'
        group = opts.delete(:group) || model.class.to_s.demodulize.underscore
        field = opts.delete(:field) || name
        default = opts.delete(:default) || nil

        attributes = { type: 'text', id: name, name: "#{group}[#{name}]", class: klass }.merge(opts)
        haml :'partials/form_control', locals: {
          model: model,
          label: label,
          attributes: attributes,
          name: name,
          group: group,
          field: field,
          default: default
        }
      end

      def flash_messages(key = :flash)
        return '' if flash(key).empty?
        id = (key == :flash ? 'flash' : "flash_#{key}")
        messages = flash(key).collect do |message|
          "  <div class='alert alert-#{message[0]} alert-dismissable' role='alert'>#{message[1]}</div>\n"
        end
        "<div id='#{id}'>\n" + messages.join + '</div>'
      end

      def delete_form(entity, label = 'Delete')
        locals = { delete_label: label, entity: entity }
        haml :'partials/delete_form', locals: locals
      end

      def pagination(list, base_path)
        locals = {
          next_link: list.last_page? ? '#' : "#{base_path}?page=#{list.next_page}&count=#{list.page_size}",
          prev_link: list.first_page? ? '#' : "#{base_path}?page=#{list.prev_page}&count=#{list.page_size}",
          base_path: base_path,
          list: list
        }
        haml :'partials/pager', locals: locals
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ditty-0.2.2 lib/ditty/helpers/views.rb
ditty-0.2.1 lib/ditty/helpers/views.rb
ditty-0.2.0 lib/ditty/helpers/views.rb