granite-form 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +26 -48
  3. data/.rubocop_todo.yml +304 -27
  4. data/CHANGELOG.md +7 -2
  5. data/granite-form.gemspec +2 -1
  6. data/lib/granite/form/active_record/associations.rb +4 -3
  7. data/lib/granite/form/config.rb +1 -1
  8. data/lib/granite/form/errors.rb +34 -32
  9. data/lib/granite/form/extensions.rb +2 -1
  10. data/lib/granite/form/model/associations/base.rb +6 -2
  11. data/lib/granite/form/model/associations/collection/embedded.rb +1 -1
  12. data/lib/granite/form/model/associations/collection/proxy.rb +3 -3
  13. data/lib/granite/form/model/associations/embeds_any.rb +1 -1
  14. data/lib/granite/form/model/associations/embeds_many.rb +15 -11
  15. data/lib/granite/form/model/associations/embeds_one.rb +9 -8
  16. data/lib/granite/form/model/associations/nested_attributes.rb +60 -32
  17. data/lib/granite/form/model/associations/persistence_adapters/active_record/referenced_proxy.rb +2 -1
  18. data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +7 -6
  19. data/lib/granite/form/model/associations/persistence_adapters/base.rb +8 -4
  20. data/lib/granite/form/model/associations/references_any.rb +1 -1
  21. data/lib/granite/form/model/associations/references_many.rb +3 -2
  22. data/lib/granite/form/model/associations/references_one.rb +1 -1
  23. data/lib/granite/form/model/associations/reflections/base.rb +3 -2
  24. data/lib/granite/form/model/associations/reflections/embeds_any.rb +4 -4
  25. data/lib/granite/form/model/associations/reflections/embeds_many.rb +4 -1
  26. data/lib/granite/form/model/associations/reflections/embeds_one.rb +4 -1
  27. data/lib/granite/form/model/associations/reflections/references_any.rb +6 -6
  28. data/lib/granite/form/model/associations/reflections/references_many.rb +1 -1
  29. data/lib/granite/form/model/associations/reflections/references_one.rb +1 -1
  30. data/lib/granite/form/model/associations/validations.rb +6 -6
  31. data/lib/granite/form/model/associations.rb +6 -4
  32. data/lib/granite/form/model/attributes/attribute.rb +1 -0
  33. data/lib/granite/form/model/attributes/base.rb +9 -7
  34. data/lib/granite/form/model/attributes/reference_one.rb +1 -1
  35. data/lib/granite/form/model/attributes/reflections/base/build_type_definition.rb +2 -1
  36. data/lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb +2 -2
  37. data/lib/granite/form/model/attributes/represents.rb +1 -1
  38. data/lib/granite/form/model/attributes.rb +21 -13
  39. data/lib/granite/form/model/conventions.rb +1 -1
  40. data/lib/granite/form/model/persistence.rb +1 -1
  41. data/lib/granite/form/model/primary.rb +1 -1
  42. data/lib/granite/form/model/representation.rb +4 -4
  43. data/lib/granite/form/model/scopes.rb +5 -5
  44. data/lib/granite/form/model/validations.rb +4 -3
  45. data/lib/granite/form/types/active_support/time_zone.rb +1 -1
  46. data/lib/granite/form/types/array.rb +1 -1
  47. data/lib/granite/form/types/big_decimal.rb +1 -1
  48. data/lib/granite/form/types/boolean.rb +1 -1
  49. data/lib/granite/form/types/date.rb +1 -1
  50. data/lib/granite/form/types/date_time.rb +1 -1
  51. data/lib/granite/form/types/dictionary.rb +1 -1
  52. data/lib/granite/form/types/float.rb +1 -1
  53. data/lib/granite/form/types/has_subtype.rb +1 -0
  54. data/lib/granite/form/types/hash_with_action_controller_parameters.rb +2 -2
  55. data/lib/granite/form/types/integer.rb +1 -1
  56. data/lib/granite/form/types/object.rb +2 -1
  57. data/lib/granite/form/types/string.rb +1 -1
  58. data/lib/granite/form/types/time.rb +1 -1
  59. data/lib/granite/form/types/uuid.rb +1 -1
  60. data/lib/granite/form/util.rb +1 -1
  61. data/lib/granite/form/version.rb +1 -1
  62. data/spec/granite/form/active_record/associations_spec.rb +35 -13
  63. data/spec/granite/form/config_spec.rb +8 -4
  64. data/spec/granite/form/model/associations/embeds_many_spec.rb +99 -51
  65. data/spec/granite/form/model/associations/embeds_one_spec.rb +48 -25
  66. data/spec/granite/form/model/associations/persistence_adapters/active_record_spec.rb +12 -7
  67. data/spec/granite/form/model/associations/references_many_spec.rb +51 -10
  68. data/spec/granite/form/model/associations/references_one_spec.rb +17 -6
  69. data/spec/granite/form/model/associations/reflections/embeds_many_spec.rb +51 -16
  70. data/spec/granite/form/model/associations/reflections/embeds_one_spec.rb +19 -9
  71. data/spec/granite/form/model/associations/reflections/references_many_spec.rb +67 -15
  72. data/spec/granite/form/model/associations/reflections/references_one_spec.rb +34 -11
  73. data/spec/granite/form/model/associations/validations_spec.rb +16 -5
  74. data/spec/granite/form/model/associations_spec.rb +28 -9
  75. data/spec/granite/form/model/attributes/attribute_spec.rb +33 -11
  76. data/spec/granite/form/model/attributes/base_spec.rb +9 -3
  77. data/spec/granite/form/model/attributes/reflections/attribute_spec.rb +1 -0
  78. data/spec/granite/form/model/attributes/reflections/base_spec.rb +1 -0
  79. data/spec/granite/form/model/attributes/reflections/represents/build_type_definition_spec.rb +3 -1
  80. data/spec/granite/form/model/attributes/reflections/represents_spec.rb +2 -2
  81. data/spec/granite/form/model/attributes/represents_spec.rb +2 -2
  82. data/spec/granite/form/model/attributes_spec.rb +76 -36
  83. data/spec/granite/form/model/dirty_spec.rb +3 -0
  84. data/spec/granite/form/model/persistence_spec.rb +15 -5
  85. data/spec/granite/form/model/primary_spec.rb +17 -2
  86. data/spec/granite/form/model/representation_spec.rb +13 -3
  87. data/spec/granite/form/model/scopes_spec.rb +8 -3
  88. data/spec/granite/form/model/validations/associated_spec.rb +20 -6
  89. data/spec/granite/form/model/validations/nested_spec.rb +30 -14
  90. data/spec/granite/form/model/validations_spec.rb +1 -1
  91. data/spec/granite/form/model_spec.rb +1 -0
  92. data/spec/granite/form/types/collection_spec.rb +2 -1
  93. data/spec/granite/form/types/date_spec.rb +1 -1
  94. data/spec/granite/form/types/date_time_spec.rb +0 -2
  95. data/spec/granite/form/types/dictionary_spec.rb +1 -0
  96. data/spec/granite/form/types/has_subtype_spec.rb +6 -1
  97. data/spec/granite/form/types/hash_with_action_controller_parameters_spec.rb +1 -1
  98. data/spec/granite/form/types/object_spec.rb +2 -0
  99. data/spec/granite/form/types/time_spec.rb +0 -2
  100. data/spec/granite/form/util_spec.rb +6 -3
  101. data/spec/support/active_record.rb +13 -0
  102. data/spec/support/shared/nested_attribute_examples.rb +110 -54
  103. metadata +23 -9
