souls 0.23.5 → 0.23.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,52 +1,51 @@
1
1
  module Souls
2
2
  module Generate
3
- class << self
4
- ## Generate Policy
5
- def policy class_name: "souls"
6
- dir_name = "./app/policies"
7
- FileUtils.mkdir_p dir_name unless Dir.exist? dir_name
8
- file_path = "#{dir_name}/#{class_name.singularize}_policy.rb"
9
- return "Policy already exist! #{file_path}" if File.exist? file_path
10
- File.open(file_path, "w") do |f|
11
- f.write <<~EOS
12
- class #{class_name.camelize}Policy < ApplicationPolicy
13
- def show?
14
- true
15
- end
16
-
17
- def index?
18
- true
19
- end
20
-
21
- def create?
22
- user_permissions?
23
- end
24
-
25
- def update?
26
- user_permissions?
27
- end
28
-
29
- def delete?
30
- admin_permissions?
31
- end
32
-
33
- private
34
-
35
- def user_permissions?
36
- @user.master? or @user.admin? or @user.user?
37
- end
38
-
39
- def admin_permissions?
40
- @user.master? or @user.admin?
41
- end
3
+ ## Generate Policy
4
+ def self.policy(class_name: "souls")
5
+ dir_name = "./app/policies"
6
+ FileUtils.mkdir_p(dir_name) unless Dir.exist?(dir_name)
7
+ file_path = "#{dir_name}/#{class_name.singularize}_policy.rb"
8
+ return "Policy already exist! #{file_path}" if File.exist?(file_path)
9
+
10
+ File.open(file_path, "w") do |f|
11
+ f.write(<<~TEXT)
12
+ class #{class_name.camelize}Policy < ApplicationPolicy
13
+ def show?
14
+ true
15
+ end
16
+
17
+ def index?
18
+ true
19
+ end
20
+
21
+ def create?
22
+ user_permissions?
23
+ end
24
+
25
+ def update?
26
+ user_permissions?
27
+ end
28
+
29
+ def delete?
30
+ admin_permissions?
31
+ end
32
+
33
+ private
34
+
35
+ def user_permissions?
36
+ @user.master? or @user.admin? or @user.user?
37
+ end
38
+
39
+ def admin_permissions?
40
+ @user.master? or @user.admin?
42
41
  end
43
- EOS
44
- end
45
- puts Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }]
46
- file_path
47
- rescue StandardError => e
48
- raise StandardError, e
42
+ end
43
+ TEXT
49
44
  end
45
+ puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
46
+ file_path
47
+ rescue StandardError => e
48
+ raise(StandardError, e)
50
49
  end
51
50
  end
52
51
  end
@@ -1,64 +1,64 @@
1
1
  module Souls
2
2
  module Generate
3
- class << self
4
- ## Generate Query / Queries
5
- def create_queries class_name: "souls"
6
- file_path = "./app/graphql/queries/#{class_name.pluralize}.rb"
7
- return "Query already exist! #{file_path}" if File.exist? file_path
8
- File.open(file_path, "w") do |f|
9
- f.write <<~EOS
10
- module Queries
11
- class #{class_name.camelize.pluralize} < Queries::BaseQuery
12
- type [Types::#{class_name.camelize}Type], null: false
3
+ ## Generate Query / Queries
4
+ def self.create_queries(class_name: "souls")
5
+ file_path = "./app/graphql/queries/#{class_name.pluralize}.rb"
6
+ return "Query already exist! #{file_path}" if File.exist?(file_path)
13
7
 
14
- def resolve
15
- ::#{class_name.camelize}.all
16
- rescue StandardError => error
17
- GraphQL::ExecutionError.new error
18
- end
8
+ File.open(file_path, "w") do |f|
9
+ f.write(<<~TEXT)
10
+ module Queries
11
+ class #{class_name.camelize.pluralize} < Queries::BaseQuery
12
+ type [Types::#{class_name.camelize}Type], null: false
13
+
14
+ def resolve
15
+ ::#{class_name.camelize}.all
16
+ rescue StandardError => error
17
+ GraphQL::ExecutionError.new error
19
18
  end
20
19
  end
21
- EOS
22
- end
23
- puts Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }]
24
- file_path
25
- rescue StandardError => e
26
- raise StandardError, e
20
+ end
21
+ TEXT
27
22
  end
