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.
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
@@ -6,12 +6,7 @@ require:
6
6
  - rubocop-graphql
7
7
 
8
8
  AllCops:
9
- Exclude:
10
- - db/migrate/*.rb
11
- - bin/*
12
- - config/application.rb
13
- - "**/vendor/**/*"
14
- - vendor/eco-api-ruby
9
+ TargetRubyVersion: 2.5
15
10
 
16
11
  # We use class vars and will have to continue doing so for compatability
17
12
  Style/ClassVars:
@@ -32,6 +27,9 @@ Naming/AccessorMethodName:
32
27
  Style/MutableConstant:
33
28
  Enabled: false
34
29
 
30
+ Style/ClassAndModuleChildren:
31
+ Enabled: false
32
+
35
33
  Style/EmptyElse:
36
34
  Enabled: false
37
35
 
@@ -51,7 +49,7 @@ Style/ConditionalAssignment:
51
49
  Enabled: false
52
50
 
53
51
  Performance/Count:
54
- Enabled: true
52
+ Enabled: false
55
53
 
56
54
  Style/RaiseArgs:
57
55
  Enabled: false
@@ -68,44 +66,44 @@ Style/AsciiComments:
68
66
  Enabled: false
69
67
 
70
68
  Layout/EndAlignment:
71
- Enabled: true
69
+ Enabled: false
72
70
 
73
71
  Layout/ElseAlignment:
74
- Enabled: true
72
+ Enabled: false
75
73
 
76
74
  Layout/IndentationWidth:
77
- Enabled: true
75
+ Enabled: false
78
76
 
79
77
  Layout/ParameterAlignment:
80
- Enabled: true
78
+ Enabled: false
81
79
 
82
80
  Layout/ClosingParenthesisIndentation:
83
- Enabled: true
81
+ Enabled: false
84
82
 
85
83
  Layout/MultilineMethodCallIndentation:
86
- Enabled: true
84
+ Enabled: false
87
85
 
88
86
  Layout/FirstArrayElementIndentation:
89
- Enabled: true
87
+ Enabled: false
90
88
 
91
89
  Layout/FirstHashElementIndentation:
92
- Enabled: true
90
+ Enabled: false
93
91
 
94
92
  Layout/HashAlignment:
95
- Enabled: true
93
+ Enabled: false
96
94
 
97
95
  Style/TrailingCommaInArguments:
98
- Enabled: true
96
+ Enabled: false
99
97
 
100
98
  Style/TrailingCommaInArrayLiteral:
101
- Enabled: true
99
+ Enabled: false
102
100
 
103
101
  Style/TrailingCommaInHashLiteral:
104
- Enabled: true
102
+ Enabled: false
105
103
 
106
104
  # Symbol Arrays are ok and the %i syntax widely unknown
107
105
  Style/SymbolArray:
108
- Enabled: true
106
+ Enabled: false
109
107
 
110
108
  Rails/DynamicFindBy:
111
109
  Whitelist:
@@ -116,7 +114,7 @@ Rails/DynamicFindBy:
116
114
  Rails/SkipsModelValidations:
117
115
  Exclude:
118
116
  - db/seeds/**/*.rb
119
- - "*/spec/**/*"
117
+ - '*/spec/**/*'
120
118
 
121
119
  # We use a lot of
122
120
  #
@@ -127,13 +125,13 @@ Rails/SkipsModelValidations:
127
125
  # syntax in the specs files.
128
126
  Lint/AmbiguousBlockAssociation:
129
127
  Exclude:
130
- - "*/spec/**/*"
131
- - "spec/**/*" # For the benefit of apps that inherit from this config
128
+ - '*/spec/**/*'
129
+ - 'spec/**/*' # For the benefit of apps that inherit from this config
132
130
 
133
131
  Security/Eval:
134
132
  Exclude:
135
- - "Gemfile"
136
- - "*/Gemfile"
133
+ - 'Gemfile'
134
+ - '*/Gemfile'
137
135
 
138
136
  Naming/VariableNumber:
139
137
  Enabled: false
@@ -237,7 +235,7 @@ Layout/SpaceInsideParens:
237
235
  StyleGuide: http://relaxed.ruby.style/#stylespaceinsideparens
238
236
 
239
237
  Style/SpecialGlobalVars:
240
- Enabled: true
238
+ Enabled: false
241
239
  StyleGuide: http://relaxed.ruby.style/#stylespecialglobalvars
242
240
 
243
241
  Style/StringLiterals:
@@ -302,9 +300,6 @@ Style/FrozenStringLiteralComment:
302
300
  Enabled: true
