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.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/ibrain/base_controller.rb +12 -14
  3. data/app/controllers/ibrain/core/graphql_controller.rb +79 -83
  4. data/app/graphql/ibrain/base_schema.rb +50 -52
  5. data/app/graphql/ibrain/extentions/default_value.rb +7 -11
  6. data/app/graphql/ibrain/extentions/roles.rb +15 -20
  7. data/app/graphql/ibrain/extentions/session_required.rb +10 -14
  8. data/app/graphql/ibrain/lazy/base.rb +2 -6
  9. data/app/graphql/ibrain/loaders/association_loader.rb +51 -55
  10. data/app/graphql/ibrain/mutations/base_mutation.rb +52 -56
  11. data/app/graphql/ibrain/policies/base_policy.rb +43 -47
  12. data/app/graphql/ibrain/policies/graphql_policy.rb +2 -6
  13. data/app/graphql/ibrain/resolvers/base_aggregate.rb +6 -10
  14. data/app/graphql/ibrain/resolvers/base_resolver.rb +8 -12
  15. data/app/graphql/ibrain/types/aggregate_type.rb +4 -8
  16. data/app/graphql/ibrain/types/attribute_type.rb +2 -5
  17. data/app/graphql/ibrain/types/base_api_connection.rb +6 -10
  18. data/app/graphql/ibrain/types/base_api_edge.rb +4 -8
  19. data/app/graphql/ibrain/types/base_api_field.rb +7 -11
  20. data/app/graphql/ibrain/types/base_api_object.rb +6 -10
  21. data/app/graphql/ibrain/types/base_argument.rb +3 -7
  22. data/app/graphql/ibrain/types/base_connection.rb +7 -11
  23. data/app/graphql/ibrain/types/base_edge.rb +3 -7
  24. data/app/graphql/ibrain/types/base_enum.rb +1 -5
  25. data/app/graphql/ibrain/types/base_field.rb +6 -10
  26. data/app/graphql/ibrain/types/base_input_object.rb +6 -10
  27. data/app/graphql/ibrain/types/base_interface.rb +5 -9
  28. data/app/graphql/ibrain/types/base_object.rb +12 -16
  29. data/app/graphql/ibrain/types/base_scalar.rb +1 -5
  30. data/app/graphql/ibrain/types/base_type.rb +4 -10
  31. data/app/graphql/ibrain/types/base_union.rb +3 -7
  32. data/app/graphql/ibrain/types/filter_type.rb +1 -5
  33. data/app/graphql/ibrain/types/node_type.rb +4 -8
  34. data/app/graphql/ibrain/util/field_combiner.rb +5 -9
  35. data/app/graphql/ibrain/util/query_combiner.rb +4 -8
  36. data/app/models/concerns/ibrain/soft_deletable.rb +6 -8
  37. data/app/models/concerns/ibrain/user_api_authentication.rb +14 -16
  38. data/app/models/concerns/ibrain/user_methods.rb +11 -13
  39. data/app/models/ibrain/ability.rb +31 -32
  40. data/app/models/ibrain/aggregate.rb +5 -7
  41. data/app/models/ibrain/application_record.rb +2 -4
  42. data/app/models/ibrain/legacy_user.rb +7 -13
  43. data/app/models/ibrain/role.rb +6 -8
  44. data/app/models/ibrain/role_user.rb +8 -10
  45. data/app/repositories/ibrain/base_repository.rb +9 -11
  46. data/lib/generators/ibrain/core/model_generator.rb +17 -21
  47. data/lib/generators/ibrain/graphql/core.rb +55 -59
  48. data/lib/generators/ibrain/graphql/mutation_generator.rb +58 -66
  49. data/lib/generators/ibrain/graphql/object_generator.rb +58 -70
  50. data/lib/generators/ibrain/graphql/resolver_generator.rb +17 -25
  51. data/lib/generators/ibrain/graphql/resolvers_generator.rb +59 -67
  52. data/lib/generators/ibrain/graphql/templates/aggregate.erb +5 -7
  53. data/lib/generators/ibrain/graphql/templates/input.erb +5 -9
  54. data/lib/generators/ibrain/graphql/templates/mutation.erb +24 -26
  55. data/lib/generators/ibrain/graphql/templates/object.erb +6 -10
  56. data/lib/generators/ibrain/graphql/templates/resolver.erb +9 -11
  57. data/lib/generators/ibrain/graphql/templates/resolvers.erb +8 -10
  58. data/lib/generators/ibrain/graphql/type_generator.rb +84 -88
  59. data/lib/generators/ibrain/install/install_generator.rb +137 -140
  60. data/lib/generators/ibrain/install/templates/graphql/types/mutation_type.rb.tt +13 -15
  61. data/lib/generators/ibrain/install/templates/graphql/types/query_type.rb.tt +11 -13
  62. data/lib/generators/ibrain/install/templates/rubocop.yml.tt +83 -30
  63. data/lib/ibrain/app_configuration.rb +34 -36
  64. data/lib/ibrain/core/class_constantizer.rb +30 -33
  65. data/lib/ibrain/core/controller_helpers/auth.rb +53 -59
  66. data/lib/ibrain/core/controller_helpers/current_host.rb +5 -11
  67. data/lib/ibrain/core/controller_helpers/response.rb +43 -49
  68. data/lib/ibrain/core/controller_helpers/strong_parameters.rb +9 -15
  69. data/lib/ibrain/core/engine.rb +5 -9
  70. data/lib/ibrain/core/environment.rb +5 -9
  71. data/lib/ibrain/core/environment_extension.rb +13 -17
  72. data/lib/ibrain/core/role_configuration.rb +52 -54
  73. data/lib/ibrain/core/validators/email.rb +15 -17
  74. data/lib/ibrain/core/version.rb +2 -2
  75. data/lib/ibrain/core/versioned_value.rb +61 -65
  76. data/lib/ibrain/encryptor.rb +18 -20
  77. data/lib/ibrain/logger.rb +13 -15
  78. data/lib/ibrain/permission_sets/base.rb +22 -26
  79. data/lib/ibrain/permission_sets/super_user.rb +3 -7
  80. data/lib/ibrain/permitted_attributes.rb +18 -20
  81. data/lib/ibrain/preferences/configuration.rb +135 -137
  82. data/lib/ibrain/preferences/preferable.rb +158 -162
  83. data/lib/ibrain/preferences/preferable_class_methods.rb +112 -114
  84. data/lib/ibrain/user_class_handle.rb +22 -24
  85. metadata +2 -2
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
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
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
- module SoftDeletable
7
- extend ActiveSupport::Concern
5
+ module Ibrain::SoftDeletable
6
+ extend ActiveSupport::Concern
8
7
 
