Sha256: 54a95d4978369f618d71e2951967d3cc84896db9c519bcff5035110de466782a

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

module ExpressAdmin
  module Components
    module Presenters
      module Helper
        COLUMN_REGEX_LIST = {
          link: /(\w+)_link$/,
          checkmark: /(\w+)_checkmark/,
          in_words: /(\w+)_in_words/
        }

        def link(accessor)
          accessor_match(accessor, COLUMN_REGEX_LIST[:link])
        end

        def checkmark(accessor)
          accessor_match(accessor, COLUMN_REGEX_LIST[:checkmark])
        end

        def words(accessor)
          accessor_match(accessor, COLUMN_REGEX_LIST[:in_words])
        end

        def accessor_match(accessor, regex)
          accessor.to_s.match(regex).try(:[], 1)
        end

        def link?(accessor)
          link(accessor).present?
        end

        def checkmark?(accessor)
          checkmark(accessor).present?
        end

        def words?(accessor)
          words(accessor).present?
        end

        def make_link(item, accessor)
          # TODO: only works with non-namespaced routes
          attrib = link(accessor)
          if item.send(attrib).present?
            helpers.link_to item.send(attrib), resource_path(item)
          end
        end

        def make_checkmark(item, accessor)
          attrib = checkmark(accessor)
          "<i class='ion-checkmark-round'></i>".html_safe if item.send(attrib)
        end

        def make_in_words(item, accessor)
          attrib = words(accessor)
          if item.send(attrib)
            if item.send(attrib) < DateTime.now
              "#{helpers.time_ago_in_words(item.send(attrib))} ago"
            else
              "in #{helpers.time_ago_in_words(item.send(attrib))}"
            end
          else
            'never'
          end
        end

      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
express_admin-2.0.0.b app/components/express_admin/helpers/presenters_helper.rb
express_admin-2.0.0.a app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.8.1 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.8.0 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.7.32 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.7.31 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.7.30 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.7.29 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.7.28 app/components/express_admin/helpers/presenters_helper.rb
express_admin-1.7.27 app/components/express_admin/helpers/presenters_helper.rb