Sha256: d03e52e3ea52c5347ee128e171e7425effa1c88e7f725e090263deeb4b155e29

Contents?: true

Size: 1.59 KB

Versions: 46

Compression:

Stored size: 1.59 KB

Contents

module ActiveAdmin
  class Resource

    module Naming
      def resource_name
        @resource_name ||= begin
          as = @options[:as].gsub /\s/, '' if @options[:as]

          if as || !resource_class.respond_to?(:model_name)
            Name.new resource_class, as
          else
            Name.new resource_class
          end
        end
      end

      # Returns the name to call this resource such as "Bank Account"
      def resource_label
        resource_name.translate count: 1,
          default: resource_name.to_s.gsub('::', ' ').titleize
      end

      # Returns the plural version of this resource such as "Bank Accounts"
      def plural_resource_label(options = {})
        defaults = {count:   Helpers::I18n::PLURAL_MANY_COUNT,
                    default: resource_label.pluralize.titleize}
        resource_name.translate defaults.merge options
      end

      # Forms use the model's original `param_key`, so we can't use our
      # custom `resource_name` when the model's been renamed in ActiveAdmin.
      def param_key
        if resource_class.respond_to? :model_name
          resource_class.model_name.param_key
        else
          resource_name.param_key
        end
      end
    end

    class Name < ActiveModel::Name
      delegate :hash, to: :to_str

      def initialize(klass, name = nil)
        super(klass, nil, name)
      end

      def translate(options = {})
        I18n.t i18n_key, {scope: [:activerecord, :models]}.merge(options)
      end

      def route_key
        plural
      end

      def eql?(other)
        to_str.eql?(other.to_str)
      end
    end

  end
end

Version data entries

46 entries across 46 versions & 5 rubygems

Version Path
activeadmin-rb-1.5.1 lib/active_admin/resource/naming.rb
activeadmin-rb-1.5.0 lib/active_admin/resource/naming.rb
activeadmin_addons-1.7.1 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin-rb-1.4.0 lib/active_admin/resource/naming.rb
activeadmin_addons-1.7.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin-1.4.3 lib/active_admin/resource/naming.rb
activeadmin-1.4.2 lib/active_admin/resource/naming.rb
activeadmin-1.4.1 lib/active_admin/resource/naming.rb
activeadmin-1.4.0 lib/active_admin/resource/naming.rb
activeadmin-1.3.1 lib/active_admin/resource/naming.rb
activeadmin_addons-1.6.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin_addons-1.5.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin-1.3.0 lib/active_admin/resource/naming.rb
activeadmin_addons-1.4.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin_addons-1.3.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin_addons-1.2.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource/naming.rb
activeadmin-1.2.1 lib/active_admin/resource/naming.rb
activeadmin_addons-1.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/resource/naming.rb
activeadmin-1.2.0 lib/active_admin/resource/naming.rb
activeadmin_addons-1.1.1 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/resource/naming.rb