Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 63 lines (54 sloc) 2.265 kb
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
1 require 'active_support/inflector'
2
1c4d28b @josh Move model naming into ActiveModel
josh authored
3 module ActiveModel
4 class Name < String
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
5 attr_reader :singular, :plural, :element, :collection, :partial_path
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
6 alias_method :cache_key, :collection
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
7
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
8 def initialize(klass)
9 super(klass.name)
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
10 @klass = klass
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
11 @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
12 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
13 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
14 @human = ActiveSupport::Inflector.humanize(@element).freeze
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
15 @collection = ActiveSupport::Inflector.tableize(self).freeze
16 @partial_path = "#{@collection}/#{@element}".freeze
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
17 end
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
18
19 # Transform the model name into a more humane format, using I18n. By default,
20 # it will underscore then humanize the class name (BlogPost.model_name.human #=> "Blog post").
21 # Specify +options+ with additional translating options.
22 def human(options={})
23 return @human unless @klass.respond_to?(:lookup_ancestors) &&
24 @klass.respond_to?(:i18n_scope)
25
26 defaults = @klass.lookup_ancestors.map do |klass|
27 klass.model_name.underscore.to_sym
28 end
29
30 defaults << options.delete(:default) if options[:default]
31 defaults << @human
32
33 options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
34 I18n.translate(defaults.shift, options)
35 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
36 end
de12b5e @rizwanreza Revised naming.rb and lint.rb
rizwanreza authored
37
38 # == Active Model Naming
39 #
40 # Creates a +model_name+ method on your object.
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
41 #
42 # To implement, just extend ActiveModel::Naming in your object:
43 #
44 # class BookCover
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
45 # extend ActiveModel::Naming
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
46 # end
47 #
48 # BookCover.model_name #=> "BookCover"
49 # BookCover.model_name.human #=> "Book cover"
50 #
51 # Providing the functionality that ActiveModel::Naming provides in your object
52 # is required to pass the ActiveModel Lint test. So either extending the provided
53 # method below, or rolling your own is required..
1c4d28b @josh Move model naming into ActiveModel
josh authored
54 module Naming
55 # Returns an ActiveModel::Name object for module. It can be
56 # used to retrieve all kinds of naming-related information.
57 def model_name
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
58 @_model_name ||= ActiveModel::Name.new(self)
1c4d28b @josh Move model naming into ActiveModel
josh authored
59 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
60 end
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
61
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
62 end
Something went wrong with that request. Please try again.