graphiti 1.0.rc.23 → 1.0.rc.24

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
  SHA256:
3
- metadata.gz: 88bb564c76122827bc72cd0126f4ea22fc25a37cf9c8d00a1a145869dcb220f3
4
- data.tar.gz: f3dc237a65ca60b5492699b1cf118a663c3a7e29807eb69bbf009acf9c703829
3
+ metadata.gz: d67c2cb973db323b43cb172e2de8a2b99e363643aaaaf32fe3dfb40136c495d7
4
+ data.tar.gz: a2c8f90f58a5d87fa1ec7bba822c0ac8b4e50a59d7ad1d3f0b71e031d97084d8
5
5
  SHA512:
6
- metadata.gz: c15a15f119ef23f72ef9bcace90f26de31aa114472f2a083a0ee82da5a1dea14b84539f8c56ac6067d3632fa42db0cd76994bd4169155a41fd1c846a9cee5df2
7
- data.tar.gz: 54394fb25b0cae16f0f0cd463434406f51d8360a6b8e4419a3b518d2645396896d8ed1973ab395a0131557add30ced2d63f092d3b8b9903d14be4f159620060f
6
+ metadata.gz: 8b08c3eb3a4ab96dfeb1af6642004c1b22d052fd6d3e0d897ee9dc8ddbc84430b49623c819fab06e4744673d5fa61e8b4b289df2d79f0853a79b2d1fc4f7c9d7
7
+ data.tar.gz: 05c816f48067d09d0d7f498c0dbdd5011d4552d1c720e947bf72513d3a761dec0425ef4d138f49de056833fd1c9998582a8bfabc9d5a26a9889ba91ffb2d605c
data/graphiti.gemspec CHANGED
@@ -18,8 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
  spec.required_ruby_version = "~> 2.3"
20
20
 
21
- # Pinning this version until backwards-incompatibility is addressed
22
- spec.add_dependency "jsonapi-serializable", "= 0.3.0"
21
+ spec.add_dependency "jsonapi-serializable", "~> 0.3.0"
23
22
  spec.add_dependency "dry-types", "~> 0.13"
24
23
  spec.add_dependency "graphiti_errors", "~> 1.0.beta.1"
25
24
  spec.add_dependency "concurrent-ruby", "~> 1.0"
@@ -14,12 +14,19 @@ module Graphiti
14
14
  default: false,
15
15
  aliases: ["--omit-comments", "-c"],
16
16
  desc: "Generate without documentation comments"
17
+
17
18
  class_option :actions,
18
19
  type: :array,
19
20
  default: nil,
20
21
  aliases: ["--actions", "-a"],
21
22
  desc: 'Array of controller actions to support, e.g. "index show destroy"'
22
23
 
24
+ class_option :'attributes-from',
25
+ banner: "Model",
26
+ type: :string,
27
+ aliases: ["--model", "-m"],
28
+ desc: "Specify to use attributes from a particular model"
29
+
23
30
  desc "This generator creates a resource file at app/resources, as well as corresponding controller/specs/route/etc"
24
31
  def generate_all
25
32
  generate_model
@@ -58,6 +65,54 @@ module Graphiti
58
65
  @options["omit-comments"]
59
66
  end
60
67
 