@@ -4,6 +4,7 @@ module Granite
4
4
  module Attributes
5
5
  class Base
6
6
  attr_reader :type_definition
7
+
7
8
  delegate :type, :reflection, :owner, :enum, to: :type_definition
8
9
  delegate :name, :readonly, to: :reflection
9
10
 
@@ -20,6 +21,7 @@ module Granite
20
21
 
21
22
  def write(value)
22
23
  return if readonly?
24
+
23
25
  write_value value
24
26
  end
25
27
 
@@ -57,12 +59,12 @@ module Granite
57
59
 
58
60
  def inspect_attribute
59
61
  value = case read
60
- when Date, Time, DateTime
61
- %("#{read.to_formatted_s(:db)}")
62
- else
63
- inspection = read.inspect
64
- inspection.size > 100 ? inspection.truncate(50) : inspection
65
- end
62
+ when Date, Time, DateTime
63
+ %("#{read.to_formatted_s(:db)}")
64
+ else
65
+ inspection = read.inspect
66
+ inspection.size > 100 ? inspection.truncate(50) : inspection
67
+ end
66
68
  "#{name}: #{value}"
67
69
  end
68
70
 
@@ -89,7 +91,7 @@ module Granite
89
91
  end
90
92
  end
91
93
 
92
- private
94
+ private
93
95
 
94
96
  def remove_variable(*names)
95
97
  names.flatten.each do |name|
@@ -32,7 +32,7 @@ module Granite
32
32
  @value_cache
33
33
  end
34
34
 
35
- private
35
+ private
36
36
 
37
37
  def association
38
38
  @association ||= owner.association(reflection.association)
@@ -6,6 +6,7 @@ module Granite
6
6
  class Base
7
7
  class BuildTypeDefinition
8
8
  attr_reader :owner, :reflection
9
+
9
10
  delegate :name, to: :reflection
10
11
 
11
12
  def initialize(owner, reflection)
@@ -19,7 +20,7 @@ module Granite
19
20
  type_definition_for(type)
20
21
  end
21
22
 
22
- private
23
+ private
23
24
 
24
25
  def type
25
26
  reflection.options[:type]
@@ -19,7 +19,7 @@ module Granite
19
19
  end
20
20
  end
21
21
 
22
- private
22
+ private
23
23
 
24
24
  def reference
25
25
  owner.__send__(reflection.reference)
@@ -61,7 +61,7 @@ module Granite
61
61
 
62
62
  def convert_active_model_type_to_definition(attribute_type)
63
63
  type = attribute_type.try(:value_class) ||
64
- Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
64
+ Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
65
65
  type_definition_for(type) if type
66
66
  end
67
67
  end
@@ -24,7 +24,7 @@ module Granite
24
24
  end
25
25
  end
26
26
 
27
- private
27
+ private
28
28
 
29
29
  def reference
30
30
  owner.__send__(reflection.reference)
@@ -25,7 +25,8 @@ module Granite
25
25
 
26
26
  %w[attribute collection dictionary].each do |kind|
27
27
  define_singleton_method kind do |*args, &block|
28
- add_attribute("Granite::Form::Model::Attributes::Reflections::#{kind.camelize}".constantize, *args, &block)
28
+ add_attribute("Granite::Form::Model::Attributes::Reflections::#{kind.camelize}".constantize, *args,
29
+ &block)
29
30
  end
30
31
  end
31
32
  end
@@ -34,7 +35,7 @@ module Granite
34
35
  def add_attribute(reflection_class, *args, &block)
35
36
  reflection = reflection_class.build(self, generated_attributes_methods, *args, &block)
36
37
  self._attributes = _attributes.merge(reflection.name => reflection)
37
- should_define_dirty = (dirty? && reflection_class != Granite::Form::Model::Attributes::Reflections::Base)
38
+ should_define_dirty = dirty? && reflection_class != Granite::Form::Model::Attributes::Reflections::Base
38
39
  define_dirty(reflection.name, generated_attributes_methods) if should_define_dirty
39
40
  reflection
40
41
  end
@@ -42,7 +43,12 @@ module Granite
42
43
  def alias_attribute(alias_name, attribute_name)
43
44
  reflection = reflect_on_attribute(attribute_name)
44
45
  raise ArgumentError, "Unable to alias undefined attribute `#{attribute_name}` on #{self}" unless reflection
45
- raise ArgumentError, "Unable to alias base attribute `#{attribute_name}`" if reflection.class == Granite::Form::Model::Attributes::Reflections::Base
46
+
47
+ if reflection.class == Granite::Form::Model::Attributes::Reflections::Base
48
+ raise ArgumentError,
49
+ "Unable to alias base attribute `#{attribute_name}`"
50
+ end
51
+
46
52
  reflection.class.generate_methods alias_name, generated_attributes_methods
47
53
  self._attribute_aliases = _attribute_aliases.merge(alias_name.to_s => reflection.name)
