ibrain-core 0.4.6 → 0.4.7
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 +4 -4
- data/app/controllers/ibrain/base_controller.rb +14 -12
- data/app/controllers/ibrain/core/graphql_controller.rb +83 -79
- data/app/graphql/ibrain/base_schema.rb +52 -50
- data/app/graphql/ibrain/extentions/default_value.rb +11 -7
- data/app/graphql/ibrain/extentions/roles.rb +20 -15
- data/app/graphql/ibrain/extentions/session_required.rb +14 -10
- data/app/graphql/ibrain/lazy/base.rb +6 -2
- data/app/graphql/ibrain/loaders/association_loader.rb +55 -51
- data/app/graphql/ibrain/mutations/base_mutation.rb +56 -52
- data/app/graphql/ibrain/policies/base_policy.rb +47 -43
- data/app/graphql/ibrain/policies/graphql_policy.rb +6 -2
- data/app/graphql/ibrain/resolvers/base_aggregate.rb +10 -6
- data/app/graphql/ibrain/resolvers/base_resolver.rb +12 -8
- data/app/graphql/ibrain/types/aggregate_type.rb +8 -4
- data/app/graphql/ibrain/types/attribute_type.rb +5 -2
- data/app/graphql/ibrain/types/base_api_connection.rb +10 -6
- data/app/graphql/ibrain/types/base_api_edge.rb +8 -4
- data/app/graphql/ibrain/types/base_api_field.rb +11 -7
- data/app/graphql/ibrain/types/base_api_object.rb +10 -6
- data/app/graphql/ibrain/types/base_argument.rb +7 -3
- data/app/graphql/ibrain/types/base_connection.rb +11 -7
- data/app/graphql/ibrain/types/base_edge.rb +7 -3
- data/app/graphql/ibrain/types/base_enum.rb +5 -1
- data/app/graphql/ibrain/types/base_field.rb +10 -6
- data/app/graphql/ibrain/types/base_input_object.rb +10 -6
- data/app/graphql/ibrain/types/base_interface.rb +9 -5
- data/app/graphql/ibrain/types/base_object.rb +16 -12
- data/app/graphql/ibrain/types/base_scalar.rb +5 -1
- data/app/graphql/ibrain/types/base_type.rb +10 -4
- data/app/graphql/ibrain/types/base_union.rb +7 -3
- data/app/graphql/ibrain/types/filter_type.rb +5 -1
- data/app/graphql/ibrain/types/node_type.rb +8 -4
- data/app/graphql/ibrain/util/field_combiner.rb +9 -5
- data/app/graphql/ibrain/util/query_combiner.rb +8 -4
- data/app/models/concerns/ibrain/soft_deletable.rb +8 -6
- data/app/models/concerns/ibrain/user_api_authentication.rb +16 -14
- data/app/models/concerns/ibrain/user_methods.rb +13 -11
- data/app/models/ibrain/ability.rb +32 -31
- data/app/models/ibrain/aggregate.rb +7 -5
- data/app/models/ibrain/application_record.rb +4 -2
- data/app/models/ibrain/legacy_user.rb +13 -7
- data/app/models/ibrain/role.rb +8 -6
- data/app/models/ibrain/role_user.rb +10 -8
- data/app/repositories/ibrain/base_repository.rb +11 -9
- data/lib/generators/ibrain/core/model_generator.rb +21 -17
- data/lib/generators/ibrain/graphql/core.rb +59 -55
- data/lib/generators/ibrain/graphql/mutation_generator.rb +66 -58
- data/lib/generators/ibrain/graphql/object_generator.rb +70 -58
- data/lib/generators/ibrain/graphql/resolver_generator.rb +25 -17
- data/lib/generators/ibrain/graphql/resolvers_generator.rb +67 -59
- data/lib/generators/ibrain/graphql/templates/aggregate.erb +7 -5
- data/lib/generators/ibrain/graphql/templates/input.erb +9 -5
- data/lib/generators/ibrain/graphql/templates/mutation.erb +26 -24
- data/lib/generators/ibrain/graphql/templates/object.erb +10 -6
- data/lib/generators/ibrain/graphql/templates/resolver.erb +11 -9
- data/lib/generators/ibrain/graphql/templates/resolvers.erb +10 -8
- data/lib/generators/ibrain/graphql/type_generator.rb +88 -84
- data/lib/generators/ibrain/install/install_generator.rb +140 -137
- data/lib/generators/ibrain/install/templates/graphql/types/mutation_type.rb.tt +15 -13
- data/lib/generators/ibrain/install/templates/graphql/types/query_type.rb.tt +13 -11
- data/lib/generators/ibrain/install/templates/rubocop.yml.tt +30 -83
- data/lib/ibrain/app_configuration.rb +36 -34
- data/lib/ibrain/core/class_constantizer.rb +33 -30
- data/lib/ibrain/core/controller_helpers/auth.rb +59 -53
- data/lib/ibrain/core/controller_helpers/current_host.rb +11 -5
- data/lib/ibrain/core/controller_helpers/response.rb +49 -43
- data/lib/ibrain/core/controller_helpers/strong_parameters.rb +15 -9
- data/lib/ibrain/core/engine.rb +9 -5
- data/lib/ibrain/core/environment.rb +9 -5
- data/lib/ibrain/core/environment_extension.rb +17 -13
- data/lib/ibrain/core/role_configuration.rb +54 -52
- data/lib/ibrain/core/validators/email.rb +17 -15
- data/lib/ibrain/core/version.rb +2 -2
- data/lib/ibrain/core/versioned_value.rb +65 -61
- data/lib/ibrain/encryptor.rb +20 -18
- data/lib/ibrain/logger.rb +15 -13
- data/lib/ibrain/permission_sets/base.rb +26 -22
- data/lib/ibrain/permission_sets/super_user.rb +7 -3
- data/lib/ibrain/permitted_attributes.rb +20 -18
- data/lib/ibrain/preferences/configuration.rb +137 -135
- data/lib/ibrain/preferences/preferable.rb +162 -158
- data/lib/ibrain/preferences/preferable_class_methods.rb +114 -112
- data/lib/ibrain/user_class_handle.rb +24 -22
- data/lib/ibrain_core.rb +1 -7
- metadata +1 -1
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
module Util
|
|
5
|
+
class QueryCombiner
|
|
6
|
+
def self.combine(query_types)
|
|
7
|
+
Array(query_types).inject({}) do |acc, query_type|
|
|
8
|
+
acc.merge!(query_type.fields)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
7
11
|
end
|
|
8
12
|
end
|
|
9
13
|
end
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
require 'discard'
|
|
4
4
|
|
|
5
|
-
module Ibrain
|
|
6
|
-
|
|
5
|
+
module Ibrain
|
|
6
|
+
module SoftDeletable
|
|
7
|
+
extend ActiveSupport::Concern
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
included do
|
|
10
|
+
include Discard::Model
|
|
11
|
+
self.discard_column = :deleted_at
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
default_scope { kept }
|
|
14
|
+
end
|
|
13
15
|
end
|
|
14
16
|
end
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module Ibrain
|
|
4
|
-
|
|
5
|
-
generate_ibrain_api_key
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
module UserApiAuthentication
|
|
5
|
+
def generate_ibrain_api_key!
|
|
6
|
+
generate_ibrain_api_key
|
|
7
|
+
save!
|
|
8
|
+
end
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
def generate_ibrain_api_key
|
|
11
|
+
self.ibrain_api_key = SecureRandom.hex(24)
|
|
12
|
+
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
def clear_ibrain_api_key!
|
|
15
|
+
clear_ibrain_api_key
|
|
16
|
+
save!
|
|
17
|
+
end
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
def clear_ibrain_api_key
|
|
20
|
+
self.ibrain_api_key = nil
|
|
21
|
+
end
|
|
20
22
|
end
|
|
21
23
|
end
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module Ibrain
|
|
4
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
module UserMethods
|
|
5
|
+
extend ActiveSupport::Concern
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
include Ibrain::UserApiAuthentication
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
included do
|
|
10
|
+
after_create :auto_generate_ibrain_api_key
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
include Ibrain::RansackableAttributes unless included_modules.include?(Ibrain::RansackableAttributes)
|
|
13
|
+
end
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
def auto_generate_ibrain_api_key
|
|
16
|
+
return if !respond_to?(:ibrain_api_key) || ibrain_api_key.present?
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
if Ibrain::Config.generate_api_key_for_all_roles || (ibrain_roles.map(&:name) & Ibrain::Config.roles_for_auto_api_key).any?
|
|
19
|
+
generate_ibrain_api_key!
|
|
20
|
+
end
|
|
19
21
|
end
|
|
20
22
|
end
|
|
21
23
|
end
|
|
@@ -5,46 +5,47 @@
|
|
|
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
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
class_attribute :abilities
|
|
13
|
+
self.abilities = Set.new
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
self.abilities = Set.new
|
|
15
|
+
attr_reader :user
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
abilities.add(ability)
|
|
23
|
-
end
|
|
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
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
def self.remove_ability(ability)
|
|
26
|
+
abilities.delete(ability)
|
|
27
|
+
end
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
def initialize(current_user)
|
|
30
|
+
@user = current_user || Ibrain.user_class.new
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
activate_permission_sets
|
|
33
|
+
register_extension_abilities
|
|
34
|
+
end
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
private
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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)
|
|
44
|
+
end
|
|
44
45
|
end
|
|
45
|
-
end
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
def activate_permission_sets
|
|
48
|
+
Ibrain::Config.roles.activate_permissions! self, user
|
|
49
|
+
end
|
|
49
50
|
end
|
|
50
51
|
end
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
# Default implementation of User.
|
|
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
|
|
5
10
|
|
|
6
|
-
|
|
11
|
+
self.table_name = 'ibrain_users'
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
def self.model_name
|
|
14
|
+
ActiveModel::Name.new Ibrain::LegacyUser, Ibrain, 'user'
|
|
15
|
+
end
|
|
11
16
|
|
|
12
|
-
|
|
17
|
+
attr_accessor :password, :password_confirmation
|
|
18
|
+
end
|
|
13
19
|
end
|
data/app/models/ibrain/role.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
class Role < Ibrain::Base
|
|
5
|
+
has_many :role_users, class_name: "Ibrain::RoleUser", dependent: :destroy
|
|
6
|
+
has_many :users, through: :role_users
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
validates :name, uniqueness: { case_sensitive: true }
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
def admin?
|
|
11
|
+
name == "admin"
|
|
12
|
+
end
|
|
11
13
|
end
|
|
12
14
|
end
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
class RoleUser < Ibrain::Base
|
|
5
|
+
belongs_to :role, class_name: "Ibrain::Role", optional: true
|
|
6
|
+
belongs_to :user, class_name: Ibrain::UserClassHandle.new, optional: true
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
after_create :auto_generate_ibrain_api_key
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
validates :role_id, uniqueness: { scope: :user_id }
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
private
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
def auto_generate_ibrain_api_key
|
|
15
|
+
user.try(:auto_generate_ibrain_api_key)
|
|
16
|
+
end
|
|
15
17
|
end
|
|
16
18
|
end
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
class BaseRepository
|
|
5
|
+
def initialize(current_user, record)
|
|
6
|
+
@current_user = current_user
|
|
7
|
+
@record = record
|
|
8
|
+
end
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
attr_reader :current_user, :record
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
protected
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
def cryptor
|
|
15
|
+
Ibrain::Encryptor.new
|
|
16
|
+
end
|
|
15
17
|
end
|
|
16
18
|
end
|
|
@@ -6,29 +6,33 @@ require 'rails/generators/base'
|
|
|
6
6
|
require 'active_support'
|
|
7
7
|
require 'active_support/core_ext/string/inflections'
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
module Ibrain
|
|
10
|
+
module Core
|
|
11
|
+
class ModelGenerator < Rails::Generators::Base
|
|
12
|
+
desc "Create a model by name"
|
|
13
|
+
source_root File.expand_path('templates', __dir__)
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
argument :name, type: :string
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
assign_names!(name)
|
|
23
|
+
end
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
attr_reader :file_name
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
def create_model_file
|
|
28
|
+
template "model.erb", "app/models/#{file_name}.rb"
|
|
29
|
+
end
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
private
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
def assign_names!(name)
|
|
34
|
+
@file_name = name.underscore
|
|
35
|
+
end
|
|
36
|
+
end
|
|
33
37
|
end
|
|
34
38
|
end
|
|
@@ -2,74 +2,78 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rails/generators/base'
|
|
4
4
|
|
|
5
|
-
module Ibrain
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
module Ibrain
|
|
6
|
+
module Graphql
|
|
7
|
+
module Core
|
|
8
|
+
def self.included(base)
|
|
9
|
+
base.send(
|
|
10
|
+
:class_option,
|
|
11
|
+
:directory,
|
|
12
|
+
type: :string,
|
|
13
|
+
default: "app/graphql",
|
|
14
|
+
desc: "Directory where generated files should be saved"
|
|
15
|
+
)
|
|
16
|
+
end
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
def insert_root_type(type, name)
|
|
19
|
+
log :add_root_type, type
|
|
20
|
+
sentinel = /< GraphQL::Schema\s*\n/m
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
in_root do
|
|
23
|
+
if File.exist?(schema_file_path)
|
|
24
|
+
inject_into_file schema_file_path, " #{type}(Types::#{name})\n", after: sentinel, verbose: false, force: false
|
|
25
|
+
end
|
|
26
|
+
end
|
|
23
27
|
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
def create_mutation_root_type
|
|
30
|
+
create_dir("#{options[:directory]}/mutations")
|
|
31
|
+
insert_root_type('mutation', 'MutationType')
|
|
32
|
+
end
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
def create_resolver_root_type
|
|
35
|
+
create_dir("#{options[:directory]}/resolvers")
|
|
36
|
+
insert_root_type('query', 'QueryType')
|
|
37
|
+
end
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
def create_repository_root_type
|
|
40
|
+
create_dir("app/repositories")
|
|
41
|
+
end
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
def schema_file_path
|
|
44
|
+
"#{options[:directory]}/#{schema_name.underscore}.rb"
|
|
45
|
+
end
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
def create_dir(dir)
|
|
48
|
+
empty_directory(dir)
|
|
49
|
+
if !options[:skip_keeps]
|
|
50
|
+
create_file("#{dir}/.keep")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
def module_namespacing_when_supported(&block)
|
|
55
|
+
if defined?(module_namespacing)
|
|
56
|
+
module_namespacing(&block)
|
|
57
|
+
else
|
|
58
|
+
yield
|
|
59
|
+
end
|
|
60
|
+
end
|
|
59
61
|
|
|
60
|
-
|
|
62
|
+
private
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
def schema_name
|
|
65
|
+
@schema_name ||= options[:schema] || "#{parent_name}Schema"
|
|
66
|
+
end
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
def parent_name
|
|
69
|
+
require File.expand_path("config/application", destination_root)
|
|
68
70
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
if Rails.application.class.respond_to?(:module_parent_name)
|
|
72
|
+
Rails.application.class.module_parent_name
|
|
73
|
+
else
|
|
74
|
+
Rails.application.class.parent_name
|
|
75
|
+
end
|
|
76
|
+
end
|
|
73
77
|
end
|
|
74
78
|
end
|
|
75
79
|
end
|
|
@@ -4,63 +4,71 @@ 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
|
-
|
|
7
|
+
module Ibrain
|
|
8
|
+
module Graphql
|
|
9
|
+
# TODO: What other options should be supported?
|
|
10
|
+
#
|
|
11
|
+
# @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
|
|
12
|
+
# rails g graphql:mutation CreatePostMutation
|
|
13
|
+
class MutationGenerator < Rails::Generators::Base
|
|
14
|
+
include Core
|
|
15
|
+
|
|
16
|
+
desc "Create a Relay Classic mutation by name"
|
|
17
|
+
source_root File.expand_path('templates', __dir__)
|
|
18
|
+
|
|
19
|
+
argument :name, type: :string
|
|
20
|
+
class_option :model, type: :string, default: nil
|
|
21
|
+
class_option :prefix, type: :string, default: nil
|
|
22
|
+
|
|
23
|
+
def initialize(args, *options) # :nodoc:
|
|
24
|
+
# Unfreeze name in case it's given as a frozen string
|
|
25
|
+
args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
|
|
26
|
+
super
|
|
27
|
+
|
|
28
|
+
assign_names!(name)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader :file_name, :mutation_name, :field_name, :model_name
|
|
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
|
|
32
72
|
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}"
|
|
65
73
|
end
|
|
66
|
-
end
|
|
74
|
+
end
|