graphiti 1.0.alpha.17 → 1.0.alpha.18

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
  SHA1:
3
- metadata.gz: c143ac6d83e9869c7348b95bbe5dfb7d749f5350
4
- data.tar.gz: 68962230d384cbc40bda6ebca1bc92694edf50b0
3
+ metadata.gz: 7a57e6a62b24c36177cc2d7191754080204f43c3
4
+ data.tar.gz: 28df77df0640febe4fa4bab38ecb9f51ee0f79f6
5
5
  SHA512:
6
- metadata.gz: d808239bc90833c4866ba40eb5f16e4f7ad711dcbcdf3ea2570603a339e46e65f2a574682e397d8ca901b2dc4365c6d54d51314706e0d18b89c9444096a4ea7c
7
- data.tar.gz: 1aaaec7b0f0a5a179e595d8147f7eef5baa60f8690aa4731fcbbef1a0a9f392143b65ed9d21b50ed82f6157ca586ded103e565717f143c8383d1b3327ef385be
6
+ metadata.gz: 828cbf88441e63d92a056296751dc9a87ccb6dd4932946824f44ccef48ef0b828c498e0acbdc4751e89858188d4b13951d781d964d006ca8385daa343217c29f
7
+ data.tar.gz: 2134f9e3dcfb4209179f9c074c17b379af68b00b0182ef7a9d854644b62328ea6d5edc45925d7bf0783b833e0fd5c48166baf29c181b388f9f0442672486ecb5
@@ -16,8 +16,8 @@ module Graphiti
16
16
  if ns.blank?
17
17
  ns = prompt \
18
18
  header: "What is your API namespace?",
19
- description: "This will be used as a route prefix, e.g. if you want the route '/books_api/v1/authors' your namespace would be 'books_api'",
20
- default: 'api'
19
+ description: "This will be used as a route prefix, e.g. if you want the route '/books_api/v1/authors' your namespace would be '/books_api/v1'",
20
+ default: '/api/v1'
21
21
  update_config!('namespace' => ns)
22
22
  end
23
23
 
@@ -0,0 +1,58 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'generator_mixin'
3
+
4
+ module Graphiti
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ include GeneratorMixin
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ class_option :'omit-comments',
11
+ type: :boolean,
12
+ default: false,
13
+ aliases: ['-c'],
14
+ desc: 'Generate without documentation comments'
15
+
16
+ desc "This generator boostraps graphiti"
17
+ def install
18
+ to = File.join('app/resources', "application_resource.rb")
19
+ template('application_resource.rb.erb', to)
20
+
21
+ inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::API\n" do
22
+ app_controller_code
23
+ end
24
+
25
+ inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do
26
+ app_controller_code
27
+ end
28
+
29
+ insert_into_file "config/routes.rb", :after => "Rails.application.routes.draw do\n" do
30
+ <<-STR
31
+ scope path: ApplicationResource.endpoint_namespace, defaults: { format: :jsonapi } do
32
+ # your routes go here
33
+ end
34
+ STR
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def omit_comments?
41
+ @options['omit-comments']
42
+ end
43
+
44
+ def app_controller_code
45
+ str = ""
46
+ str << " include Graphiti::Rails\n"
47
+ str << " include Graphiti::Responders\n"
48
+ str << "\n"
49
+ str << " register_exception Graphiti::Errors::RecordNotFound,\n"
50
+ str << " status: 404\n"
51
+ str << "\n"
52
+ str << " rescue_from Exception do |e|\n"
53
+ str << " handle_exception(e)\n"
54
+ str << " end\n"
55
+ str
56
+ end
57
+ end
58
+ end
@@ -59,10 +59,10 @@ module Graphiti
59
59
  end
60
60
 
61
61
  def generate_route
62
- code = " resources :#{type}"
62
+ code = " resources :#{type}"
63
63
  code << ", only: [#{actions.map { |a| ":#{a}" }.join(', ')}]" if actions.length < 5
64
64
  code << "\n"
65
- inject_into_file 'config/routes.rb', after: "scope path: '/v1' do\n" do
65
+ inject_into_file 'config/routes.rb', after: /ApplicationResource.*$\n/ do
66
66
  code
67
67
  end
68
68
  end
@@ -32,12 +32,12 @@ module Graphiti
32
32
 
33
33
  def generate_resource_specs
34
34
  if actions?('create', 'update', 'destroy')
35
- to = "spec/resources/#{var}/writes_spec.rb.rb"
35
+ to = "spec/resources/#{var}/writes_spec.rb"
36
36
  template('resource_writes_spec.rb.erb', to)
37
37
  end
38
38
 
39
39
  if actions?('index', 'show')
40
- to = "spec/resources/#{var}/reads_spec.rb.rb"
40
+ to = "spec/resources/#{var}/reads_spec.rb"
41
41
  template('resource_reads_spec.rb.erb', to)
42
42
  end
43
43
  end
@@ -9,5 +9,6 @@ class ApplicationResource < Graphiti::Resource
9
9
  # Subclasses can still override this default.
10
10
  <%- end -%>
11
11
  self.abstract_class = true
