ibrain-core 0.4.6 → 0.4.7

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