23
+ puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
24
+ file_path
25
+ rescue StandardError => e
26
+ raise(StandardError, e)
27
+ end
28
+
29
+ def self.create_query(class_name: "souls")
30
+ file_path = "./app/graphql/queries/#{class_name}.rb"
31
+ return "Query already exist! #{file_path}" if File.exist?(file_path)
28
32
 
29
- def create_query class_name: "souls"
30
- file_path = "./app/graphql/queries/#{class_name}.rb"
31
- return "Query already exist! #{file_path}" if File.exist? file_path
32
- File.open(file_path, "w") do |f|
33
- f.write <<~EOS
34
- module Queries
35
- class #{class_name.camelize} < Queries::BaseQuery
36
- type Types::#{class_name.camelize}Type, null: false
37
- argument :id, String, required: true
33
+ File.open(file_path, "w") do |f|
34
+ f.write(<<~TEXT)
35
+ module Queries
36
+ class #{class_name.camelize} < Queries::BaseQuery
37
+ type Types::#{class_name.camelize}Type, null: false
38
+ argument :id, String, required: true
38
39
 
39
- def resolve **args
40
- _, data_id = SoulsApiSchema.from_global_id args[:id]
41
- ::#{class_name.camelize}.find(data_id)
42
- rescue StandardError => error
43
- GraphQL::ExecutionError.new error
44
- end
40
+ def resolve **args
41
+ _, data_id = SoulsApiSchema.from_global_id args[:id]
42
+ ::#{class_name.camelize}.find(data_id)
43
+ rescue StandardError => error
44
+ GraphQL::ExecutionError.new error
45
45
  end
46
46
  end
47
- EOS
48
- puts Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }]
49
- file_path
50
- rescue StandardError => e
51
- raise StandardError, e
52
- end
53
- end
54
-
55
- def query class_name: "souls"
56
- singularized_class_name = class_name.singularize
57
- create_query(class_name: singularized_class_name)
58
- create_queries(class_name: singularized_class_name)
47
+ end
48
+ TEXT
49
+ puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
50
+ file_path
59
51
  rescue StandardError => e
60
- raise StandardError, e
52
+ raise(StandardError, e)
61
53
  end
62
54
  end
55
+
56
+ def self.query(class_name: "souls")
57
+ singularized_class_name = class_name.singularize
58
+ create_query(class_name: singularized_class_name)
59
+ create_queries(class_name: singularized_class_name)
60
+ rescue StandardError => e
61
+ raise(StandardError, e)
62
+ end
63
63
  end
64
64
  end
@@ -1,155 +1,152 @@
1
1
  module Souls
2
2
  module Generate