68
+ def attributes_class
69
+ return @attributes_class if @attributes_class
70
+
71
+ case @options["attributes-from"]
72
+ # thor will set the value to the key if no value is specified
73
+ when "attributes-from"
74
+ klass = class_name
75
+ when :kind_of?, String
76
+ klass = @options["attributes-from"].classify
77
+ else
78
+ # return nil if attributes-from isn't set or has an invalid value
79
+ return
80
+ end
81
+ begin
82
+ @attributes_class = klass.safe_constantize
83
+ rescue NameError
84
+ raise NameError, "attributes-from #{klass.inspect} does not exist."
85
+ end
86
+ end
87
+
88
+ ##
89
+ # Generates a list of OpenStruct(:name, :type) objects that map to
90
+ # the +attributes_class+ columns.
91
+ ##
92
+ def default_attributes
93
+ unless attributes_class.is_a?(Class) && attributes_class <= ApplicationRecord
94
+ raise "Unable to set #{self} default_attributes from #{attributes_class}. #{attributes_class} must be a kind of ApplicationRecord"
95
+ end
96
+ if attributes_class.table_exists?
97
+ return attributes_class.columns.map do |c|
98
+ OpenStruct.new({name: c.name.to_sym, type: c.type})
99
+ end
100
+ else
101
+ raise "#{attributes_class} table must exist. Please run migrations."
102
+ end
103
+ end
104
+
105
+ def resource_attributes
106
+ # set a temporary variable because overriding attributes causes
107
+ # weird behavior when the generator is run. It will override
108
+ # everytime regardless of the conditional.
109
+ if !attributes_class.nil?
110
+ default_attributes
111
+ else
112
+ attributes
113
+ end
114
+ end
115
+
61
116
  def responders?
62
117
  defined?(Responders)
63
118
  end
@@ -1,7 +1,7 @@
1
1
  <% module_namespacing do -%>
2
2
  class <%= class_name %>Resource < ApplicationResource
3
- <%- attributes.each do |a| -%>
4
- <%- if [:created_at, :updated_at].include?(a.name.to_sym) -%>
3
+ <%- resource_attributes.each do |a| -%>
4
+ <%- if [:id, :created_at, :updated_at].include?(a.name.to_sym) -%>
5
5
  attribute :<%= a.name %>, :<%= a.type %>, writable: false
6
6
  <%- else -%>
7
7
  attribute :<%= a.name %>, :<%= a.type %>
@@ -35,9 +35,23 @@ module Graphiti
35
35
  end
36
36
  end
37
37
 
38
+ # See above comment
39
+ module ResourceForOverride
40
+ def resource_for(object, options, inferrer)
41
+ resource = object.instance_variable_get(:@__graphiti_resource)
42
+ klass = object.instance_variable_get(:@__graphiti_serializer)
43
+ klass.new(options.merge(object: object, resource: resource))
44
+ end
45
+ end
46
+
38
47
  JSONAPI::Serializable::Relationship
39
48
  .send(:prepend, RelationshipOverrides)
40
- JSONAPI::Serializable::Renderer
41
- .send(:prepend, RendererOverrides)
49
+
50
+ if JSONAPI::Serializable.methods.include?(:resource_for)
51
+ JSONAPI::Serializable.singleton_class.send(:prepend, ResourceForOverride)
52
+ else
53
+ JSONAPI::Serializable::Renderer
54
+ .send(:prepend, RendererOverrides)
55
+ end
42
56
  end
43
57
  end
@@ -45,7 +45,7 @@ module Graphiti
45
45
  options[:expose][:proxy] = proxy
46
46
  options[:include] = proxy.include_hash
47
47
  options[:links] = proxy.pagination.links if proxy.pagination.links?
48
- options[:meta] ||= {}
48
+ options[:meta] ||= proxy.meta
49
49
  options[:meta][:stats] = proxy.stats unless proxy.stats.empty?
50
50
  options[:meta][:debug] = Debugger.to_a if debug_json?
51
51
 
@@ -33,7 +33,6 @@ module Graphiti
33
33
  end
34
34
 
35
35
  def jsonapi_render_options(opts = {})
36
- opts[:meta] ||= {}
37
36
  opts[:expose] ||= {}
38
37
  opts[:expose][:context] = Graphiti.context[:object]
39
38
  opts
@@ -64,6 +63,10 @@ module Graphiti
64
63
  end
65
64
  alias to_a data
66
65
 
66
+ def meta
67
+ @meta ||= data.respond_to?(:meta) ? data.meta : {}
68
+ end
69
+
67
70
  def each(&blk)
68
71
  to_a.each(&blk)
69
72
  end
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.rc.23"
2
+ VERSION = "1.0.rc.24"
3
3
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.rc.23
4
+ version: 1.0.rc.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-09 00:00:00.000000000 Z
11
+ date: 2019-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency