This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathspec_helper.rb
179 lines (141 loc) · 3.21 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true
require_relative 'support/simplecov'
require 'bundler/setup'
require 'graphql-pundit'
require 'fuubar'
require 'pry'
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.fuubar_progress_bar_options = {format: '[%B] %c/%C',
progress_mark: '#',
remainder_mark: '-'}
end
Field = GraphQL::Pundit::Field
class BaseObject < GraphQL::Schema::Object
field_class GraphQL::Pundit::Field
end
class Query < BaseObject
field :test, Int, null: true
end
class Schema < GraphQL::Schema
query(Query)
end
def spec_context
GraphQL::Query::Context.new(query: Query, schema: Schema, object: {}, values: {})
end
class CarDataset
attr_reader :cars
def initialize(cars)
@cars = cars
end
def object
self
end
def where(&block)
self.class.new(cars.select(&block))
end
def first
cars.first
end
def all
self
end
def to_a
@cars
end
def model
Car
end
def names
self.to_a.map(&:name)
end
end
class Car
attr_reader :name, :country
def self.all
CarDataset.new(CARS)
end
def self.object
self
end
def self.where(&block)
all.where(&block)
end
def self.first(&block)
where(&block).first
end
def object
self
end
def self.longer_then_five
self.where { |c| c.name.length > 5 }
end
def initialize(name, country)
@name = name
@country = country
end
CARS = [{name: 'Toyota', country: 'Japan'},
{name: 'Volkswagen Group', country: 'Germany'},
{name: 'Hyundai', country: 'South Korea'},
{name: 'General Motors', country: 'USA'},
{name: 'Ford', country: 'USA'},
{name: 'Nissan', country: 'Japan'},
{name: 'Honda', country: 'Japan'},
{name: 'Fiat Chrysler', country: 'Italy'},
{name: 'Renault', country: 'France'},
{name: 'Groupe PSA', country: 'France'},
{name: 'Suzuki', country: 'Japan'},
{name: 'SAIC', country: 'China'},
{name: 'Daimler', country: 'Germany'},
{name: 'BMW', country: 'Germany'},
{name: 'Changan', country: 'China'},
{name: 'Mazda', country: 'Japan'},
{name: 'BAIC', country: 'China'},
{name: 'Dongfeng Motor', country: 'China'},
{name: 'Geely', country: 'China'},
{name: 'Great Wall', country: 'China'}].
map { |c| Car.new(c[:name], c[:country]) }
end
class CarPolicy
class Scope
attr_reader :scope
def initialize(_user, scope)
@scope = scope
end
def resolve
@scope.where { |c| c.country == 'Germany' }
end
end
def initialize(_user, car)
@car = car
end
def name?
false
end
def display_name?
false
end
end
class ChineseCarPolicy
class Scope
def initialize(_user, scope)
@scope = scope
end
def resolve
@scope.where { |c| c.country == 'China' }
end
end
def initialize(_user, car)
@car = car
end
def name?
false
end
def display_name?
false
end
end