embeds_many 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df5b6c590bea371b92b51fcc0d997179a9a31f79
4
- data.tar.gz: 822d11065876bd3721db8ff8d5ea42161eab83da
3
+ metadata.gz: ba71f51a1cc397e9552e7df72bd48f850f6b94f1
4
+ data.tar.gz: d2a3a9e1d58f919d9151203c1a742f2cb30046be
5
5
  SHA512:
6
- metadata.gz: edb630f9cec39da2062f0fe599074357d0427a071f8e489cf0a07ecadad47d52cf84f4f35d606f5a365a62f5f27efbb502dc833d1e3fee017169b76c8204529d
7
- data.tar.gz: f026f9f1796fa051ddd02f2d42afec6f8b37a9e47568b7de09fab2910b0b51f230e744c92d78602b3701fd09c9ee0f3faa99ccc2ae0bf995c18be9167c15aa02
6
+ metadata.gz: 6f28e15a662b00e961dd31a3fc2cd3dafc0e02d4542c7f70244d6338dac273122e518de8a15098f1e63ba2f1230fc15c6d0a3baa91f5df8434310c2c3c04babb
7
+ data.tar.gz: d348e086153200946b6e644ca9c047c1f2784f7770ecf9a0361b3ce68061638ef4416351b09ed145b07dd66d7e3f9ef0d7712b7ceeb0b054ffaff9711d3cba1b
@@ -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
- # many embedded tags
53
+ # embedded tags
46
54
  embeds_many :tags
55
+ end
56
+ ```
47
57
 
48
- # validation tags
49
- embedded :tags do
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
- ### Work with embedded tags
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
@@ -1,7 +1,7 @@
1
1
  module EmbedsMany
2
2
  module Base
3
- def embeds_many(field)
4
- child_klass = instance_variable_set "@#{field}_klass", EmbedsMany::Child.clone
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
- # define validations and helper instance methods
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
@@ -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|
@@ -1,10 +1,10 @@
1
1
  module EmbedsMany
2
2
  class ChildrenCollection
3
- def new(attrs)
3
+ def new(attrs={})
4
4
  @child_klass.new(attrs.merge(parent: @obj))
5
5
  end
6
6
 
7
- def create(attrs)
7
+ def create(attrs={})
8
8
  record = @child_klass.new(attrs.merge(parent: @obj))
9
9
 
10
10
  record.save
@@ -1,3 +1,3 @@
1
1
  module EmbedsMany
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
 
@@ -10,10 +10,9 @@ end
10
10
 
11
11
  class User < ActiveRecord::Base
12
12
  # many embedded tags
13
- embeds_many :tags
14
-
15
- # validation tags
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embeds_many
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - liufengyun