Sha256: d49d09a8dd46f0f75ab30a3e4ac09e875565e892eb131a479abc52bc83464d81

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module Georgia
  class PermissionTablePresenter < Presenter

    def initialize context, title, actions
      super
      @title = title
      @actions = actions
    end

    def to_s
      html = ActiveSupport::SafeBuffer.new
      html << title_tag
      html << table
      html.to_s.html_safe
    end

    private

    def title_tag
      content_tag :h2, @title.to_s.titleize
    end

    def table
      content_tag :table, class: 'table table-results' do
        thead + tbody
      end
    end

    def thead
      content_tag(:thead) do
        content_tag(:tr) do
          content_tag(:th, 'Actions', class: 'permissions-actions') +
          roles.map{|role| content_tag(:th, role.to_s.titleize, class: 'role') }.join.html_safe
        end
      end
    end

    def tbody
      content_tag(:tbody, action_rows)
    end

    def action_rows
      @actions.map do |action, permissions|
        content_tag(:tr) do
          content_tag(:td, action.to_s.titleize) +
          roles.map{|role| permission_cell(permissions[role.to_sym]) }.join.html_safe
        end
      end.join.html_safe
    end

    def permission_cell permission
      content_tag(:td, permission_icon(permission), class: "text-center #{permission_class(permission)}")
    end

    def permission_icon permission
      icon_tag(
        case permission
        when true then 'check'
        when false then 'times'
        when :partial then 'minus'
        else 'times'
        end
      )
    end

    def permission_class permission
      case permission
      when true then 'success'
      when false then 'danger'
      when :partial then 'warning'
      else 'danger'
      end
    end

    def roles
      @roles ||= Georgia::Role.pluck(:name)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
georgia-0.8.0 app/presenters/georgia/permission_table_presenter.rb