303
301
  EnforcedStyle: always
304
302
 
305
- Style/FetchEnvVar:
306
- Enabled: true
307
-
308
303
  # json.() is idiomatic in jbuilder files
309
304
  Style/LambdaCall:
310
305
  Enabled: false
@@ -320,7 +315,7 @@ Style/IdenticalConditionalBranches:
320
315
  Enabled: false
321
316
 
322
317
  Naming/MemoizedInstanceVariableName:
323
- Enabled: true
318
+ Enabled: false
324
319
 
325
320
  Lint/BinaryOperatorWithIdenticalOperands:
326
321
  Enabled: false
@@ -604,7 +599,10 @@ GraphQL/FieldDescription:
604
599
  Enabled: false
605
600
 
606
601
  GraphQL/ObjectDescription:
607
- Enabled: false
602
+ Enabled: true
603
+ Exclude:
604
+ - app/graphql/policies/*.rb
605
+ - app/graphql/*.rb
608
606
 
609
607
  GraphQL/ExtractType:
610
608
  Enabled: false
@@ -614,55 +612,4 @@ Rails/UnknownEnv:
614
612
  - production
615
613
  - development
616
614
  - test
617
- - staging
618
-
619
- Style/MixinUsage:
620
- Enabled: true
621
- Exclude:
622
- - db/seeds/*.rb
623
-
624
- GraphQL/ExtractInputType:
625
- Enabled: false
626
- GraphQL/ArgumentDescription:
627
- Enabled: false
628
-
629
- Style/AccessorGrouping:
630
- Enabled: true
631
-
632
- Layout/AccessModifierIndentation:
633
- Enabled: true
634
- EnforcedStyle: indent
635
-
636
- Layout/ArrayAlignment:
637
- Enabled: true
638
-
639
- Layout/ArgumentAlignment:
640
- Enabled: true
641
-
642
- Layout/ClassStructure:
643
- Categories:
644
- association:
645
- - has_many
646
- - has_one
647
- attribute_macros:
648
- - attr_accessor
649
- - attr_reader
650
- - attr_writer
651
- macros:
652
- - validates
653
- - validate
654
- module_inclusion:
655
- - include
656
- - prepend
657
- - extend
658
- Layout/IndentationConsistency:
659
- EnforcedStyle: indented_internal_methods
660
-
661
- Layout/InitialIndentation:
662
- Enabled: true
663
-
664
- Layout/IndentationStyle:
665
- EnforcedStyle: spaces
666
-
667
- Style/ClassAndModuleChildren:
668
- EnforcedStyle: compact
615
+ - staging
@@ -20,54 +20,56 @@
20
20
  require 'ibrain/preferences/configuration'
21
21
  require 'ibrain/core/environment'
22
22
 
23
- class Ibrain::AppConfiguration < Ibrain::Preferences::Configuration
24
- # Preferences (alphabetized to more easily lookup particular preferences)
23
+ module Ibrain
24
+ class AppConfiguration < Preferences::Configuration
25
+ # Preferences (alphabetized to more easily lookup particular preferences)
25
26
 
26
- # @!attribute [rw] guest_token_cookie_options
27
- # @return [Hash] Add additional guest_token cookie options here (ie. domain or path)
28
- preference :guest_token_cookie_options, :hash, default: {}
27
+ # @!attribute [rw] guest_token_cookie_options
28
+ # @return [Hash] Add additional guest_token cookie options here (ie. domain or path)
29
+ preference :guest_token_cookie_options, :hash, default: {}
29
30
 
30
- # @!attribute [rw] generate_api_key_for_all_roles
31
- # @return [Boolean] Allow generating api key automatically for user
32
- # at role_user creation for all roles. (default: +false+)
33
- preference :generate_api_key_for_all_roles, :boolean, default: false
31
+ # @!attribute [rw] generate_api_key_for_all_roles
32
+ # @return [Boolean] Allow generating api key automatically for user
33
+ # at role_user creation for all roles. (default: +false+)
34
+ preference :generate_api_key_for_all_roles, :boolean, default: false
34
35
 
35
- # @!attribute [rw] mails_from
36
- # @return [String] Email address used as +From:+ field in transactional emails.
37
- preference :mails_from, :string, default: 'ibrain@example.com'
36
+ # @!attribute [rw] mails_from
37
+ # @return [String] Email address used as +From:+ field in transactional emails.
38
+ preference :mails_from, :string, default: 'ibrain@example.com'
38
39
 
39
- preference :graphql_policy, :string, default: 'Ibrain::Policies::GraphqlPolicy'
40
+ preference :graphql_policy, :string, default: 'Ibrain::Policies::GraphqlPolicy'
40
41
 
41
- # Api version for route config
42
- preference :api_version, :string, default: 'v1'
42
+ # Api version for route config
43
+ preference :api_version, :string, default: 'v1'
43
44
 
44
- # Graphql Schema name
45
- preference :graphql_schema, :string, default: 'Ibrain::BaseSchema'
45
+ # Graphql Schema name
46
+ preference :graphql_schema, :string, default: 'Ibrain::BaseSchema'
46
47
 
47
- # Graphql Encryptor key
48
- preference :ibrain_encryptor_key, :string, default: nil
48
+ # Graphql Encryptor key
49
+ preference :ibrain_encryptor_key, :string, default: nil
49
50
 
50
- # Parent controller
51
- preference :parent_controller, :string, default: 'ActionController::API'
51
+ # Parent controller
52
+ preference :parent_controller, :string, default: 'ActionController::API'
52
53
 
53
- preference :master_database, :string, default: 'primary'
54
+ preference :master_database, :string, default: 'primary'
54
55
 
55
- preference :graphql_max_depth, :integer, default: 3
56
+ preference :graphql_max_depth, :integer, default: 3
56
57
 
57
- preference :is_auto_append_mutation, :boolean, default: true
58
+ preference :is_auto_append_mutation, :boolean, default: true
58
59
 
59
- def static_model_preferences
60
- @static_model_preferences ||= Ibrain::Preferences::StaticModelPreferences.new
61
- end
60
+ def static_model_preferences
61
+ @static_model_preferences ||= Ibrain::Preferences::StaticModelPreferences.new
62
+ end
62
63
 
63
- def roles
64
- @roles ||= Ibrain::RoleConfiguration.new.tap do |roles|
65
- roles.assign_permissions :default, ['Ibrain::PermissionSets::DefaultCustomer']
66
- roles.assign_permissions :admin, ['Ibrain::PermissionSets::SuperUser']
64
+ def roles
65
+ @roles ||= Ibrain::RoleConfiguration.new.tap do |roles|
66
+ roles.assign_permissions :default, ['Ibrain::PermissionSets::DefaultCustomer']
67
+ roles.assign_permissions :admin, ['Ibrain::PermissionSets::SuperUser']
68
+ end
67
69
  end
68
- end
69
70
 
70
- def environment
71
- @environment ||= Ibrain::Core::Environment.new(self)
71
+ def environment
72
+ @environment ||= Ibrain::Core::Environment.new(self)
73
+ end
72
74
  end
73
75
  end
@@ -2,37 +2,40 @@
2
2
 
3
3
  require 'active_support/core_ext/module'
4
4
 
5
- module Ibrain::Core::ClassConstantizer
6
- class Set
7
- include Enumerable
8
-
9
- def initialize
10
- @collection = ::Set.new
11
- end
12
-
13
- def <<(klass)
14
- @collection << klass.to_s
15
- end
16
-
17
- def concat(klasses)
18
- klasses.each do |klass|
19
- self << klass
20
- end
21
-
22
- self
23
- end
24
-
25
- delegate :clear, :empty?, to: :@collection
26
-
27
- def delete(object)
28
- @collection.delete(object.to_s)
29
- end
30
-
31
- def each
32
- @collection.each do |klass|
33
- yield klass.constantize
5
+ module Ibrain
6
+ module Core
7
+ module ClassConstantizer
8
+ class Set
9
+ include Enumerable
10
+
11
+ def initialize
12
+ @collection = ::Set.new
13
+ end
14
+
15
+ def <<(klass)
16
+ @collection << klass.to_s
17
+ end
18
+
19
+ def concat(klasses)
20
+ klasses.each do |klass|
21
+ self << klass
22
+ end
23
+
24
+ self
25
+ end
26
+
27
+ delegate :clear, :empty?, to: :@collection
28
+
29
+ def delete(object)
30
+ @collection.delete(object.to_s)
31
+ end
32
+
33
+ def each
34
+ @collection.each do |klass|
35
+ yield klass.constantize
36
+ end
37
+ end
34
38
  end
35
39
  end
36
40
  end
37
41
  end
38
-
@@ -1,62 +1,68 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Ibrain::Core::ControllerHelpers::Auth
4
- extend ActiveSupport::Concern
5
- include Response
6
-
7
- # @!attribute [rw] fallback_on_unauthorized
8
- # @!scope class
9
- # Extension point for overriding behaviour of access denied errors.
10
- # Default behaviour is to redirect back or to "/unauthorized" with a flash
11
- # message.
12
- # @return [Proc] action to take when access denied error is raised.
13
-
14
- included do
15
- before_action :set_guest_token
16
- helper_method :try_ibrain_current_user
17
-
18
- class_attribute :fallback_on_unauthorized
19
- self.fallback_on_unauthorized = -> do
20
- error = ::Struct.new(
21
- message: I18n.t('ibrain.authorization_failure')
22
- )
23
-
24
- render_json_error(error, :unauthorized)
25
- end
3
+ module Ibrain
4
+ module Core
5
+ module ControllerHelpers
6
+ module Auth
7
+ extend ActiveSupport::Concern
8
+ include Response
26
9
 
27
- rescue_from CanCan::AccessDenied do
28
- instance_exec(&fallback_on_unauthorized)
29
- end
30
- end
10
+ # @!attribute [rw] fallback_on_unauthorized
11
+ # @!scope class
12
+ # Extension point for overriding behaviour of access denied errors.
13
+ # Default behaviour is to redirect back or to "/unauthorized" with a flash
14
+ # message.
15
+ # @return [Proc] action to take when access denied error is raised.
31
16
 
32
- # Needs to be overriden so that we use Brain's Ability rather than anyone else's.
33
- def current_ability
34
- @current_ability ||= Ibrain::Ability.new(try_ibrain_current_user)
35
- end
17
+ included do
18
+ before_action :set_guest_token
19
+ helper_method :try_ibrain_current_user
36
20
 
37
- def set_guest_token
38
- # if cookies.signed[:guest_token].blank?
39
- # cookies.permanent.signed[:guest_token] = Ibrain::Config[:guest_token_cookie_options].merge(
40
- # value: SecureRandom.urlsafe_base64(nil, false),
41
- # httponly: true
42
- # )
43
- # end
44
- end
21
+ class_attribute :fallback_on_unauthorized
22
+ self.fallback_on_unauthorized = -> do
23
+ error = ::Struct.new(
24
+ message: I18n.t('ibrain.authorization_failure')
25
+ )
45
26
 
46
- # proxy method to *possible* ibrain_current_user method
47
- # Authentication extensions (such as ibrain-auth) are meant to provide ibrain_current_user
48
- def try_ibrain_current_user
49
- # This one will be defined by apps looking to hook into Ibrain
50
- # As per authentication_helpers.rb
51
- if respond_to?(:ibrain_current_user, true)
52
- try(:ibrain_current_user)
53
- # This one will be defined by Devise
54
- elsif respond_to?(:current_ibrain_user, true)
55
- try(:current_ibrain_user)
56
- end
57
- rescue StandardError => e
58
- Ibrain::Logger.warn e.message.to_s
27
+ render_json_error(error, :unauthorized)
28
+ end
59
29
 
60
- nil
30
+ rescue_from CanCan::AccessDenied do
31
+ instance_exec(&fallback_on_unauthorized)
32
+ end
33
+ end
34
+
35
+ # Needs to be overriden so that we use Brain's Ability rather than anyone else's.
36
+ def current_ability
37
+ @current_ability ||= Ibrain::Ability.new(try_ibrain_current_user)
38
+ end
39
+
40
+ def set_guest_token
41
+ # if cookies.signed[:guest_token].blank?
42
+ # cookies.permanent.signed[:guest_token] = Ibrain::Config[:guest_token_cookie_options].merge(
43
+ # value: SecureRandom.urlsafe_base64(nil, false),
44
+ # httponly: true
45
+ # )
46
+ # end
47
+ end
48
+
49
+ # proxy method to *possible* ibrain_current_user method
50
+ # Authentication extensions (such as ibrain-auth) are meant to provide ibrain_current_user
51
+ def try_ibrain_current_user
52
+ # This one will be defined by apps looking to hook into Ibrain
53
+ # As per authentication_helpers.rb
54
+ if respond_to?(:ibrain_current_user, true)
55
+ try(:ibrain_current_user)
56
+ # This one will be defined by Devise
57
+ elsif respond_to?(:current_ibrain_user, true)
58
+ try(:current_ibrain_user)
59
+ end
60
+ rescue StandardError => e
61
+ Ibrain::Logger.warn e.message.to_s
62
+
63
+ nil
64
+ end
65
+ end
66
+ end
61
67
  end
62
68
  end
@@ -1,11 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Ibrain::Core::ControllerHelpers::CurrentHost
4
- extend ActiveSupport::Concern
3
+ module Ibrain
4
+ module Core
5
+ module ControllerHelpers
6
+ module CurrentHost
7
+ extend ActiveSupport::Concern
5
8
 
6
- included do
7
- before_action do
8
- ActiveStorage::Current.host = request.base_url
9
+ included do
10
+ before_action do
11
+ ActiveStorage::Current.host = request.base_url
12
+ end
13
+ end
14
+ end
9
15
  end
10
16
  end
11
17
  end
@@ -1,47 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Ibrain::Core::ControllerHelpers::Response
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- helper_method :render_json_error
8
- helper_method :render_json_ok
9
- end
10
-
11
- protected
12
-
13
- def render_json_error(error, status)
14
- e_message = error.try(:record).try(:errors).try(:full_messages).try(:first)
15
- e_message = error.try(:message) if e_message.blank?
16
- e_message = error.try(:details) if e_message.blank?
17
-
18
- backtrace = error.try(:backtrace).try(:join, "\n")
19
-
20
- Ibrain::Logger.error e_message
21
- Ibrain::Logger.error backtrace
22
-
23
- render json: {
24
- errors: [{
25
- message: e_message,
26
- extensions: {
27
- code: status,
28
- exception: {
29
- stacktrace: [
30
- backtrace
31
- ]
32
- }
33
- }
34
- }],
35
- message: e_message,
36
- data: nil
37
- }, status: status
38
- end
39
-
40
- def render_json_ok(data, message, errors = [])
41
- render json: {
42
- errors: errors,
43
- message: message || I18n.t('ibrain.system.message.ok'),
44
- data: data.as_json
45
- }, status: :ok
3
+ module Ibrain
4
+ module Core
5
+ module ControllerHelpers
6
+ module Response
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ helper_method :render_json_error
11
+ helper_method :render_json_ok
12
+ end
13
+
14
+ protected
15
+
16
+ def render_json_error(error, status)
17
+ e_message = error.try(:record).try(:errors).try(:full_messages).try(:first)
18
+ e_message = error.try(:message) if e_message.blank?
19
+ e_message = error.try(:details) if e_message.blank?
20
+
21
+ backtrace = error.try(:backtrace).try(:join, "\n")
22
+
23
+ Ibrain::Logger.error e_message
24
+ Ibrain::Logger.error backtrace
25
+
26
+ render json: {
27
+ errors: [{
28
+ message: e_message,
29
+ extensions: {
30
+ code: status,
31
+ exception: {
32
+ stacktrace: [
33
+ backtrace
34
+ ]
35
+ }
36
+ }
37
+ }],
38
+ message: e_message,
39
+ data: nil
40
+ }, status: status
41
+ end
42
+
43
+ def render_json_ok(data, message, errors = [])
44
+ render json: {
45
+ errors: errors,
46
+ message: message || I18n.t('ibrain.system.message.ok'),
47
+ data: data.as_json
48
+ }, status: :ok
49
+ end
50
+ end
51
+ end
46
52
  end
47
53
  end
@@ -1,15 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Ibrain::Core::ControllerHelpers::StrongParameters
4
- def permitted_attributes
5
- Ibrain::PermittedAttributes
6
- end
3
+ module Ibrain
4
+ module Core
5
+ module ControllerHelpers
6
+ module StrongParameters
7
+ def permitted_attributes
8
+ Ibrain::PermittedAttributes
9
+ end
7
10
 
8
- delegate(*Ibrain::PermittedAttributes::ATTRIBUTES,
9
- to: :permitted_attributes,
10
- prefix: :permitted)
11
+ delegate(*Ibrain::PermittedAttributes::ATTRIBUTES,
12
+ to: :permitted_attributes,
13
+ prefix: :permitted)
11
14
 
12
- def permitted_user_attributes
13
- permitted_attributes.user_attributes
15
+ def permitted_user_attributes
16
+ permitted_attributes.user_attributes
17
+ end
18
+ end
19
+ end
14
20
  end
15
21
  end
@@ -2,11 +2,15 @@
2
2
 
3
3
  require 'ibrain/config'
4
4
 
5
- class Ibrain::Core::Engine < ::Rails::Engine
6
- isolate_namespace Ibrain::Core
7
- config.generators.api_only = true
5
+ module Ibrain
6
+ module Core
7
+ class Engine < ::Rails::Engine
8
+ isolate_namespace Ibrain::Core
9
+ config.generators.api_only = true
8
10
 
9
- initializer "ibrain.environment", before: :load_config_initializers do |app|
10
- app.config.ibrain = Ibrain::Config.environment
11
+ initializer "ibrain.environment", before: :load_config_initializers do |app|
12
+ app.config.ibrain = Ibrain::Config.environment
13
+ end
14
+ end
11
15
  end
12
16
  end