informal 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +26 -1
- data/informal.gemspec +2 -0
- data/lib/informal/model_no_init.rb +10 -1
- data/lib/informal/version.rb +1 -1
- data/test/model_test_cases.rb +15 -0
- metadata +5 -7
data/README.md
CHANGED
@@ -60,6 +60,31 @@ Make your own `#initialize` method, and in that you can assign the attributes
|
|
60
60
|
using the `#attributes=` method and also call super with whatever args are
|
61
61
|
needed.
|
62
62
|
|
63
|
+
## Overriding the `model_name`
|
64
|
+
|
65
|
+
If you name your model `InformalCommand`, form params get passed to your controller
|
66
|
+
in the `params[:informal_command]` hash. As that's a bit ugly and perhaps doesn't
|
67
|
+
play well with standing in for a real ActiveRecord model, Informal provides a
|
68
|
+
method to override the model name.
|
69
|
+
|
70
|
+
class InformalCommand
|
71
|
+
informal_model_name "Command"
|
72
|
+
# ...
|
73
|
+
end
|
74
|
+
|
75
|
+
Note: the `informal_model_name` feature is available only in Rails 3.1 or greater
|
76
|
+
(unless somebody back-ports the required API change to 3.0.x).
|
77
|
+
|
78
|
+
## Idiosyncrasies
|
79
|
+
|
80
|
+
The standard way that Rails generates ids for new records is to name them like
|
81
|
+
`command_new`, as opposed to `command_17` for persisted records. I've found that
|
82
|
+
when using informal models I often want more than one per page, and it's helpful
|
83
|
+
to have a unique id for JavaScript to use. Therefore Informal uses the model's
|
84
|
+
`object_id` to get a unique id for the record. Those ids in the DOM will look like
|
85
|
+
`command_2157193640`, which would be scary if you did anything with those memory
|
86
|
+
addresses except use them for attaching scripts.
|
87
|
+
|
63
88
|
## License
|
64
89
|
|
65
|
-
Released under the MIT License.
|
90
|
+
Copyright © 2011 Josh Susser. Released under the MIT License. See the LICENSE file.
|
data/informal.gemspec
CHANGED
@@ -20,4 +20,6 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
22
|
s.add_dependency('activemodel', "~> 3.0")
|
23
|
+
# s.add_dependency('activemodel', "~> 3.0.0") # to test w/o 3.1 only features
|
24
|
+
# s.add_dependency('activemodel', "~> 3.1.0.rc4") # to test 3.1 only features, ex: informal_model_name
|
23
25
|
end
|
@@ -5,11 +5,20 @@ module Informal
|
|
5
5
|
klass.class_eval do
|
6
6
|
extend ActiveModel::Naming
|
7
7
|
include ActiveModel::Validations
|
8
|
+
extend ClassMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
if ActiveModel::VERSION::MINOR > 0
|
14
|
+
def informal_model_name(name)
|
15
|
+
@_model_name = ActiveModel::Name.new(self, nil, name)
|
16
|
+
end
|
8
17
|
end
|
9
18
|
end
|
10
19
|
|
11
20
|
def attributes=(attrs)
|
12
|
-
attrs.each_pair { |name, value| self.send("#{name}=", value) }
|
21
|
+
attrs && attrs.each_pair { |name, value| self.send("#{name}=", value) }
|
13
22
|
end
|
14
23
|
|
15
24
|
def persisted?
|
data/lib/informal/version.rb
CHANGED
data/test/model_test_cases.rb
CHANGED
@@ -5,12 +5,20 @@ module ModelTestCases
|
|
5
5
|
@model = self.poro_class.new(:x => 1, :y => 2)
|
6
6
|
end
|
7
7
|
|
8
|
+
def teardown
|
9
|
+
self.poro_class.instance_variable_set(:@_model_name, nil)
|
10
|
+
end
|
11
|
+
|
8
12
|
def test_new
|
9
13
|
assert_equal 1, @model.x
|
10
14
|
assert_equal 2, @model.y
|
11
15
|
assert_nil @model.z
|
12
16
|
end
|
13
17
|
|
18
|
+
def test_new_with_nil
|
19
|
+
assert_nothing_raised { self.poro_class.new(nil) }
|
20
|
+
end
|
21
|
+
|
14
22
|
def test_persisted
|
15
23
|
assert !@model.persisted?
|
16
24
|
end
|
@@ -27,6 +35,13 @@ module ModelTestCases
|
|
27
35
|
assert_equal "Poro", @model.class.model_name.human
|
28
36
|
end
|
29
37
|
|
38
|
+
if ActiveModel::VERSION::MINOR > 0
|
39
|
+
def test_model_name_override
|
40
|
+
self.poro_class.informal_model_name("Alias")
|
41
|
+
assert_equal "Alias", @model.class.model_name.human
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
30
45
|
def test_validations
|
31
46
|
assert @model.invalid?
|
32
47
|
assert_equal [], @model.errors[:x]
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: informal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Susser
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-06-19 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: activemodel
|
@@ -57,7 +56,6 @@ files:
|
|
57
56
|
- test/model_test.rb
|
58
57
|
- test/model_test_cases.rb
|
59
58
|
- test/test_helper.rb
|
60
|
-
has_rdoc: true
|
61
59
|
homepage: https://github.com/joshsusser/informal
|
62
60
|
licenses: []
|
63
61
|
|
@@ -87,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
85
|
requirements: []
|
88
86
|
|
89
87
|
rubyforge_project: informal
|
90
|
-
rubygems_version: 1.
|
88
|
+
rubygems_version: 1.8.5
|
91
89
|
signing_key:
|
92
90
|
specification_version: 3
|
93
91
|
summary: Easily use any Plain Old Ruby Object as the model for Rails form helpers.
|