48
54
  define_dirty alias_name, generated_attributes_methods if dirty?
@@ -85,7 +91,7 @@ module Granite
85
91
  self._sanitize = previous_sanitize
86
92
  end
87
93
 
88
- private
94
+ private
89
95
 
90
96
  def original_inspect
91
97
  Object.method(:inspect).unbind.bind(self).call
@@ -101,7 +107,7 @@ module Granite
101
107
  def generated_attributes_methods
102
108
  @generated_attributes_methods ||=
103
109
  const_set(:GeneratedAttributesMethods, Module.new)
104
- .tap { |proxy| include proxy }
110
+ .tap { |proxy| include proxy }
105
111
  end
106
112
 
107
113
  def inverted_attribute_aliases
@@ -117,14 +123,15 @@ module Granite
117
123
  end
118
124
 
119
125
  def ==(other)
120
- super || other.instance_of?(self.class) && other.attributes(false) == attributes(false)
126
+ super || (other.instance_of?(self.class) && other.attributes(false) == attributes(false))
121
127
  end
122
128
 
123
- alias_method :eql?, :==
129
+ alias eql? ==
124
130
 
125
131
  def attribute(name)
126
132
  reflection = self.class.reflect_on_attribute(name)
127
133
  return unless reflection
134
+
128
135
  initial_value = @initial_attributes.to_h.fetch(reflection.name, Granite::Form::UNDEFINED)
129
136
  @_attributes ||= {}
130
137
  @_attributes[reflection.name] ||= reflection.build_attribute(self, initial_value)
@@ -134,13 +141,13 @@ module Granite
134
141
  attribute(name).write(value)
135
142
  end
136
143
 
137
- alias_method :[]=, :write_attribute
144
+ alias []= write_attribute
138
145
 
139
146
  def read_attribute(name)
140
147
  attribute(name).read
141
148
  end
142
149
 
143
- alias_method :[], :read_attribute
150
+ alias [] read_attribute
144
151
 
145
152
  def read_attribute_before_type_cast(name)
146
153
  attribute(name).read_before_type_cast
@@ -162,7 +169,7 @@ module Granite
162
169
  assign_attributes(attrs)
163
170
  end
164
171
 
165
- alias_method :update_attributes, :update
172
+ alias update_attributes update
166
173
 
167
174
  def assign_attributes(attrs)
168
175
  attrs.each do |name, value|
@@ -172,12 +179,13 @@ module Granite
172
179
  if respond_to?("#{name}=") && !sanitize_value
173
180
  public_send("#{name}=", value)
174
181
  else
175
- logger.info("Ignoring #{sanitize_value ? 'primary' : 'undefined'} `#{name}` attribute value for #{self} during mass-assignment")
182
+ attribute_type = sanitize_value ? 'primary' : 'undefined'
183
+ logger.debug("Ignoring #{attribute_type} `#{name}` attribute value for #{self} during mass-assignment")
176
184
  end
177
185
  end
178
186
  end
179
187
 
180
- alias_method :attributes=, :assign_attributes
188
+ alias attributes= assign_attributes
181
189
 
182
190
  def sync_attributes
183
191
  attribute_names.each do |name|
@@ -198,7 +206,7 @@ module Granite
198
206
  super
199
207
  end
200
208
 
201
- private
209
+ private
202
210
 
203
211
  def attributes_for_inspect
204
212
  attribute_names(false).map do |name|
@@ -19,7 +19,7 @@ module Granite
19
19
  !persisted?
20
20
  end
21
21
 
22
- alias_method :new_object?, :new_record?
22
+ alias new_object? new_record?
23
23
 
24
24
  module ClassMethods
25
25
  def i18n_scope
@@ -30,7 +30,7 @@ module Granite
30
30
  false
31
31
  end
32
32
 
33
- private
33
+ private
34
34
 
35
35
  def mark_persisted!
36
36
  @persisted = true
@@ -29,7 +29,7 @@ module Granite
29
29
  alias_attribute :primary_attribute, _primary_name
30
30
  end
31
31
 
32
- alias_method :primary_attribute, :primary
32
+ alias primary_attribute primary
33
33
 
