Sha256: 2f19dffddfae57d1046a2d2edc3fdd0f82a8b7ee82b3dbaecbbce40595925037

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module SimpleAdmin
  module DisplayHelper

    # Return a pretty string for any object
    # Date Time are formatted via #localize with :format => :long
    # ActiveRecord objects are formatted via #auto_link
    # We attempt to #display_name of any other objects
    def pretty_format(object)
      case object
      when String
        object
      when Date, Time
        localize(object, :format => :long)
      else
        (object.respond_to?(:display_name) && object.send(:display_name)) ||
        (object.respond_to?(:full_name) && object.send(:full_name)) ||
        (object.respond_to?(:name) && object.send(:name)) ||
        (object.respond_to?(:username) && object.send(:username)) ||
        (object.respond_to?(:login) && object.send(:login)) ||
        (object.respond_to?(:title) && object.send(:title)) ||
        (object.respond_to?(:email) && object.send(:email)) ||
        (object.respond_to?(:to_s) && object.send(:to_s)) ||
        "#{object}"
      end
    end

    def data_for(col)
      value = if col[:data]
        instance_exec(@resource, &col[:data])
      elsif col[:attribute].to_s =~ /^([\w]+)_id$/ && @resource.respond_to?($1.to_sym)
        pretty_format(@resource.send($1))
      else
        pretty_format(@resource.send(col[:attribute]))
      end
      value ||= content_tag(:span, 'Empty', :class => 'empty')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_admin-0.7.1 app/helpers/simple_admin/display_helper.rb