duck_record 0.0.16 → 0.0.18

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50cfc614adaa1a3af48769d41305d88b4ace7149
4
- data.tar.gz: ba8306b2ac4d88f8e7e69600e6398f6312132746
3
+ metadata.gz: e9bf46dd431abd8476ad8351490c70739b7ad33a
4
+ data.tar.gz: 0c2c4c633293dce041e0a1f1d0cf33b8c941197f
5
5
  SHA512:
6
- metadata.gz: d8249209d1c8315953b4b5404b4f591bc9138a24d04fc4af0c9c8c7d75b277a827721b5a2431695baea747b3a0fffe1be460e814b979180853ab3c4f7255769d
7
- data.tar.gz: f7a092d53c15c85a5938d6558f66d93c4b9f01d1db23e561b76fedfa764d2bfd054270fd622d987c6a740b0994ffa22063ab336c490bf943abef5e554d8acc17
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
- has_one :author, class_name: 'Person', validate: true
24
+ embeds_one :author, class_name: 'Person', validate: true
25
25
  accepts_nested_attributes_for :author
26
26
 
27
- has_many :comments, validate: true
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 Association #:nodoc:
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 + Association.extensions.flat_map(&: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
- Association.extensions.each do |extension|
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, force_write_readonly: false)
89
- return if !force_write_readonly && self.class.readonly_attributes.include?("#{name}")
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 CollectionAssociation < Association #:nodoc:
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 + [:before_add, :after_add]
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 SingularAssociation < Association #:nodoc:
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 Association #:nodoc:
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 CollectionAssociation < Association #:nodoc:
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 ||= CollectionProxy.new(klass, self)
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 CollectionProxy
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 SingularAssociation < Association #:nodoc:
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(_record)
31
- raise NotImplementedError, "Subclasses must implement a replace(record) method"
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)