34
34
  def has_primary_attribute? # rubocop:disable Naming/PredicateName
35
35
  has_attribute? _primary_name
@@ -19,7 +19,7 @@ module Granite
19
19
  attrs = attrs.to_unsafe_hash if attrs.respond_to?(:to_unsafe_hash)
20
20
  attrs = attrs.stringify_keys
21
21
  represented_attrs = self.class.represented_names_and_aliases
22
- .each_with_object({}) do |name, result|
22
+ .each_with_object({}) do |name, result|
23
23
  result[name] = attrs.delete(name) if attrs.key?(name)
24
24
  end
25
25
 
@@ -29,7 +29,7 @@ module Granite
29
29
  end
30
30
  end
31
31
 
32
- alias_method :attributes=, :assign_attributes
32
+ alias attributes= assign_attributes
33
33
  end
34
34
 
35
35
  module ClassMethods
@@ -53,9 +53,9 @@ module Granite
53
53
  end
54
54
  end
55
55
 
56
- private
56
+ private
57
57
 
58
- def run_validations! #:nodoc:
58
+ def run_validations! # :nodoc:
59
59
  super
60
60
  emerge_represented_attributes_errors!
61
61
  errors.empty?
@@ -26,11 +26,14 @@ module Granite
26
26
 
27
27
  unless trust && source.is_a?(self.class)
28
28
  source.each do |entity|
29
- raise AssociationTypeMismatch.new(self.class._scope_model, entity.class) unless entity.is_a?(self.class._scope_model)
29
+ unless entity.is_a?(self.class._scope_model)
30
+ raise AssociationTypeMismatch.new(self.class._scope_model,
31
+ entity.class)
32
+ end
30
33
  end
31
34
  end
32
35
 
33
- super source
36
+ super(source)
34
37
  end
35
38
  end
36
39
 
@@ -38,7 +41,6 @@ module Granite
38
41
  super || self.class._scope_model.respond_to?(method)
39
42
  end
40
43
 
41
- # rubocop:disable Style/MethodMissing
42
44
  # rubocop-0.52.1 doesn't understand that `#respond_to_missing?` is defined above
43
45
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
44
46
  def method_missing(method, *args, **kwargs, &block)
@@ -76,8 +78,6 @@ module Granite
76
78
  end
77
79
  end
78
80
  end
79
- # rubocop:enable Style/MethodMissing
80
-
81
81
  def with_scope
82
82
  previous_scope = self.class._scope_model.current_scope
83
83
  self.class._scope_model.current_scope = self
@@ -1,3 +1,6 @@
1
+ require_relative 'validations/nested'
2
+ require_relative 'validations/associated'
3
+
1
4
  module Granite
2
5
  module Form
3
6
  module Model
@@ -22,7 +25,7 @@ module Granite
22
25
  valid?(context) || raise_validation_error
23
26
  end
24
27
 
25
- protected
28
+ protected
26
29
 
27
30
  def raise_validation_error
28
31
  raise Granite::Form::ValidationError, self
@@ -31,5 +34,3 @@ module Granite
31
34
  end
32
35
  end
33
36
  end
34
-
35
- Dir[File.dirname(__FILE__) + '/validations/*.rb'].each { |file| require file }
@@ -5,7 +5,7 @@ module Granite
5
5
  module Types
6
6
  module ActiveSupport
7
7
  class TimeZone < Float
8
- private
8
+ private
9
9
 
10
10
  def typecast(value)
11
11
  case value
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Array < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  if value.is_a?(::String)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class BigDecimal < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  BigDecimal(Float(value).to_s) if value
@@ -29,7 +29,7 @@ module Granite
29
29
  MAPPING[value]
30
30
  end
31
31
 
32
- private
32
+ private
33
33
 
34
34
  def typecast(value)
35
35
  self.class.typecast(value)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Date < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.try(:to_date)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class DateTime < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.try(:to_datetime)
@@ -10,7 +10,7 @@ module Granite
10
10
  value.transform_values { |v| subtype_definition.prepare(v) }.with_indifferent_access
11
11
  end
12
12
 
13
- private
13
+ private
14
14
 
15
15
  def to_hash(value)