9
- included do
10
- include Discard::Model
11
- self.discard_column = :deleted_at
8
+ included do
9
+ include Discard::Model
10
+ self.discard_column = :deleted_at
12
11
 
13
- default_scope { kept }
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
- module UserApiAuthentication
5
- def generate_ibrain_api_key!
6
- generate_ibrain_api_key
7
- save!
8
- end
3
+ module Ibrain::UserApiAuthentication
4
+ def generate_ibrain_api_key!
5
+ generate_ibrain_api_key
6
+ save!
7
+ end
9
8
 
10
- def generate_ibrain_api_key
11
- self.ibrain_api_key = SecureRandom.hex(24)
12
- end
9
+ def generate_ibrain_api_key
10
+ self.ibrain_api_key = SecureRandom.hex(24)
11
+ end
13
12
 
14
- def clear_ibrain_api_key!
15
- clear_ibrain_api_key
16
- save!
17
- end
13
+ def clear_ibrain_api_key!
14
+ clear_ibrain_api_key
15
+ save!
16
+ end
18
17
 
19
- def clear_ibrain_api_key
20
- self.ibrain_api_key = nil
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
- module UserMethods
5
- extend ActiveSupport::Concern
3
+ module Ibrain::UserMethods
4
+ extend ActiveSupport::Concern
6
5
 
7
- include Ibrain::UserApiAuthentication
6
+ include Ibrain::UserApiAuthentication
8
7
 
9
- included do
10
- after_create :auto_generate_ibrain_api_key
8
+ included do
9
+ after_create :auto_generate_ibrain_api_key
11
10
 
12
- include Ibrain::RansackableAttributes unless included_modules.include?(Ibrain::RansackableAttributes)
13
- end
11
+ include Ibrain::RansackableAttributes unless included_modules.include?(Ibrain::RansackableAttributes)
12
+ end
14
13
 
15
- def auto_generate_ibrain_api_key
16
- return if !respond_to?(:ibrain_api_key) || ibrain_api_key.present?
14
+ def auto_generate_ibrain_api_key
15
+ return if !respond_to?(:ibrain_api_key) || ibrain_api_key.present?
17
16
 
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
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
- class_attribute :abilities
13
- self.abilities = Set.new
9
+ class Ibrain::Ability
10
+ include CanCan::Ability
14
11
 
15
- attr_reader :user
12
+ class_attribute :abilities
13
+ self.abilities = Set.new
16
14
 
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
15
+ attr_reader :user
24
16
 
25
- def self.remove_ability(ability)
26
- abilities.delete(ability)
27
- 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
28
24
 
29
- def initialize(current_user)
30
- @user = current_user || Ibrain.user_class.new
25
+ def self.remove_ability(ability)
26
+ abilities.delete(ability)
27
+ end
31
28
 
32
- activate_permission_sets
33
- register_extension_abilities
34
- end
29
+ def initialize(current_user)
30
+ @user = current_user || Ibrain.user_class.new
35
31
 
36
- private
32
+ activate_permission_sets
33
+ register_extension_abilities
34
+ end
37
35
 
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
45
- end
36
+ private
46
37
 