12
- self.adapter = Graphiti::Adapters::ActiveRecord::Base.new
12
+ self.adapter = Graphiti::Adapters::ActiveRecord::Base
13
+ self.endpoint_namespace = '<%= api_namespace %>'
13
14
  end
@@ -37,20 +37,36 @@ RSpec.describe <%= resource_class %>, type: :resource do
37
37
  end
38
38
 
39
39
  describe 'sorting' do
40
- context 'by id descending' do
40
+ describe 'by id' do
41
41
  let!(:<%= var %>1) { create(:<%= var %>) }
42
42
  let!(:<%= var %>2) { create(:<%= var %>) }
43
43
 
44
- before do
45
- params[:sort] = '-id'
44
+ context 'when ascending' do
45
+ before do
46
+ params[:sort] = 'id'
47
+ end
48
+
49
+ it 'works' do
50
+ render
51
+ expect(d.map(&:id)).to eq([
52
+ <%= var %>1.id,
53
+ <%= var %>2.id
54
+ ])
55
+ end
46
56
  end
47
57
 
48
- it 'works' do
49
- render
50
- expect(d.map(&:id)).to eq([
51
- <%= var %>2.id,
52
- <%= var %>1.id
53
- ])
58
+ context 'when descending' do
59
+ before do
60
+ params[:sort] = '-id'
61
+ end
62
+
63
+ it 'works' do
64
+ render
65
+ expect(d.map(&:id)).to eq([
66
+ <%= var %>2.id,
67
+ <%= var %>1.id
68
+ ])
69
+ end
54
70
  end
55
71
  end
56
72
  end
@@ -43,7 +43,7 @@ RSpec.describe <%= resource_class %>, type: :resource do
43
43
  expect {
44
44
  expect(instance.update_attributes).to eq(true)
45
45
  }.to change { <%= var %>.reload.updated_at }
46
- # and change { instance.foo }.to('bar') <- example
46
+ # .and change { <%= var %>.foo }.to('bar') <- example
47
47
  end
48
48
  end
49
49
 
@@ -69,6 +69,8 @@ module Graphiti
69
69
  end
70
70
 
71
71
  def allow_request?(path, params, action)
72
+ path = path.split('.')[0]
73
+
72
74
  endpoints.any? do |e|
73
75
  has_id = params[:id] || params[:data].try(:[], :id)
74
76
  if [:update, :show, :destroy].include?(context_namespace) && has_id
@@ -76,7 +78,6 @@ module Graphiti
76
78
  path.pop
77
79
  path = path.join('/')
78
80
  end
79
-
80
81
  e[:full_path].to_s == path && e[:actions].include?(context_namespace)
81
82
  end
82
83
  end
@@ -66,7 +66,15 @@ module Graphiti
66
66
  end
67
67
 
68
68
  def stats
69
- @stats ||= @scope.resolve_stats
69
+ @stats ||= if @query.hash[:stats]
70
+ payload = Stats::Payload.new @resource,
71
+ @query,
72
+ @scope.unpaginated_object,
73
+ data
74
+ payload.generate
75
+ else
76
+ {}
77
+ end
70
78
  end
71
79
 
72
80
  def save(action: :create)
@@ -13,14 +13,6 @@ module Graphiti
13
13
  end
14
14
  end
15
15
 
16
- def resolve_stats
17
- if @query.hash[:stats]
18
- Stats::Payload.new(@resource, @query, @unpaginated_object).generate
19
- else
20
- {}
21
- end
22
- end
23
-
24
16
  def resolve
25
17
  if @query.zero_results?
26
18
  []
@@ -14,10 +14,11 @@ module Graphiti
14
14
  # meta: { stats: { total: { count: 100 } } }
15
15
  # }
16
16
  class Payload
17
- def initialize(resource, query, scope)
17
+ def initialize(resource, query, scope, data)
18
18
  @resource = resource
19
19
  @query = query
20
20
  @scope = scope
21
+ @data = data
21
22
  end
22
23
 
23
24
  # Generate the payload for +{ meta: { stats: { ... } } }+
@@ -30,7 +31,11 @@ module Graphiti
30
31
  stats[name] = {}
31
32
 
32
33
  each_calculation(name, calculation) do |calc, function|
33
- stats[name][calc] = function.call(@scope, name)
34
+ args = [@scope, name]
35
+ args << @resource.context if function.arity >= 3
36
+ args << @data if function.arity == 4
37
+
38
+ stats[name][calc] = function.call(*args)
34
39
  end
35
40
  end
36
41
  end
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.alpha.17"
2
+ VERSION = "1.0.alpha.18"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.alpha.17
4
+ version: 1.0.alpha.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-30 00:00:00.000000000 Z
11
+ date: 2018-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable
@@ -220,6 +220,7 @@ files:
220
220
  - graphiti.gemspec
221
221
  - lib/generators/graphiti/api_test_generator.rb
222
222
  - lib/generators/graphiti/generator_mixin.rb
223
+ - lib/generators/graphiti/install_generator.rb
223
224
  - lib/generators/graphiti/resource_generator.rb
224
225
  - lib/generators/graphiti/resource_test_generator.rb
225
226
  - lib/generators/graphiti/templates/application_resource.rb.erb