Module: TransForms::MainModel::Proxy::ClassMethods

Defined in:
lib/trans_forms/main_model/proxy.rb

Overview

A module that adds functionality to delegate certain methods to the main model. This is done to make forms and controllers handle the form model as if it were the main model itself

Instance Method Summary collapse

Instance Method Details

#model_nameObject

Returns an ActiveModel::Name object for module. It can be used to retrieve all kinds of naming-related information (See ActiveModel::Name for more information).

class PostForm < TransForms::FormBase
  set_main_model :post, proxy: true
end

PostForm.model_name          # => Post
PostForm.model_name.class    # => ActiveModel::Name
PostForm.model_name.singular # => "post"
PostForm.model_name.plural   # => "posts"


24
25
26
27
28
29
30
31
32
33
# File 'lib/trans_forms/main_model/proxy.rb', line 24

def model_name
  @_model_name ||= begin
    klass = respond_to?(:main_model) ? main_model.to_s.classify.constantize : self

    namespace = klass.parents.detect do |n|
      n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
    end
    ActiveModel::Name.new(klass, namespace)
  end
end