Skip to content

Commit

Permalink
Feature: model_name overridden in form_object module
Browse files Browse the repository at this point in the history
The model_name class method is part of the ActiveModel::Naming module.

Typically Form Objects want to override the self.model_name method,
as this method is used by form_for to determine the URL that the form is
submitted to.

The FormObject module uses the resource part of the concrete form-object
Class name as the name attribute of the ActiveModel::Name instance
returned from model_name.
  • Loading branch information
mattfreer committed Feb 19, 2014
1 parent ea98193 commit 132f855
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/active_model/form_object.rb
Expand Up @@ -19,6 +19,10 @@ def new(*args, &block)
allocate_and_init(*args, &block)
end

def model_name
ActiveModel::Name.new(self, nil, self.name.split(/(?=[A-Z])/).first)
end

private

def create_accessors(*args)
Expand Down
4 changes: 0 additions & 4 deletions spec/dummy/app/form_objects/user_signup.rb
Expand Up @@ -8,10 +8,6 @@ class UserSignup

validates :name, :length => { :minimum => 2 }

def self.model_name
ActiveModel::Name.new(self, nil, "User")
end

def persist!
@user = User.create!(:name => name)
end
Expand Down

0 comments on commit 132f855

Please sign in to comment.