16
16
  Hash[value]
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Float < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  Float(value)
@@ -3,6 +3,7 @@ module Granite
3
3
  module Types
4
4
  class HasSubtype
5
5
  attr_reader :subtype_definition
6
+
6
7
  delegate :reflection, :owner, :type, :name, :enum, to: :subtype_definition
7
8
 
8
9
  def initialize(subtype_definition)
@@ -4,13 +4,13 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class HashWithActionControllerParameters < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  case value
11
11
  when ActionController::Parameters
12
12
  value.to_h if value.permitted?
13
- when ::Hash then
13
+ when ::Hash
14
14
  value
15
15
  end
16
16
  end
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Integer < Float
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  super.try(:to_i)
@@ -5,6 +5,7 @@ module Granite
5
5
  module Types
6
6
  class Object
7
7
  attr_reader :reflection, :owner, :type
8
+
8
9
  delegate :name, to: :reflection
9
10
 
10
11
  def initialize(type, reflection, owner)
@@ -34,7 +35,7 @@ module Granite
34
35
  end.to_set
35
36
  end
36
37
 
37
- private
38
+ private
38
39
 
39
40
  def ensure_type(value)
40
41
  if value.instance_of?(type)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class String < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.to_s
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Time < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.is_a?(::String) && ::Time.zone ? ::Time.zone.parse(value) : value.try(:to_time)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class UUID < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  case value
@@ -41,7 +41,7 @@ module Granite
41
41
  end
42
42
  end
43
43
 
44
- private
44
+ private
45
45
 
46
46
  def evaluate_proc(value, *args)
47
47
  instance_exec(*args, &value)
@@ -1,5 +1,5 @@
1
1
  module Granite
2
2
  module Form
3
- VERSION = '0.5.0'.freeze
3
+ VERSION = '0.6.0'.freeze
4
4
  end
5
5
  end
@@ -57,16 +57,19 @@ describe Granite::Form::ActiveRecord::Associations do
57
57
  describe '#projects' do
58
58
  specify do
59
59
  expect { user.projects << Project.new }
60
- .not_to change { user.read_attribute(:projects) }
60
+ .not_to(change { user.read_attribute(:projects) })
61
61
  end
62
+
62
63
  specify do
63
64
  expect { user.projects << Project.new(title: 'First') }
64
- .not_to change { user.read_attribute(:projects) }
65
+ .not_to(change { user.read_attribute(:projects) })
65
66
  end
67
+
66
68
  specify do
67
69
  expect { user.projects << Project.new(title: 'First') }
68
- .not_to change { user.projects.reload.count }
70
+ .not_to(change { user.projects.reload.count })
69
71
  end
72
+
70
73
  specify do
71
74
  user.projects << Project.new(title: 'First')
72
75
  user.save
@@ -77,12 +80,14 @@ describe Granite::Form::ActiveRecord::Associations do
77
80
  describe '#profile' do
78
81
  specify do
79
82
  expect { user.profile = Profile.new(first_name: 'google.com') }
80
- .not_to change { user.read_attribute(:profile) }
83
+ .not_to(change { user.read_attribute(:profile) })
81
84
  end
85
+
82
86
  specify do
83
87
  expect { user.profile = Profile.new(first_name: 'google.com') }
84
88
  .to change { user.profile }.from(nil).to(an_instance_of(Profile))
85
89
  end
90
+
86
91
  specify do
87
92
  user.profile = Profile.new(first_name: 'google.com')
88
93
  user.save
@@ -110,8 +115,9 @@ describe Granite::Form::ActiveRecord::Associations do
110
115
  describe '#projects' do
111
116
  specify do
112
117
  expect { user.projects << Project.new(title: 'First') }
113
- .not_to change { user.read_attribute(:projects) }
118
+ .not_to(change { user.read_attribute(:projects) })
114
119
  end
120
+
115
121
  specify do
116
122
  user.projects << Project.new(title: 'First')
117
123
  user.save
@@ -120,19 +126,21 @@ describe Granite::Form::ActiveRecord::Associations do
120
126
 
121
127
  context do
122
128
  let(:project) { Project.new(title: 'First') }
129
+
123
130
  before { project.build_author(name: 'Author') }
124
131
 
125
132
  specify do