3
- class << self
4
- ## Generate Resolver
5
- def resolver_head class_name: "souls"
6
- FileUtils.mkdir_p "./app/graphql/resolvers" unless Dir.exist? "./app/graphql/resolvers"
7
- file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
8
- @relation_params = []
9
- File.open(file_path, "w") do |f|
10
- f.write <<~EOS
11
- module Resolvers
12
- class #{class_name.camelize}Search < Base
13
- include SearchObject.module(:graphql)
14
- scope { ::#{class_name.camelize}.all }
15
- type Types::#{class_name.camelize}Type.connection_type, null: false
16
- description "Search #{class_name.camelize}"
3
+ ## Generate Resolver
4
+ def self.resolver_head(class_name: "souls")
5
+ FileUtils.mkdir_p("./app/graphql/resolvers") unless Dir.exist?("./app/graphql/resolvers")
6
+ file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
7
+ @relation_params = []
8
+ File.open(file_path, "w") do |f|
9
+ f.write(<<~TEXT)
10
+ module Resolvers
11
+ class #{class_name.camelize}Search < Base
12
+ include SearchObject.module(:graphql)
13
+ scope { ::#{class_name.camelize}.all }
14
+ type Types::#{class_name.camelize}Type.connection_type, null: false
15
+ description "Search #{class_name.camelize}"
17
16
 
18
- class #{class_name.camelize}Filter < ::Types::BaseInputObject
19
- argument :OR, [self], required: false
20
- EOS
21
- end
17
+ class #{class_name.camelize}Filter < ::Types::BaseInputObject
18
+ argument :OR, [self], required: false
19
+ TEXT
22
20
  end
21
+ end
23
22
 
24
- def resolver_params class_name: "souls"
25
- file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
26
- path = "./db/schema.rb"
27
- @on = false
28
- @user_exist = false
29
- @relation_params = []
30
- File.open(file_path, "a") do |new_line|
31
- File.open(path, "r") do |f|
32
- f.each_line.with_index do |line, i|
33
- if @on
34
- if line.include?("end") || line.include?("t.index")
35
- break
36
- end
37
- field = "[String]" if line.include?("array: true")
38
- type, name = get_type_and_name(line)
39
- field ||= type_check type
40
- case name
41
- when "user_id"
42
- @user_exist = true
43
- when /$*_id\z/
44
- @relation_params << name
45
- new_line.write " argument :#{name}, String, required: false\n"
46
- when "created_at", "updated_at"
47
- next
48
- else
49
- new_line.write " argument :#{name}, #{field}, required: false\n"
50
- end
23
+ def self.resolver_params(class_name: "souls")
24
+ file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
25
+ path = "./db/schema.rb"
26
+ @on = false
27
+ @user_exist = false
28
+ @relation_params = []
29
+ File.open(file_path, "a") do |new_line|
30
+ File.open(path, "r") do |f|
31
+ f.each_line.with_index do |line, _i|
32
+ if @on
33
+ break if line.include?("end") || line.include?("t.index")
34
+
35
+ field = "[String]" if line.include?("array: true")
36
+ type, name = get_type_and_name(line)
37
+ field ||= type_check(type)
38
+ case name
39
+ when "user_id"
40
+ @user_exist = true
41
+ when /$*_id\z/
42
+ @relation_params << name
43
+ new_line.write(" argument :#{name}, String, required: false\n")
44
+ when "created_at", "updated_at"
45
+ next
46
+ else
47
+ new_line.write(" argument :#{name}, #{field}, required: false\n")
51
48
  end
52
- @on = true if table_check(line: line, class_name: class_name)
53
49
  end
50
+ @on = true if table_check(line: line, class_name: class_name)
54
51
  end
55
52
  end
56
53
  end
57
-
58
- def resolver_after_params class_name: "souls"
59
- file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
60
- File.open(file_path, "a") do |f|
61
- f.write <<-EOS
62
- argument :start_date, String, required: false
63
- argument :end_date, String, required: false
64
54
  end
65
55
 
66
- option :filter, type: #{class_name.camelize}Filter, with: :apply_filter
67
- option :first, type: types.Int, with: :apply_first
68
- option :skip, type: types.Int, with: :apply_skip
56
+ def self.resolver_after_params(class_name: "souls")
57
+ file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
58
+ File.open(file_path, "a") do |f|
59
+ f.write(<<-TEXT)
60
+ argument :start_date, String, required: false
61
+ argument :end_date, String, required: false
62
+ end
69
63
 
70
- def apply_filter(scope, value)
71
- branches = normalize_filters(value).inject { |a, b| a.or(b) }
72
- scope.merge branches
73
- end
64
+ option :filter, type: #{class_name.camelize}Filter, with: :apply_filter
65
+ option :first, type: types.Int, with: :apply_first
66
+ option :skip, type: types.Int, with: :apply_skip
74
67
 
75
- def normalize_filters(value, branches = [])
76
- scope = ::#{class_name.camelize}.all
77
- EOS
78
- end
68
+ def apply_filter(scope, value)
69
+ branches = normalize_filters(value).inject { |a, b| a.or(b) }
70
+ scope.merge branches
71
+ end
72
+
73
+ def normalize_filters(value, branches = [])
74
+ scope = ::#{class_name.camelize}.all
75
+ TEXT
79
76
  end
77
+ end
80
78
 
81
- def resolver_before_end class_name: "souls"
82
- file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
83
- path = "./db/schema.rb"
84
- @on = false
85
- @user_exist = false
86
- @relation_params = []
87
- File.open(file_path, "a") do |new_line|
88
- File.open(path, "r") do |f|
89
- f.each_line.with_index do |line, i|
90
- if @on
91
- if line.include?("end") || line.include?("t.index")
92
- break
93
- end
94
- type, name = get_type_and_name(line)
95
- if line.include?("array: true")
96
- new_line.write " scope = scope.where(\"#{name} @> ARRAY[?]::text[]\", value[:#{name}]) if value[:#{name}]\n"
97
- next
98
- end
99
- case name
100
- when "user_id"
101
- @user_exist = true
102
- when /$*_id\z/
103
- @relation_params << name
104
- new_line.write " scope = scope.where(#{name}: decode_global_key(value[:#{name}])) if value[:#{name}]\n"
105
- when "created_at", "updated_at"
106
- next
79
+ def self.resolver_before_end(class_name: "souls")
80
+ file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
81
+ path = "./db/schema.rb"
82
+ @on = false
83
+ @user_exist = false
84
+ @relation_params = []
85
+ File.open(file_path, "a") do |new_line|
86
+ File.open(path, "r") do |f|
87
+ f.each_line.with_index do |line, _i|
88
+ if @on
89
+ break if line.include?("end") || line.include?("t.index")
90
+
91
+ type, name = get_type_and_name(line)
92
+ if line.include?("array: true")
93
+ new_line.write(" scope = scope.where(\"#{name} @> ARRAY[?]::text[]\", value[:#{name}]) if value[:#{name}]\n")
94
+ next
95
+ end
96
+ case name
97
+ when "user_id"
98
+ @user_exist = true
99
+ when /$*_id\z/
100
+ @relation_params << name
101
+ new_line.write(" scope = scope.where(#{name}: decode_global_key(value[:#{name}])) if value[:#{name}]\n")
102
+ when "created_at", "updated_at"
103
+ next
104
+ else
105
+ case type
106
+ when "boolean"
107
+ new_line.write(" scope = scope.where(#{name}: value[:#{name}]) unless value[:#{name}].nil?\n")
107
108
  else
108
- case type
109
- when "boolean"
110
- new_line.write " scope = scope.where(#{name}: value[:#{name}]) unless value[:#{name}].nil?\n"
111
- else
112
- new_line.write " scope = scope.where(#{name}: value[:#{name}]) if value[:#{name}]\n"
113
- end
109
+ new_line.write(" scope = scope.where(#{name}: value[:#{name}]) if value[:#{name}]\n")
114
110
  end
115
111
  end
116
- @on = true if table_check(line: line, class_name: class_name)
117
112
  end
113
+ @on = true if table_check(line: line, class_name: class_name)
118
114
  end
119
115
  end
120
116
  end
117
+ end
121
118
 
122
- def resolver_end class_name: "souls"
123
- file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
124
- File.open(file_path, "a") do |f|
125
- f.write <<~EOS
126
- scope = scope.where("created_at >= ?", value[:start_date]) if value[:start_date]
127
- scope = scope.where("created_at <= ?", value[:end_date]) if value[:end_date]
128
- branches << scope
129
- value[:OR].inject(branches) { |s, v| normalize_filters(v, s) } if value[:OR].present?
130
- branches
131
- end
119
+ def self.resolver_end(class_name: "souls")
120
+ file_path = "./app/graphql/resolvers/#{class_name.singularize}_search.rb"
121
+ File.open(file_path, "a") do |f|
122
+ f.write(<<~TEXT)
123
+ scope = scope.where("created_at >= ?", value[:start_date]) if value[:start_date]
124
+ scope = scope.where("created_at <= ?", value[:end_date]) if value[:end_date]
125
+ branches << scope
126
+ value[:OR].inject(branches) { |s, v| normalize_filters(v, s) } if value[:OR].present?
127
+ branches
132
128
  end
133
129
  end
134
- EOS
135
- end
136
- file_path
130
+ end
131
+ TEXT
137
132
  end
133
+ file_path
134
+ end
138
135
 
139
- def resolver class_name: "souls"
140
- singularized_class_name = class_name.singularize.underscore
141
- file_path = "./app/graphql/resolvers/#{singularized_class_name}_search.rb"
142
- return "Resolver already exist! #{file_path}" if File.exist? file_path
143
- resolver_head class_name: singularized_class_name
144
- resolver_params class_name: singularized_class_name
145
- resolver_after_params class_name: singularized_class_name
146
- resolver_before_end class_name: singularized_class_name
147
- resolver_end class_name: singularized_class_name
148
- puts Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }]
149
- file_path
150
- rescue StandardError => e
151
- raise StandardError, e
152
- end
136
+ def self.resolver(class_name: "souls")
137
+ singularized_class_name = class_name.singularize.underscore
138
+ file_path = "./app/graphql/resolvers/#{singularized_class_name}_search.rb"
139
+ return "Resolver already exist! #{file_path}" if File.exist?(file_path)
140
+
141
+ resolver_head(class_name: singularized_class_name)
142
+ resolver_params(class_name: singularized_class_name)
143
+ resolver_after_params(class_name: singularized_class_name)
144
+ resolver_before_end(class_name: singularized_class_name)
145
+ resolver_end(class_name: singularized_class_name)
146
+ puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
147
+ file_path
148
+ rescue StandardError => e
149
+ raise(StandardError, e)
153
150
  end
154
151
  end
155
152
  end