pragma-decorator 2.2.4 → 2.2.6

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
  SHA256:
3
- metadata.gz: 1a95d2a912a1328c91c7ba4bd2cb023ebe3772bf67900fed17e7e6119a0e4a1a
4
- data.tar.gz: 6fe2f0c3e0a87767d25f31e06b4fa8d3dd00fa420456c98f9b47ddb3ee0185a3
3
+ metadata.gz: 98b91700778dde7caf78fca62ad944dd136d8c2ce8d48bb598b085ad8567c741
4
+ data.tar.gz: 9d428e1e9e9c0c6eb00a0e128728da271375d939c60230fe67f7c62466086cc2
5
5
  SHA512:
6
- metadata.gz: 4764e283b3fc8f3d5528f648096a4684dba6f322c0fb59f6a6da32399cfe7bf1c1d56ab9b57e0c6dffa5f658f3dfc17a5509d988adcea15091fb439f0be29101
7
- data.tar.gz: 1fd589f81ff1e00d7e01ba98fb19ef10a812096ac10e471fe707000f7b71d7feb3604298889b8013503cc418d6c86fcd321578ee9a290cf064d989be0c1ebbc7
6
+ metadata.gz: 72b88fd97948adf0f682469ab5854cb302fd967d09da55afc04974d5a2200748dbf70d36699b810eb4d9999723b72e7088d5a0074846b64d17639ad6167becdb
7
+ data.tar.gz: c7c96cce6da9dff0946e003a0ab990013ccf1dfc02e77656b31e4ebd747007e79341c7fc8d042f981c14e8e3d6cc80e6a0dc6ce345bb9fec8fd58d43dc7c71bb
data/.rubocop.yml CHANGED
@@ -1,10 +1,11 @@
1
1
  require: rubocop-rspec
2
2
 
3
3
  AllCops:
4
- TargetRubyVersion: 2.3
4
+ TargetRubyVersion: 2.5
5
5
  Include:
6
6
  - '**/Gemfile'
7
7
  - '**/Rakefile'
8
+ - '**/*.rb'
8
9
  Exclude:
9
10
  - 'bin/*'
10
11
  - 'db/**/*'
@@ -53,7 +54,7 @@ Style/SignalException:
53
54
  Style/BracesAroundHashParameters:
54
55
  EnforcedStyle: context_dependent
55
56
 
56
- Lint/EndAlignment:
57
+ Layout/EndAlignment:
57
58
  EnforcedStyleAlignWith: variable
58
59
  AutoCorrect: true
59
60
 
data/CHANGELOG.md CHANGED
@@ -7,11 +7,24 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.2.6]
11
+
12
+ ### Fixed
13
+
14
+ - Fixed an issue with association definition
15
+
16
+ ## [2.2.5] (yanked)
17
+
18
+ ### Fixed
19
+
20
+ - Fixed the expansion of associations with custom names requiring the original name to be used
21
+
10
22
  ## [2.2.4]
11
23
 
12
24
  ### Fixed
13
25
 
14
- - Fixed an issue where expanding a property would expand properties with the same name in associated objects
26
+ - Fixed an issue where expanding a property would expand properties with the same name in associated
27
+ objects
15
28
 
16
29
  ## [2.2.3]
17
30
 
@@ -80,7 +93,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
80
93
 
81
94
  First Pragma 2 release.
82
95
 
83
- [Unreleased]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.4...HEAD
96
+ [Unreleased]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.6...HEAD
97
+ [2.2.6]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.5...v2.2.6
98
+ [2.2.5]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.4...v2.2.5
84
99
  [2.2.4]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.3...v2.2.4
85
100
  [2.2.3]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.2...v2.2.3
86
101
  [2.2.2]: https://github.com/pragmarb/pragma-decorator/compare/v2.2.1...v2.2.2
data/README.md CHANGED
@@ -96,7 +96,7 @@ This would result in the following representation:
96
96
  ```json
97
97
  {
98
98
  "type": "user",
99
- "...": "...""
99
+ "...": "..."
100
100
  }
101
101
  ```
102
102
 
@@ -167,7 +167,7 @@ module API
167
167
  class Instance < Pragma::Decorator::Base
168
168
  include Pragma::Decorator::Association
169
169
 
170
- belongs_to :customer, decorator: API::V1::Customer::Decorator
170
+ belongs_to :customer, decorator: API::V1::Customer::Decorator::Instance
171
171
  end
