duck_record 0.0.16 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/duck_record/associations/builder/{association.rb → embeds_association.rb} +5 -5
- data/lib/duck_record/associations/builder/{collection_association.rb → embeds_many.rb} +6 -2
- data/lib/duck_record/associations/builder/{singular_association.rb → embeds_one.rb} +13 -1
- data/lib/duck_record/associations/{association.rb → embeds_association.rb} +1 -1
- data/lib/duck_record/associations/{collection_association.rb → embeds_many_association.rb} +2 -2
- data/lib/duck_record/associations/{collection_proxy.rb → embeds_many_proxy.rb} +1 -1
- data/lib/duck_record/associations/{singular_association.rb → embeds_one_association.rb} +10 -3
- data/lib/duck_record/associations.rb +11 -892
- data/lib/duck_record/attribute_assignment.rb +16 -17
- data/lib/duck_record/attribute_methods/write.rb +19 -16
- data/lib/duck_record/base.rb +0 -20
- data/lib/duck_record/core.rb +5 -1
- data/lib/duck_record/nested_attributes.rb +1 -1
- data/lib/duck_record/nested_validate_association.rb +1 -1
- data/lib/duck_record/readonly_attributes.rb +13 -0
- data/lib/duck_record/reflection.rb +11 -11
- data/lib/duck_record/serialization.rb +5 -16
- data/lib/duck_record/version.rb +1 -1
- metadata +10 -14
- data/lib/duck_record/associations/builder/has_many.rb +0 -7
- data/lib/duck_record/associations/builder/has_one.rb +0 -15
- data/lib/duck_record/associations/has_many_association.rb +0 -11
- data/lib/duck_record/associations/has_one_association.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9bf46dd431abd8476ad8351490c70739b7ad33a
|
4
|
+
data.tar.gz: 0c2c4c633293dce041e0a1f1d0cf33b8c941197f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ade0bf78d799f87febbaacd5a5616547f61e767a0ea5da8ce22a3c2e37869cd41349888c0ecf13ad8865e7945dfd3cc9f526d8fec7cdaec2469fd87bd540ad58
|
7
|
+
data.tar.gz: a74a31a095e4d0db122c11c2bbf403191d15ca854c9fc8e55ab6382bfb6128ea8365490071a56bec1a3cb54c72179926cb3ca0e8c763bd9998a71a61d3a319ba
|
data/README.md
CHANGED
@@ -21,10 +21,10 @@ class Comment < DuckRecord::Base
|
|
21
21
|
end
|
22
22
|
|
23
23
|
class Book < DuckRecord::Base
|
24
|
-
|
24
|
+
embeds_one :author, class_name: 'Person', validate: true
|
25
25
|
accepts_nested_attributes_for :author
|
26
26
|
|
27
|
-
|
27
|
+
embeds_many :comments, validate: true
|
28
28
|
accepts_nested_attributes_for :comments
|
29
29
|
|
30
30
|
attribute :title, :string
|
@@ -10,7 +10,7 @@
|
|
10
10
|
# - HasManyAssociation
|
11
11
|
|
12
12
|
module DuckRecord::Associations::Builder # :nodoc:
|
13
|
-
class
|
13
|
+
class EmbedsAssociation #:nodoc:
|
14
14
|
class << self
|
15
15
|
attr_accessor :extensions
|
16
16
|
end
|
@@ -46,7 +46,7 @@ module DuckRecord::Associations::Builder # :nodoc:
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def self.valid_options(options)
|
49
|
-
VALID_OPTIONS +
|
49
|
+
VALID_OPTIONS + EmbedsAssociation.extensions.flat_map(&:valid_options)
|
50
50
|
end
|
51
51
|
|
52
52
|
def self.validate_options(options)
|
@@ -57,7 +57,7 @@ module DuckRecord::Associations::Builder # :nodoc:
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def self.define_callbacks(model, reflection)
|
60
|
-
|
60
|
+
EmbedsAssociation.extensions.each do |extension|
|
61
61
|
extension.build model, reflection
|
62
62
|
end
|
63
63
|
end
|
@@ -85,8 +85,8 @@ module DuckRecord::Associations::Builder # :nodoc:
|
|
85
85
|
|
86
86
|
def self.define_writers(mixin, name)
|
87
87
|
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
88
|
-
def #{name}=(value
|
89
|
-
return if
|
88
|
+
def #{name}=(value)
|
89
|
+
return if self.class.readonly_attributes.include?("#{name}")
|
90
90
|
association(:#{name}).writer(value)
|
91
91
|
end
|
92
92
|
CODE
|
@@ -3,11 +3,15 @@
|
|
3
3
|
require "duck_record/associations"
|
4
4
|
|
5
5
|
module DuckRecord::Associations::Builder # :nodoc:
|
6
|
-
class
|
6
|
+
class EmbedsMany < EmbedsAssociation #:nodoc:
|
7
7
|
CALLBACKS = [:before_add, :after_add]
|
8
8
|
|
9
|
+
def self.macro
|
10
|
+
:embeds_many
|
11
|
+
end
|
12
|
+
|
9
13
|
def self.valid_options(options)
|
10
|
-
super +
|
14
|
+
super + CALLBACKS
|
11
15
|
end
|
12
16
|
|
13
17
|
def self.define_callbacks(model, reflection)
|
@@ -1,7 +1,19 @@
|
|
1
1
|
# This class is inherited by the has_one and belongs_to association classes
|
2
2
|
|
3
3
|
module DuckRecord::Associations::Builder # :nodoc:
|
4
|
-
class
|
4
|
+
class EmbedsOne < EmbedsAssociation #:nodoc:
|
5
|
+
def self.macro
|
6
|
+
:embeds_one
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.define_validations(model, reflection)
|
10
|
+
super
|
11
|
+
|
12
|
+
if reflection.options[:required]
|
13
|
+
model.validates_presence_of reflection.name, message: :required
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
5
17
|
def self.define_accessors(model, reflection)
|
6
18
|
super
|
7
19
|
mixin = model.generated_association_methods
|
@@ -15,7 +15,7 @@ module DuckRecord
|
|
15
15
|
# CollectionAssociation
|
16
16
|
# HasManyAssociation + ForeignAssociation
|
17
17
|
# HasManyThroughAssociation + ThroughAssociation
|
18
|
-
class
|
18
|
+
class EmbedsAssociation #:nodoc:
|
19
19
|
attr_reader :owner, :target, :reflection
|
20
20
|
|
21
21
|
delegate :options, to: :reflection
|
@@ -23,10 +23,10 @@ module DuckRecord
|
|
23
23
|
#
|
24
24
|
# If you need to work on all current children, new and existing records,
|
25
25
|
# +load_target+ and the +loaded+ flag are your friends.
|
26
|
-
class
|
26
|
+
class EmbedsManyAssociation < EmbedsAssociation #:nodoc:
|
27
27
|
# Implements the reader method, e.g. foo.items for Foo.has_many :items
|
28
28
|
def reader
|
29
|
-
@_reader ||=
|
29
|
+
@_reader ||= EmbedsManyProxy.new(klass, self)
|
30
30
|
end
|
31
31
|
|
32
32
|
# Implements the writer method, e.g. foo.items= for Foo.has_many :items
|
@@ -27,7 +27,7 @@ module DuckRecord
|
|
27
27
|
#
|
28
28
|
# is computed directly through SQL and does not trigger by itself the
|
29
29
|
# instantiation of the actual post records.
|
30
|
-
class
|
30
|
+
class EmbedsManyProxy
|
31
31
|
include Enumerable
|
32
32
|
|
33
33
|
delegate :to_xml, :encode_with, :length, :collect, :map, :each, :all?, :include?, :to_ary, :join,
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module DuckRecord
|
2
2
|
module Associations
|
3
|
-
class
|
3
|
+
class EmbedsOneAssociation < EmbedsAssociation #:nodoc:
|
4
4
|
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar
|
5
5
|
def reader
|
6
6
|
target
|
@@ -27,8 +27,15 @@ module DuckRecord
|
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def replace(
|
31
|
-
|
30
|
+
def replace(record)
|
31
|
+
self.target =
|
32
|
+
if record.is_a? klass
|
33
|
+
record
|
34
|
+
elsif record.respond_to?(:to_h)
|
35
|
+
build_record(record.to_h)
|
36
|
+
end
|
37
|
+
rescue
|
38
|
+
raise_on_type_mismatch!(record)
|
32
39
|
end
|
33
40
|
|
34
41
|
def set_new_record(record)
|