Skip to content

Commit

Permalink
Add informal_model_name feature
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsusser committed Jun 19, 2011
1 parent ab6a8df commit b40eb1b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Expand Up @@ -60,6 +60,31 @@ Make your own `#initialize` method, and in that you can assign the attributes
using the `#attributes=` method and also call super with whatever args are
needed.

## Overriding the `model_name`

If you name your model `InformalCommand`, form params get passed to your controller
in the `params[:informal_command]` hash. As that's a bit ugly and perhaps doesn't
play well with standing in for a real ActiveRecord model, Informal provides a
method to override the model name.

class InformalCommand
informal_model_name "Command"
# ...
end

Note: the `informal_model_name` feature is available only in Rails 3.1 or greater
(unless somebody back-ports the required API change to 3.0.x).

## Idiosyncrasies

The standard way that Rails generates ids for new records is to name them like
`command_new`, as opposed to `command_17` for persisted records. I've found that
when using informal models I often want more than one per page, and it's helpful
to have a unique id for JavaScript to use. Therefore Informal uses the model's
`object_id` to get a unique id for the record. Those ids in the DOM will look like
`command_2157193640`, which would be scary if you did anything with those memory
addresses except use them for attaching scripts.

## License

Released under the MIT License. See the LICENSE file.
Copyright © 2011 Josh Susser. Released under the MIT License. See the LICENSE file.
2 changes: 2 additions & 0 deletions informal.gemspec
Expand Up @@ -20,4 +20,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency('activemodel', "~> 3.0")
# s.add_dependency('activemodel', "~> 3.0.0") # to test w/o 3.1 only features
# s.add_dependency('activemodel', "~> 3.1.0.rc4") # to test 3.1 only features, ex: informal_model_name
end
9 changes: 9 additions & 0 deletions lib/informal/model_no_init.rb
Expand Up @@ -5,6 +5,15 @@ def self.included(klass)
klass.class_eval do
extend ActiveModel::Naming
include ActiveModel::Validations
extend ClassMethods
end
end

module ClassMethods
if ActiveModel::VERSION::MINOR > 0
def informal_model_name(name)
@_model_name = ActiveModel::Name.new(self, nil, name)
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/model_test_cases.rb
Expand Up @@ -5,6 +5,10 @@ def setup
@model = self.poro_class.new(:x => 1, :y => 2)
end

def teardown
self.poro_class.instance_variable_set(:@_model_name, nil)
end

def test_new
assert_equal 1, @model.x
assert_equal 2, @model.y
Expand All @@ -31,6 +35,13 @@ def test_naming
assert_equal "Poro", @model.class.model_name.human
end

if ActiveModel::VERSION::MINOR > 0
def test_model_name_override
self.poro_class.informal_model_name("Alias")
assert_equal "Alias", @model.class.model_name.human
end
end

def test_validations
assert @model.invalid?
assert_equal [], @model.errors[:x]
Expand Down

0 comments on commit b40eb1b

Please sign in to comment.