Sha256: 2a0cbccbe71acad672188ca86e8032caafb0e2c2ac42dca57d53ab956130ce74

Contents?: true

Size: 985 Bytes

Versions: 28

Compression:

Stored size: 985 Bytes

Contents

module Lolita
  class ModelName
    def initialize(given_class)
      @klass = given_class
      @class_name = @klass.to_s
    end

    def human(options={})
      name = if defined?(ActiveRecord::Base) && @klass.ancestors.include?(ActiveRecord::Base)
        "activerecord.models.#{@class_name.underscore}"
      else
        "lolita.models.#{@class_name.underscore.gsub("/",".")}"
      end
      ::I18n.t("#{name}.#{prefix(options)}", default: default_human_name(options))
    end

    private

    def default_human_name options = {}
      if prefix(options) == 'one'
        @class_name.humanize
      else
        ActiveModel::Naming.plural(@klass).humanize
      end
    end

    def prefix options
      if options[:count]
        if options[:count] == 1
          "one"
        else
          "other"
        end
      else
        "one"
      end
    end
  end
end

Object.class_eval do
  def lolita_model_name
    @lolita_model_name ||= Lolita::ModelName.new(self)
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
lolita-4.0.11 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.2.0 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.10 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.9 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.8 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.7 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.6 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.5 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.4 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.3 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.2 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.1 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-4.0.0 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.4.3 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.4.2 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.4.1 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.4.0 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.3.9 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.3.8 lib/lolita/ruby_ext/lolita_model_name.rb
lolita-3.3.7 lib/lolita/ruby_ext/lolita_model_name.rb