ibrain-core 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/ibrain/base_controller.rb +12 -14
- data/app/controllers/ibrain/core/graphql_controller.rb +79 -83
- data/app/graphql/ibrain/base_schema.rb +50 -52
- data/app/graphql/ibrain/extentions/default_value.rb +7 -11
- data/app/graphql/ibrain/extentions/roles.rb +15 -20
- data/app/graphql/ibrain/extentions/session_required.rb +10 -14
- data/app/graphql/ibrain/lazy/base.rb +2 -6
- data/app/graphql/ibrain/loaders/association_loader.rb +51 -55
- data/app/graphql/ibrain/mutations/base_mutation.rb +52 -56
- data/app/graphql/ibrain/policies/base_policy.rb +43 -47
- data/app/graphql/ibrain/policies/graphql_policy.rb +2 -6
- data/app/graphql/ibrain/resolvers/base_aggregate.rb +6 -10
- data/app/graphql/ibrain/resolvers/base_resolver.rb +8 -12
- data/app/graphql/ibrain/types/aggregate_type.rb +4 -8
- data/app/graphql/ibrain/types/attribute_type.rb +2 -5
- data/app/graphql/ibrain/types/base_api_connection.rb +6 -10
- data/app/graphql/ibrain/types/base_api_edge.rb +4 -8
- data/app/graphql/ibrain/types/base_api_field.rb +7 -11
- data/app/graphql/ibrain/types/base_api_object.rb +6 -10
- data/app/graphql/ibrain/types/base_argument.rb +3 -7
- data/app/graphql/ibrain/types/base_connection.rb +7 -11
- data/app/graphql/ibrain/types/base_edge.rb +3 -7
- data/app/graphql/ibrain/types/base_enum.rb +1 -5
- data/app/graphql/ibrain/types/base_field.rb +6 -10
- data/app/graphql/ibrain/types/base_input_object.rb +6 -10
- data/app/graphql/ibrain/types/base_interface.rb +5 -9
- data/app/graphql/ibrain/types/base_object.rb +12 -16
- data/app/graphql/ibrain/types/base_scalar.rb +1 -5
- data/app/graphql/ibrain/types/base_type.rb +4 -10
- data/app/graphql/ibrain/types/base_union.rb +3 -7
- data/app/graphql/ibrain/types/filter_type.rb +1 -5
- data/app/graphql/ibrain/types/node_type.rb +4 -8
- data/app/graphql/ibrain/util/field_combiner.rb +5 -9
- data/app/graphql/ibrain/util/query_combiner.rb +4 -8
- data/app/models/concerns/ibrain/soft_deletable.rb +6 -8
- data/app/models/concerns/ibrain/user_api_authentication.rb +14 -16
- data/app/models/concerns/ibrain/user_methods.rb +11 -13
- data/app/models/ibrain/ability.rb +31 -32
- data/app/models/ibrain/aggregate.rb +5 -7
- data/app/models/ibrain/application_record.rb +2 -4
- data/app/models/ibrain/legacy_user.rb +7 -13
- data/app/models/ibrain/role.rb +6 -8
- data/app/models/ibrain/role_user.rb +8 -10
- data/app/repositories/ibrain/base_repository.rb +9 -11
- data/lib/generators/ibrain/core/model_generator.rb +17 -21
- data/lib/generators/ibrain/graphql/core.rb +55 -59
- data/lib/generators/ibrain/graphql/mutation_generator.rb +58 -66
- data/lib/generators/ibrain/graphql/object_generator.rb +58 -70
- data/lib/generators/ibrain/graphql/resolver_generator.rb +17 -25
- data/lib/generators/ibrain/graphql/resolvers_generator.rb +59 -67
- data/lib/generators/ibrain/graphql/templates/aggregate.erb +5 -7
- data/lib/generators/ibrain/graphql/templates/input.erb +5 -9
- data/lib/generators/ibrain/graphql/templates/mutation.erb +24 -26
- data/lib/generators/ibrain/graphql/templates/object.erb +6 -10
- data/lib/generators/ibrain/graphql/templates/resolver.erb +9 -11
- data/lib/generators/ibrain/graphql/templates/resolvers.erb +8 -10
- data/lib/generators/ibrain/graphql/type_generator.rb +84 -88
- data/lib/generators/ibrain/install/install_generator.rb +137 -140
- data/lib/generators/ibrain/install/templates/graphql/types/mutation_type.rb.tt +13 -15
- data/lib/generators/ibrain/install/templates/graphql/types/query_type.rb.tt +11 -13
- data/lib/generators/ibrain/install/templates/rubocop.yml.tt +83 -30
- data/lib/ibrain/app_configuration.rb +34 -36
- data/lib/ibrain/core/class_constantizer.rb +30 -33
- data/lib/ibrain/core/controller_helpers/auth.rb +53 -59
- data/lib/ibrain/core/controller_helpers/current_host.rb +5 -11
- data/lib/ibrain/core/controller_helpers/response.rb +43 -49
- data/lib/ibrain/core/controller_helpers/strong_parameters.rb +9 -15
- data/lib/ibrain/core/engine.rb +5 -9
- data/lib/ibrain/core/environment.rb +5 -9
- data/lib/ibrain/core/environment_extension.rb +13 -17
- data/lib/ibrain/core/role_configuration.rb +52 -54
- data/lib/ibrain/core/validators/email.rb +15 -17
- data/lib/ibrain/core/version.rb +2 -2
- data/lib/ibrain/core/versioned_value.rb +61 -65
- data/lib/ibrain/encryptor.rb +18 -20
- data/lib/ibrain/logger.rb +13 -15
- data/lib/ibrain/permission_sets/base.rb +22 -26
- data/lib/ibrain/permission_sets/super_user.rb +3 -7
- data/lib/ibrain/permitted_attributes.rb +18 -20
- data/lib/ibrain/preferences/configuration.rb +135 -137
- data/lib/ibrain/preferences/preferable.rb +158 -162
- data/lib/ibrain/preferences/preferable_class_methods.rb +112 -114
- data/lib/ibrain/user_class_handle.rb +22 -24
- metadata +2 -2
@@ -1,13 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
Array(query_types).inject({}) do |acc, query_type|
|
8
|
-
acc.merge!(query_type.fields)
|
9
|
-
end
|
10
|
-
end
|
3
|
+
class Ibrain::Util::QueryCombiner
|
4
|
+
def self.combine(query_types)
|
5
|
+
Array(query_types).inject({}) do |acc, query_type|
|
6
|
+
acc.merge!(query_type.fields)
|
11
7
|
end
|
12
8
|
end
|
13
9
|
end
|
@@ -2,15 +2,13 @@
|
|
2
2
|
|
3
3
|
require 'discard'
|
4
4
|
|
5
|
-
module Ibrain
|
6
|
-
|
7
|
-
extend ActiveSupport::Concern
|
5
|
+
module Ibrain::SoftDeletable
|
6
|
+
extend ActiveSupport::Concern
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
included do
|
9
|
+
include Discard::Model
|
10
|
+
self.discard_column = :deleted_at
|
12
11
|
|
13
|
-
|
14
|
-
end
|
12
|
+
default_scope { kept }
|
15
13
|
end
|
16
14
|
end
|
@@ -1,23 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Ibrain
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
3
|
+
module Ibrain::UserApiAuthentication
|
4
|
+
def generate_ibrain_api_key!
|
5
|
+
generate_ibrain_api_key
|
6
|
+
save!
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def generate_ibrain_api_key
|
10
|
+
self.ibrain_api_key = SecureRandom.hex(24)
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
def clear_ibrain_api_key!
|
14
|
+
clear_ibrain_api_key
|
15
|
+
save!
|
16
|
+
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
end
|
18
|
+
def clear_ibrain_api_key
|
19
|
+
self.ibrain_api_key = nil
|
22
20
|
end
|
23
21
|
end
|
@@ -1,23 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Ibrain
|
4
|
-
|
5
|
-
extend ActiveSupport::Concern
|
3
|
+
module Ibrain::UserMethods
|
4
|
+
extend ActiveSupport::Concern
|
6
5
|
|
7
|
-
|
6
|
+
include Ibrain::UserApiAuthentication
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
included do
|
9
|
+
after_create :auto_generate_ibrain_api_key
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
include Ibrain::RansackableAttributes unless included_modules.include?(Ibrain::RansackableAttributes)
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
def auto_generate_ibrain_api_key
|
15
|
+
return if !respond_to?(:ibrain_api_key) || ibrain_api_key.present?
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
17
|
+
if Ibrain::Config.generate_api_key_for_all_roles || (ibrain_roles.map(&:name) & Ibrain::Config.roles_for_auto_api_key).any?
|
18
|
+
generate_ibrain_api_key!
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -5,47 +5,46 @@
|
|
5
5
|
#
|
6
6
|
# See http://github.com/ryanb/cancan for more details on cancan.
|
7
7
|
require 'cancan'
|
8
|
-
module Ibrain
|
9
|
-
class Ability
|
10
|
-
include CanCan::Ability
|
11
8
|
|
12
|
-
|
13
|
-
|
9
|
+
class Ibrain::Ability
|
10
|
+
include CanCan::Ability
|
14
11
|
|
15
|
-
|
12
|
+
class_attribute :abilities
|
13
|
+
self.abilities = Set.new
|
16
14
|
|
17
|
-
|
18
|
-
# modify the default +Ability+ of an application. The +ability+ argument must be a class that includes
|
19
|
-
# the +CanCan::Ability+ module. The registered ability should behave properly as a stand-alone class
|
20
|
-
# and therefore should be easy to test in isolation.
|
21
|
-
def self.register_ability(ability)
|
22
|
-
abilities.add(ability)
|
23
|
-
end
|
15
|
+
attr_reader :user
|
24
16
|
|
25
|
-
|
26
|
-
|
27
|
-
|
17
|
+
# Allows us to go beyond the standard cancan initialize method which makes it difficult for engines to
|
18
|
+
# modify the default +Ability+ of an application. The +ability+ argument must be a class that includes
|
19
|
+
# the +CanCan::Ability+ module. The registered ability should behave properly as a stand-alone class
|
20
|
+
# and therefore should be easy to test in isolation.
|
21
|
+
def self.register_ability(ability)
|
22
|
+
abilities.add(ability)
|
23
|
+
end
|
28
24
|
|
29
|
-
|
30
|
-
|
25
|
+
def self.remove_ability(ability)
|
26
|
+
abilities.delete(ability)
|
27
|
+
end
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
end
|
29
|
+
def initialize(current_user)
|
30
|
+
@user = current_user || Ibrain.user_class.new
|
35
31
|
|
36
|
-
|
32
|
+
activate_permission_sets
|
33
|
+
register_extension_abilities
|
34
|
+
end
|
37
35
|
|
38
|
-
|
39
|
-
# It is recommended to use them instead for extension purposes if possible.
|
40
|
-
def register_extension_abilities
|
41
|
-
Ability.abilities.each do |clazz|
|
42
|
-
ability = clazz.send(:new, user)
|
43
|
-
merge(ability)
|
44
|
-
end
|
45
|
-
end
|
36
|
+
private
|
46
37
|
|
47
|
-
|
48
|
-
|
38
|
+
# Before, this was the only way to extend this ability. Permission sets have been added since.
|
39
|
+
# It is recommended to use them instead for extension purposes if possible.
|
40
|
+
def register_extension_abilities
|
41
|
+
Ability.abilities.each do |clazz|
|
42
|
+
ability = clazz.send(:new, user)
|
43
|
+
merge(ability)
|
49
44
|
end
|
50
45
|
end
|
46
|
+
|
47
|
+
def activate_permission_sets
|
48
|
+
Ibrain::Config.roles.activate_permissions! self, user
|
49
|
+
end
|
51
50
|
end
|
@@ -1,19 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
# @note This class is intended to be modified by extensions (ex.
|
7
|
-
# ibrain-auth)
|
8
|
-
class LegacyUser < Ibrain::Base
|
9
|
-
include Ibrain::UserMethods
|
3
|
+
class Ibrain::LegacyUser < Ibrain::Base
|
4
|
+
include Ibrain::UserMethods
|
10
5
|
|
11
|
-
|
6
|
+
self.table_name = 'ibrain_users'
|
12
7
|
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
attr_accessor :password, :password_confirmation
|
8
|
+
def self.model_name
|
9
|
+
ActiveModel::Name.new Ibrain::LegacyUser, Ibrain, 'user'
|
18
10
|
end
|
11
|
+
|
12
|
+
attr_accessor :password, :password_confirmation
|
19
13
|
end
|
data/app/models/ibrain/role.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
has_many :users, through: :role_users
|
3
|
+
class Ibrain::Role < Ibrain::Base
|
4
|
+
has_many :role_users, class_name: "Ibrain::RoleUser", dependent: :destroy
|
5
|
+
has_many :users, through: :role_users
|
7
6
|
|
8
|
-
|
7
|
+
validates :name, uniqueness: { case_sensitive: true }
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
9
|
+
def admin?
|
10
|
+
name == "admin"
|
13
11
|
end
|
14
12
|
end
|
@@ -1,18 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
belongs_to :user, class_name: Ibrain::UserClassHandle.new, optional: true
|
3
|
+
class Ibrain::RoleUser < Ibrain::Base
|
4
|
+
belongs_to :role, class_name: "Ibrain::Role", optional: true
|
5
|
+
belongs_to :user, class_name: Ibrain::UserClassHandle.new, optional: true
|
7
6
|
|
8
|
-
|
7
|
+
after_create :auto_generate_ibrain_api_key
|
9
8
|
|
10
|
-
|
9
|
+
validates :role_id, uniqueness: { scope: :user_id }
|
11
10
|
|
12
|
-
|
11
|
+
private
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
13
|
+
def auto_generate_ibrain_api_key
|
14
|
+
user.try(:auto_generate_ibrain_api_key)
|
17
15
|
end
|
18
16
|
end
|
@@ -1,18 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
3
|
+
class Ibrain::BaseRepository
|
4
|
+
def initialize(current_user, record)
|
5
|
+
@current_user = current_user
|
6
|
+
@record = record
|
7
|
+
end
|
9
8
|
|
10
|
-
|
9
|
+
attr_reader :current_user, :record
|
11
10
|
|
12
|
-
|
11
|
+
protected
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
13
|
+
def cryptor
|
14
|
+
Ibrain::Encryptor.new
|
17
15
|
end
|
18
16
|
end
|
@@ -6,33 +6,29 @@ require 'rails/generators/base'
|
|
6
6
|
require 'active_support'
|
7
7
|
require 'active_support/core_ext/string/inflections'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
desc "Create a model by name"
|
13
|
-
source_root File.expand_path('templates', __dir__)
|
9
|
+
class Ibrain::Core::ModelGenerator < Rails::Generators::Base
|
10
|
+
desc "Create a model by name"
|
11
|
+
source_root File.expand_path('templates', __dir__)
|
14
12
|
|
15
|
-
|
13
|
+
argument :name, type: :string
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
def initialize(args, *options) # :nodoc:
|
16
|
+
# Unfreeze name in case it's given as a frozen string
|
17
|
+
args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
|
18
|
+
super
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
assign_names!(name)
|
21
|
+
end
|
24
22
|
|
25
|
-
|
23
|
+
attr_reader :file_name
|
26
24
|
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
def create_model_file
|
26
|
+
template "model.erb", "app/models/#{file_name}.rb"
|
27
|
+
end
|
30
28
|
|
31
|
-
|
29
|
+
private
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
31
|
+
def assign_names!(name)
|
32
|
+
@file_name = name.underscore
|
37
33
|
end
|
38
34
|
end
|
@@ -2,78 +2,74 @@
|
|
2
2
|
|
3
3
|
require 'rails/generators/base'
|
4
4
|
|
5
|
-
module Ibrain
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
)
|
16
|
-
end
|
5
|
+
module Ibrain::Graphql::Core
|
6
|
+
def self.included(base)
|
7
|
+
base.send(
|
8
|
+
:class_option,
|
9
|
+
:directory,
|
10
|
+
type: :string,
|
11
|
+
default: "app/graphql",
|
12
|
+
desc: "Directory where generated files should be saved"
|
13
|
+
)
|
14
|
+
end
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
def insert_root_type(type, name)
|
17
|
+
log :add_root_type, type
|
18
|
+
sentinel = /< GraphQL::Schema\s*\n/m
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
20
|
+
in_root do
|
21
|
+
if File.exist?(schema_file_path)
|
22
|
+
inject_into_file schema_file_path, " #{type}(Types::#{name})\n", after: sentinel, verbose: false, force: false
|
27
23
|
end
|
24
|
+
end
|
25
|
+
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
def create_mutation_root_type
|
28
|
+
create_dir("#{options[:directory]}/mutations")
|
29
|
+
insert_root_type('mutation', 'MutationType')
|
30
|
+
end
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
def create_resolver_root_type
|
33
|
+
create_dir("#{options[:directory]}/resolvers")
|
34
|
+
insert_root_type('query', 'QueryType')
|
35
|
+
end
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
def create_repository_root_type
|
38
|
+
create_dir("app/repositories")
|
39
|
+
end
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
def schema_file_path
|
42
|
+
"#{options[:directory]}/#{schema_name.underscore}.rb"
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
def create_dir(dir)
|
46
|
+
empty_directory(dir)
|
47
|
+
if !options[:skip_keeps]
|
48
|
+
create_file("#{dir}/.keep")
|
49
|
+
end
|
50
|
+
end
|
53
51
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
52
|
+
def module_namespacing_when_supported(&block)
|
53
|
+
if defined?(module_namespacing)
|
54
|
+
module_namespacing(&block)
|
55
|
+
else
|
56
|
+
yield
|
57
|
+
end
|
58
|
+
end
|
61
59
|
|
62
|
-
|
60
|
+
private
|
63
61
|
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
def schema_name
|
63
|
+
@schema_name ||= options[:schema] || "#{parent_name}Schema"
|
64
|
+
end
|
67
65
|
|
68
|
-
|
69
|
-
|
66
|
+
def parent_name
|
67
|
+
require File.expand_path("config/application", destination_root)
|
70
68
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
76
|
-
end
|
69
|
+
if Rails.application.class.respond_to?(:module_parent_name)
|
70
|
+
Rails.application.class.module_parent_name
|
71
|
+
else
|
72
|
+
Rails.application.class.parent_name
|
77
73
|
end
|
78
74
|
end
|
79
75
|
end
|
@@ -4,71 +4,63 @@ require 'rails/generators'
|
|
4
4
|
require 'rails/generators/named_base'
|
5
5
|
require_relative 'core'
|
6
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
|
-
def create_mutation_file
|
34
|
-
if @behavior == :revoke
|
35
|
-
log :gsub, "#{options[:directory]}/types/mutation_type.rb"
|
36
|
-
else
|
37
|
-
create_mutation_root_type
|
38
|
-
end
|
39
|
-
|
40
|
-
if options[:model].present?
|
41
|
-
system("bundle exec rails generate ibrain:graphql:object #{options[:model].underscore}")
|
42
|
-
end
|
43
|
-
|
44
|
-
template "mutation.erb", "#{options[:directory]}/mutations/#{file_name}.rb"
|
45
|
-
return unless ::Ibrain::Config.is_auto_append_mutation
|
46
|
-
|
47
|
-
in_root do
|
48
|
-
gsub_file "#{options[:directory]}/types/mutation_type.rb", / \# TODO: Add Mutations as fields\s*\n/m, ""
|
49
|
-
inject_into_file "#{options[:directory]}/types/mutation_type.rb", "\n field :#{field_name}, mutation: Mutations::#{mutation_name} \n ", before: "end\nend", verbose: true, force: true
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def assign_names!(name)
|
56
|
-
underscore_name = name&.camelize&.underscore
|
57
|
-
prefix = options[:prefix].try(:underscore)
|
58
|
-
@model_name = options[:model].blank? ? 'Post' : options[:model].try(:camelize, :upper)
|
59
|
-
|
60
|
-
if prefix.blank?
|
61
|
-
@mutation_name = name.camelize(:upper)
|
62
|
-
@file_name = underscore_name
|
63
|
-
@field_name = underscore_name
|
64
|
-
|
65
|
-
return
|
66
|
-
end
|
67
|
-
|
68
|
-
@mutation_name = "#{prefix.try(:camelize, :upper)}::#{name.camelize(:upper)}"
|
69
|
-
@file_name = "#{prefix}/#{underscore_name}"
|
70
|
-
@field_name = "#{prefix}_#{underscore_name}"
|
71
|
-
end
|
7
|
+
class Ibrain::Graphql::MutationGenerator < Rails::Generators::Base
|
8
|
+
include Core
|
9
|
+
|
10
|
+
desc "Create a Relay Classic mutation by name"
|
11
|
+
source_root File.expand_path('templates', __dir__)
|
12
|
+
|
13
|
+
argument :name, type: :string
|
14
|
+
class_option :model, type: :string, default: nil
|
15
|
+
class_option :prefix, type: :string, default: nil
|
16
|
+
|
17
|
+
def initialize(args, *options) # :nodoc:
|
18
|
+
# Unfreeze name in case it's given as a frozen string
|
19
|
+
args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
|
20
|
+
super
|
21
|
+
|
22
|
+
assign_names!(name)
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :file_name, :mutation_name, :field_name, :model_name
|
26
|
+
|
27
|
+
def create_mutation_file
|
28
|
+
if @behavior == :revoke
|
29
|
+
log :gsub, "#{options[:directory]}/types/mutation_type.rb"
|
30
|
+
else
|
31
|
+
create_mutation_root_type
|
72
32
|
end
|
33
|
+
|
34
|
+
if options[:model].present?
|
35
|
+
system("bundle exec rails generate ibrain:graphql:object #{options[:model].underscore}")
|
36
|
+
end
|
37
|
+
|
38
|
+
template "mutation.erb", "#{options[:directory]}/mutations/#{file_name}.rb"
|
39
|
+
return unless ::Ibrain::Config.is_auto_append_mutation
|
40
|
+
|
41
|
+
in_root do
|
42
|
+
gsub_file "#{options[:directory]}/types/mutation_type.rb", / \# TODO: Add Mutations as fields\s*\n/m, ""
|
43
|
+
inject_into_file "#{options[:directory]}/types/mutation_type.rb", "\n field :#{field_name}, mutation: Mutations::#{mutation_name} \n ", before: "end\nend", verbose: true, force: true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def assign_names!(name)
|
50
|
+
underscore_name = name&.camelize&.underscore
|
51
|
+
prefix = options[:prefix].try(:underscore)
|
52
|
+
@model_name = options[:model].blank? ? 'Post' : options[:model].try(:camelize, :upper)
|
53
|
+
|
54
|
+
if prefix.blank?
|
55
|
+
@mutation_name = name.camelize(:upper)
|
56
|
+
@file_name = underscore_name
|
57
|
+
@field_name = underscore_name
|
58
|
+
|
59
|
+
return
|
60
|
+
end
|
61
|
+
|
62
|
+
@mutation_name = "#{prefix.try(:camelize, :upper)}::#{name.camelize(:upper)}"
|
63
|
+
@file_name = "#{prefix}/#{underscore_name}"
|
64
|
+
@field_name = "#{prefix}_#{underscore_name}"
|
73
65
|
end
|
74
|
-
end
|
66
|
+
end
|