172
172
  end
173
173
  end
@@ -211,7 +211,26 @@ decorator.to_json(user_options: {
211
211
  Needless to say, this is done automatically for you when you use all components together through
212
212
  the [pragma](https://github.com/pragmarb/pragma) gem! :)
213
213
 
214
- Associations support all the options supported by `#property`.
214
+ Associations support all the options supported by `#property`. Additionally, `decorator` can be a
215
+ callable object, which is useful for polymorphic associations:
216
+
217
+ ```ruby
218
+ module API
219
+ module V1
220
+ module Discount
221
+ module Decorator
222
+ class Instance < Pragma::Decorator::Base
223
+ include Pragma::Decorator::Association
224
+
225
+ belongs_to :discountable, decorator: -> (discountable) {
226
+ "API::V1::#{discountable.class}::Decorator::Instance".constantize
227
+ }
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
233
+ ```
215
234
 
216
235
  ### Collection
217
236
 
@@ -39,10 +39,10 @@ module Pragma
39
39
  #
40
40
  # This method supports all the usual options accepted by +#property+.
41
41
  #
42
- # @param property_name [Symbol] name of the association
42
+ # @param attribute_name [Symbol] name of the association
43
43
  # @param options [Hash] the association's options
44
- def belongs_to(property_name, options = {})
45
- define_association :belongs_to, property_name, options
44
+ def belongs_to(attribute_name, options = {})
45
+ define_association :belongs_to, attribute_name, options
46
46
  end
47
47
 
48
48
  # Defines a +has_one+ association on this decorator.
@@ -52,36 +52,43 @@ module Pragma
52
52
  #
53
53
  # This method supports all the usual options accepted by +#property+.
54
54
  #
55
- # @param property_name [Symbol] name of the association
55
+ # @param attribute_name [Symbol] name of the association
56
56
  # @param options [Hash] the association's options
57
- def has_one(property_name, options = {}) # rubocop:disable Naming/PredicateName
58
- define_association :has_one, property_name, options
57
+ def has_one(attribute_name, options = {}) # rubocop:disable Naming/PredicateName
58
+ define_association :has_one, attribute_name, options
59
59
  end
60
60
 
61
61
  private
62
62
 
63
- def define_association(type, property_name, options = {})
64
- create_association_definition(type, property_name, options)
65
- create_association_property(type, property_name, options)
63
+ def define_association(type, attribute_name, options = {})
64
+ create_association_definition(type, attribute_name, options)
65
+ create_association_property(attribute_name, options)
66
66
  end
67
67
 
68
- def create_association_definition(type, property_name, options)
69
- associations[property_name.to_sym] = Reflection.new(type, property_name, options)
68
+ def create_association_definition(type, attribute_name, options)
69
+ association_name = options.fetch(:as, attribute_name.to_sym)
70
+ associations[association_name] = Reflection.new(options.merge(
71
+ type: type,
72
+ name: association_name,
73
+ attribute: attribute_name
74
+ ))
70
75
  end
71
76
 
72
- def create_association_property(_type, property_name, options)
77
+ def create_association_property(attribute_name, options)
78
+ association_name = options.fetch(:as, attribute_name.to_sym)
79
+
73
80
  property_options = options.dup.tap { |po| po.delete(:decorator) }.merge(
74
81
  exec_context: :decorator,
75
- as: options[:as] || property_name,
82
+ as: options[:as] || attribute_name,
76
83
  getter: (lambda do |decorator:, user_options:, **_args|
77
84
  Bond.new(
78
- reflection: decorator.class.associations[property_name],
85
+ reflection: decorator.class.associations[association_name],
79
86
  decorator: decorator
80
87
  ).render(user_options)
81
88
  end)
82
89
  )
83
90
 
84
- property("_#{property_name}_association", property_options)
91
+ property("_#{association_name}_association", property_options)
85
92
  end
86
93
  end
87
94
 
@@ -97,7 +97,7 @@ module Pragma
97
97
  association_reflection.association_primary_key
98
98
  end
99
99
 
100
- if model.association(reflection.property).loaded?
100
+ if model.association(reflection.attribute).loaded?
101
101
  return associated_object&.public_send(primary_key)
102
102
  end
103
103
 
@@ -112,7 +112,7 @@ module Pragma
112
112
  end
113
113
 
114
114
  def compute_has_one_fk
115
- if model.association(reflection.property).loaded?
115
+ if model.association(reflection.attribute).loaded?
116
116
  return associated_object&.public_send(association_reflection.association_primary_key)
117
117
  end
118
118
 
@@ -133,7 +133,7 @@ module Pragma
133
133
  end
134
134
 
135
135
  def association_reflection
136
- @association_reflection ||= model.class.reflect_on_association(reflection.property)
136
+ @association_reflection ||= model.class.reflect_on_association(reflection.attribute)
137
137
  end
138
138
 
139
139
  def check_type_consistency
@@ -29,9 +29,9 @@ module Pragma
29
29
  def associated_object
30
30
  case reflection.options[:exec_context]
31
31
  when :decorated
32
- model.public_send(reflection.property)
32
+ model.public_send(reflection.attribute)
33
33
  when :decorator
34
- decorator.public_send(reflection.property)
34
+ decorator.public_send(reflection.attribute)
35
35
  end
36
36
  end
37
37
 
@@ -81,7 +81,7 @@ module Pragma
81
81
  #
82
82
  # @return [Hash|Pragma::Decorator::Base]
83
83
  def render(user_options)
84
- if user_options[:expand]&.any? { |value| value.to_s == reflection.property.to_s }
84
+ if user_options[:expand]&.any? { |value| value.to_s == reflection.name.to_s }
85
85
  expanded_value(user_options)
86
86
  else
87
87
  unexpanded_value
@@ -104,7 +104,7 @@ module Pragma
104
104
  def flatten_expand(expand)
105
105
  expand ||= []
106
106
 
107
- expected_beginning = "#{reflection.property}."
107
+ expected_beginning = "#{reflection.name}."
108
108
 
109
109
  expand.select { |value| value.start_with?(expected_beginning) }.map do |value|
110
110
  value.sub(expected_beginning, '')
@@ -55,7 +55,7 @@ module Pragma
55
55
  # @param model_type [Symbol|String] the real type of the association
56
56
  def initialize(decorator:, reflection:, model_type:)
57
57
  message = <<~MSG.tr("\n", ' ')
58
- #{decorator.class}: Association #{reflection.property} is defined as #{model_type} on
58
+ #{decorator.class}: Association #{reflection.attribute} is defined as #{model_type} on
59
59
  the model, but as #{reflection.type} in the decorator.
60
60
  MSG
61
61
 
@@ -10,17 +10,20 @@ module Pragma
10
10
  # @!attribute [r] type
11
11
  # @return [Symbol] the type of the association
12
12
  #
13
- # @!attribute [r] property
14
- # @return [Symbol] the property holding the associated object
13
+ # @!attribute [r] name
14
+ # @return [Symbol] the name of the association property
15
+ #
16
+ # @!attribute [r] attribute
17
+ # @return [Symbol] the attribute holding the associated object
15
18
  #
16
19
  # @!attribute [r] options
17
20
  # @return [Hash] additional options for the association
18
- attr_reader :type, :property, :options
21
+ attr_reader :type, :name, :attribute, :options
19
22
 
20
23
  # Initializes the association.
21
24
  #
22
25
  # @param type [Symbol] the type of the association
23
- # @param property [Symbol] the property holding the associated object
26
+ # @param attribute [Symbol] the attribute holding the associated object
24
27
  # @param options [Hash] additional options
25
28
  #
26
29
  # @option options [Class|Proc] :decorator the decorator to use for the associated object
@@ -28,9 +31,10 @@ module Pragma
28
31
  # @option options [Boolean] :render_nil (`true`) whether to render a +nil+ association
29
32
  # @option options [Symbol] :exec_context (`decorated`) whether to call the getter on the
30
33
  # decorator (+decorator+) or the decorated object (+decorated+)
31
- def initialize(type, property, **options)
34
+ def initialize(type:, name:, attribute:, **options)
32
35
  @type = type
33
- @property = property
36
+ @name = name
37
+ @attribute = attribute
34
38
  @options = options
35
39
 
36
40
  normalize_options
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Pragma
4
4
  module Decorator
5
- VERSION = '2.2.4'
5
+ VERSION = '2.2.6'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pragma-decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.4
4
+ version: 2.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Desantis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-09 00:00:00.000000000 Z
11
+ date: 2018-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roar