126
133
  expect { user.projects << project }
127
- .not_to change { user.read_attribute(:projects) }
134
+ .not_to(change { user.read_attribute(:projects) })
128
135
  end
136
+
129
137
  specify do
130
138
  expect do
131
139
  user.projects << project
132
140
  user.save
133
141
  end
134
142
  .to change { user.reload.read_attribute(:projects) }.from([])
135
- .to([{'title' => 'First', 'author' => {'name' => 'Author'}}])
143
+ .to([{ 'title' => 'First', 'author' => { 'name' => 'Author' } }])
136
144
  end
137
145
  end
138
146
  end
@@ -142,22 +150,25 @@ describe Granite::Form::ActiveRecord::Associations do
142
150
  expect { user.profile = Profile.new(first_name: 'google.com') }
143
151
  .to change { user.profile }.from(nil).to(an_instance_of(Profile))
144
152
  end
153
+
145
154
  specify do
146
155
  user.profile = Profile.new(first_name: 'google.com')
147
156
  user.save
148
157
  expect(user.reload.profile.first_name).to eq('google.com')
149
158
  end
159
+
150
160
  specify do
151
161
  expect { user.profile = Profile.new(first_name: 'google.com') }
152
- .not_to change { user.read_attribute(:profile) }
162
+ .not_to(change { user.read_attribute(:profile) })
153
163
  end
164
+
154
165
  specify do
155
166
  expect do
156
167
  user.profile = Profile.new(first_name: 'google.com')
157
168
  user.save
158
169
  end
159
170
  .to change { user.reload.read_attribute(:profile) }.from(nil)
160
- .to({first_name: 'google.com', last_name: nil, admin: nil}.to_json)
171
+ .to({ first_name: 'google.com', last_name: nil, admin: nil }.to_json)
161
172
  end
162
173
  end
163
174
  end
@@ -167,7 +178,8 @@ describe Granite::Form::ActiveRecord::Associations do
167
178
  expect do
168
179
  stub_class(:book, ActiveRecord::Base) do
169
180
  embeds_one :author, class_name: 'Borogoves'
170
- end.reflect_on_association(:author).klass end.to raise_error NameError
181
+ end.reflect_on_association(:author).klass
182
+ end.to raise_error NameError
171
183
  end
172
184
 
173
185
  specify do
@@ -176,7 +188,8 @@ describe Granite::Form::ActiveRecord::Associations do
176
188
  embeds_many :projects, class_name: 'Borogoves' do
177
189
  attribute :title
178
190
  end
179
- end.reflect_on_association(:projects).klass end.to raise_error NameError
191
+ end.reflect_on_association(:projects).klass
192
+ end.to raise_error NameError
180
193
  end
181
194
  end
182
195
 
@@ -194,13 +207,22 @@ describe Granite::Form::ActiveRecord::Associations do
194
207
 
195
208
  specify { expect(User.reflect_on_association(:projects).klass).to eq(User::Project) }
196
209
  specify { expect(User.new.projects).to eq([]) }
197
- specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to be_a(Granite::Form::Model::Associations::Collection::Embedded) }
198
- specify { expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects).to match([have_attributes(title: 'Project')]) }
210
+
211
+ specify do
212
+ expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects)
213
+ .to be_a(Granite::Form::Model::Associations::Collection::Embedded)
214
+ end
215
+
216
+ specify do
217
+ expect(User.new.tap { |u| u.projects.build(title: 'Project') }.projects)
218
+ .to match([have_attributes(title: 'Project')])
219
+ end
199
220
 
200
221
  specify { expect(User.reflect_on_association(:profile).klass).to eq(User::Profile) }
201
222
  specify { expect(User.reflect_on_association(:profile).klass).to be < Profile }
202
223
  specify { expect(User.new.profile).to be_nil }
203
224
  specify { expect(User.new.tap { |u| u.build_profile(first_name: 'Profile') }.profile).to be_a(User::Profile) }
225
+
204
226
  specify do
205
227
  expect(User.new.tap { |u| u.build_profile(first_name: 'Profile') }.profile)
206
228
  .to have_attributes(first_name: 'Profile', last_name: nil, admin: nil, age: nil)