Permalink
Browse files

Add informal_model_name feature

  • Loading branch information...
1 parent ab6a8df commit b40eb1bff4cc7123dc2130e644685ec66f0cf0e7 @joshsusser joshsusser committed Jun 19, 2011
Showing with 48 additions and 1 deletion.
  1. +26 −1 README.md
  2. +2 −0 informal.gemspec
  3. +9 −0 lib/informal/model_no_init.rb
  4. +11 −0 test/model_test_cases.rb
View
@@ -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.
View
@@ -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
@@ -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
View
@@ -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
@@ -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]

0 comments on commit b40eb1b

Please sign in to comment.