embeds_many 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/README.markdown +21 -4
- data/lib/embeds_many/base.rb +3 -7
- data/lib/embeds_many/child.rb +2 -2
- data/lib/embeds_many/child_collection.rb +2 -2
- data/lib/embeds_many/version.rb +1 -1
- data/spec/embeds_many/user_spec.rb +12 -0
- data/spec/schema.rb +3 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba71f51a1cc397e9552e7df72bd48f850f6b94f1
|
4
|
+
data.tar.gz: d2a3a9e1d58f919d9151203c1a742f2cb30046be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f28e15a662b00e961dd31a3fc2cd3dafc0e02d4542c7f70244d6338dac273122e518de8a15098f1e63ba2f1230fc15c6d0a3baa91f5df8434310c2c3c04babb
|
7
|
+
data.tar.gz: d348e086153200946b6e644ca9c047c1f2784f7770ecf9a0361b3ce68061638ef4416351b09ed145b07dd66d7e3f9ef0d7712b7ceeb0b054ffaff9711d3cba1b
|
data/README.markdown
CHANGED
@@ -7,6 +7,12 @@ EmbedsMany allows programmers to work with embedded records the same way as acti
|
|
7
7
|
|
8
8
|
**NOTE**: EmbedsMany only works with Rails/ActiveRecord `4.0.4` or above. To use EmbedsMany, you must use PostgreSQL.
|
9
9
|
|
10
|
+
## Limitations and assumptions
|
11
|
+
|
12
|
+
- Embedded keys and values can only be simply text strings due to the restriction of [hstore](http://www.postgresql.org/docs/9.2/static/hstore.html).
|
13
|
+
- Embedded records should be of limited number, or they may cause performance problems.
|
14
|
+
- The `id` of embedded records may duplicate when race condition happens.
|
15
|
+
|
10
16
|
## Usage
|
11
17
|
|
12
18
|
### Installation
|
@@ -40,13 +46,22 @@ add_column :users, :tags, :hstore, array: true, default: []
|
|
40
46
|
|
41
47
|
### Setup Model
|
42
48
|
|
49
|
+
A simple usage is as follows:
|
50
|
+
|
43
51
|
```ruby
|
44
52
|
class User < ActiveRecord::Base
|
45
|
-
#
|
53
|
+
# embedded tags
|
46
54
|
embeds_many :tags
|
55
|
+
end
|
56
|
+
```
|
47
57
|
|
48
|
-
|
49
|
-
|
58
|
+
If you need validations on embedded records, you can provide an optional block:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class User < ActiveRecord::Base
|
62
|
+
# embedded tags
|
63
|
+
embeds_many :tags do
|
64
|
+
# add accessors
|
50
65
|
embedded_fields :name, :color
|
51
66
|
|
52
67
|
validates :name, uniqueness: true, presence: true
|
@@ -63,7 +78,9 @@ class User < ActiveRecord::Base
|
|
63
78
|
end
|
64
79
|
```
|
65
80
|
|
66
|
-
|
81
|
+
**Note**: There's no need to define a class for the embedded records, it's taken over automatically by `embeds_many`.
|
82
|
+
|
83
|
+
### Work with embedded records
|
67
84
|
|
68
85
|
```ruby
|
69
86
|
# create new tag
|
data/lib/embeds_many/base.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module EmbedsMany
|
2
2
|
module Base
|
3
|
-
def embeds_many(field)
|
4
|
-
child_klass =
|
3
|
+
def embeds_many(field, &block)
|
4
|
+
child_klass = Class.new(EmbedsMany::Child)
|
5
5
|
child_klass.instance_variable_set "@field_name", field
|
6
6
|
|
7
7
|
# rewrite association
|
@@ -9,12 +9,8 @@ module EmbedsMany
|
|
9
9
|
instance_variable_get("@#{field}_collection") ||
|
10
10
|
instance_variable_set("@#{field}_collection", ChildrenCollection.new(self, field, child_klass))
|
11
11
|
end
|
12
|
-
end
|
13
12
|
|
14
|
-
|
15
|
-
def embedded(field, &block)
|
16
|
-
child_klass = instance_variable_get "@#{field}_klass"
|
17
|
-
child_klass.class_eval(&block)
|
13
|
+
child_klass.class_eval(&block) if block
|
18
14
|
end
|
19
15
|
end
|
20
16
|
end
|
data/lib/embeds_many/child.rb
CHANGED
@@ -30,10 +30,10 @@ module EmbedsMany
|
|
30
30
|
|
31
31
|
# validation requires model name
|
32
32
|
def self.model_name
|
33
|
-
ActiveModel::Name.new(self, nil, @field_name.to_s.classify)
|
33
|
+
ActiveModel::Name.new(self, nil, self.name || @field_name.to_s.classify)
|
34
34
|
end
|
35
35
|
|
36
|
-
def initialize(attrs)
|
36
|
+
def initialize(attrs={})
|
37
37
|
@attributes = ActiveSupport::HashWithIndifferentAccess.new
|
38
38
|
|
39
39
|
attrs.each do |name, value|
|
data/lib/embeds_many/version.rb
CHANGED
@@ -18,6 +18,10 @@ describe User do
|
|
18
18
|
user.reload.tags.any? {|t| t.name == 'bug'}.should be_true
|
19
19
|
end
|
20
20
|
|
21
|
+
it "should to initialize an tag with no parameters" do
|
22
|
+
user.tags.new.should_not be_nil
|
23
|
+
end
|
24
|
+
|
21
25
|
it "should be unable to create tags with duplicate name" do
|
22
26
|
user.tags.new(name: 'bug', color: 'red').save.should be_true
|
23
27
|
|
@@ -27,6 +31,13 @@ describe User do
|
|
27
31
|
tag.errors[:name].should_not be_empty
|
28
32
|
end
|
29
33
|
|
34
|
+
it "should be unable to create tags without name" do
|
35
|
+
tag = user.tags.new(color: 'red')
|
36
|
+
|
37
|
+
tag.save.should be_false
|
38
|
+
tag.errors[:name].should_not be_empty
|
39
|
+
end
|
40
|
+
|
30
41
|
it "should be unable to create tags without color" do
|
31
42
|
tag = user.tags.new(name: 'bug')
|
32
43
|
|
@@ -35,6 +46,7 @@ describe User do
|
|
35
46
|
end
|
36
47
|
end
|
37
48
|
|
49
|
+
|
38
50
|
it "should be able to update record" do
|
39
51
|
tag = user.tags.create(name: 'bug', color: 'red')
|
40
52
|
|
data/spec/schema.rb
CHANGED
@@ -10,10 +10,9 @@ end
|
|
10
10
|
|
11
11
|
class User < ActiveRecord::Base
|
12
12
|
# many embedded tags
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
embedded :tags do
|
13
|
+
# the block is optional
|
14
|
+
embeds_many :tags do
|
15
|
+
# add accessors
|
17
16
|
embedded_fields :name, :color
|
18
17
|
|
19
18
|
validates :name, uniqueness: true, presence: true
|