47
- def activate_permission_sets
48
- Ibrain::Config.roles.activate_permissions! self, user
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,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Ibrain
4
- class Aggregate
5
- def initialize(count)
6
- @count = count
7
- end
8
-
9
- attr_reader :count
3
+ class Ibrain::Aggregate
4
+ def initialize(count)
5
+ @count = count
10
6
  end
7
+
8
+ attr_reader :count
11
9
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Ibrain
4
- class ApplicationRecord < ActiveRecord::Base
5
- self.abstract_class = true
6
- end
3
+ class Ibrain::ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
7
5
  end
@@ -1,19 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
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
3
+ class Ibrain::LegacyUser < Ibrain::Base
4
+ include Ibrain::UserMethods
10
5
 
11
- self.table_name = 'ibrain_users'
6
+ self.table_name = 'ibrain_users'
12
7
 
13
- def self.model_name
14
- ActiveModel::Name.new Ibrain::LegacyUser, Ibrain, 'user'
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
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
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
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
- validates :name, uniqueness: { case_sensitive: true }
7
+ validates :name, uniqueness: { case_sensitive: true }
9
8
 
10
- def admin?
11
- name == "admin"
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
- 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
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
- after_create :auto_generate_ibrain_api_key
7
+ after_create :auto_generate_ibrain_api_key
9
8
 
10
- validates :role_id, uniqueness: { scope: :user_id }
9
+ validates :role_id, uniqueness: { scope: :user_id }
11
10
 
12
- private
11
+ private
13
12
 
14
- def auto_generate_ibrain_api_key
15
- user.try(:auto_generate_ibrain_api_key)
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
- module Ibrain
4
- class BaseRepository
5
- def initialize(current_user, record)
6
- @current_user = current_user
7
- @record = record
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
- attr_reader :current_user, :record
9
+ attr_reader :current_user, :record
11
10
 
12
- protected
11
+ protected
13
12
 
14
- def cryptor
15
- Ibrain::Encryptor.new
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
- 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__)
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
- argument :name, type: :string
13
+ argument :name, type: :string
16
14
 
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
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
- assign_names!(name)
23
- end
20
+ assign_names!(name)
21
+ end
24
22
 
25
- attr_reader :file_name
23
+ attr_reader :file_name
26
24
 
27
- def create_model_file
28
- template "model.erb", "app/models/#{file_name}.rb"
29
- end
25
+ def create_model_file
26
+ template "model.erb", "app/models/#{file_name}.rb"
27
+ end
30
28
 
31
- private
29
+ private
32
30
 
33
- def assign_names!(name)
34
- @file_name = name.underscore
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
- 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
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
- def insert_root_type(type, name)
19
- log :add_root_type, type
20
- sentinel = /< GraphQL::Schema\s*\n/m
16
+ def insert_root_type(type, name)
17
+ log :add_root_type, type
18
+ sentinel = /< GraphQL::Schema\s*\n/m
21
19
 
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
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
- def create_mutation_root_type
30
- create_dir("#{options[:directory]}/mutations")
31
- insert_root_type('mutation', 'MutationType')
32
- end
27
+ def create_mutation_root_type
28
+ create_dir("#{options[:directory]}/mutations")
29
+ insert_root_type('mutation', 'MutationType')
30
+ end
33
31
 
34
- def create_resolver_root_type
35
- create_dir("#{options[:directory]}/resolvers")
36
- insert_root_type('query', 'QueryType')
37
- end
32
+ def create_resolver_root_type
33
+ create_dir("#{options[:directory]}/resolvers")
34
+ insert_root_type('query', 'QueryType')
35
+ end
38
36
 
39
- def create_repository_root_type
40
- create_dir("app/repositories")
41
- end
37
+ def create_repository_root_type
38
+ create_dir("app/repositories")
39
+ end
42
40
 
43
- def schema_file_path
44
- "#{options[:directory]}/#{schema_name.underscore}.rb"
45
- end
41
+ def schema_file_path
42
+ "#{options[:directory]}/#{schema_name.underscore}.rb"
43
+ end
46
44
 
47
- def create_dir(dir)
48
- empty_directory(dir)
49
- if !options[:skip_keeps]
50
- create_file("#{dir}/.keep")
51
- end
52
- end
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
- def module_namespacing_when_supported(&block)
55
- if defined?(module_namespacing)
56
- module_namespacing(&block)
57
- else
58
- yield
59
- end
60
- end
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
- private
60
+ private
63
61
 
64
- def schema_name
65
- @schema_name ||= options[:schema] || "#{parent_name}Schema"
66
- end
62
+ def schema_name
63
+ @schema_name ||= options[:schema] || "#{parent_name}Schema"
64
+ end
67
65
 
68
- def parent_name
69
- require File.expand_path("config/application", destination_root)
66
+ def parent_name
67
+ require File.expand_path("config/application", destination_root)
70
68
 
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
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
- 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
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