ibrain-core 0.1.2 → 0.1.6

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 (28) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/ibrain/graphql_controller.rb +1 -1
  3. data/app/graphql/ibrain/extentions/default_value.rb +0 -2
  4. data/app/graphql/ibrain/interfaces/record_interface.rb +0 -2
  5. data/app/graphql/ibrain/resolvers/base_resolver.rb +3 -3
  6. data/app/graphql/ibrain/types/attribute_type.rb +8 -0
  7. data/app/graphql/ibrain/types/base_node.rb +1 -1
  8. data/app/graphql/ibrain/types/base_object.rb +1 -1
  9. data/app/graphql/ibrain/types/filter_type.rb +0 -1
  10. data/app/models/ibrain/base.rb +24 -12
  11. data/app/repositories/ibrain/base_repository.rb +18 -0
  12. data/lib/generators/ibrain/graphql/core.rb +4 -0
  13. data/lib/generators/ibrain/graphql/mutation_generator.rb +4 -0
  14. data/lib/generators/ibrain/graphql/object_generator.rb +7 -0
  15. data/lib/generators/ibrain/graphql/resolver_generator.rb +4 -0
  16. data/lib/generators/ibrain/graphql/resolvers_generator.rb +4 -0
  17. data/lib/generators/ibrain/graphql/templates/aggregate.erb +1 -1
  18. data/lib/generators/ibrain/graphql/templates/mutation.erb +2 -1
  19. data/lib/generators/ibrain/graphql/templates/repository.erb +19 -0
  20. data/lib/generators/ibrain/graphql/templates/resolver.erb +1 -1
  21. data/lib/generators/ibrain/graphql/templates/resolvers.erb +1 -1
  22. data/lib/generators/ibrain/install/install_generator.rb +5 -16
  23. data/lib/generators/ibrain/install/templates/config/initializers/cors.tt +1 -1
  24. data/lib/generators/ibrain/install/templates/graphql/app_schema.rb.tt +0 -2
  25. data/lib/generators/ibrain/install/templates/rubocop.yml.tt +19 -2
  26. data/lib/ibrain/core/controller_helpers/auth.rb +6 -2
  27. data/lib/ibrain/core/version.rb +2 -2
  28. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15662385f790ab0c279015f64d2b030b3a8876b3ae065dcba33f882d1c8eb5ef
4
- data.tar.gz: b4d0abf1ada4db4e786c708625c554406259a98cb76ad49f23769e2da5c39aee
3
+ metadata.gz: f097ac075d8648653298078113ae7d36b8b9058b8a06dd93d450c6c1126ea1fc
4
+ data.tar.gz: 9dcdccbb0202e258a67d1f7373e2abb329f47fc6e2ad94faccd832e489a19c82
5
5
  SHA512:
6
- metadata.gz: bab3e2f29f099217bd5ba3f9d09ab5479208d80e0ced40df6284056d1eb5e5ba3a10c027b3e674ba813c76446e437faa4e8d696b85dc4940a887a42fe6e5c91f
7
- data.tar.gz: e23a79aa2347ca540cac61f20923fed42e0288ee98fda296e70734b1cab86740ad4e4a5b4b45524b593cb518ec94b303a8e6a230195a6bbed5a5205111feb6e3
6
+ metadata.gz: 49610235d7470b0634732e8f47c40d12d6e80cfeff70fa676b586883e251b2a125367ee34232e49c79456910e679827b1597a16b8aabe1a4dc4266c6d10bbf81
7
+ data.tar.gz: 137dbd8406dc08d92624cf78e0c8732b31b89507a39372cd0215d2ed6e3514d990f4ab90da097a45861cf351228e9c33af3bb9373561a2a9deab2790c2300e4c
@@ -10,7 +10,7 @@ module Ibrain
10
10
  variables: variables,
11
11
  context: {
12
12
  session: session,
13
- current_user: current_ibrain_user
13
+ current_user: try_ibrain_current_user
14
14
  },
15
15
  operation_name: operation_name
16
16
  )
@@ -3,8 +3,6 @@
3
3
  module Ibrain
4
4
  module Extentions
5
5
  class DefaultValue < GraphQL::Schema::FieldExtension
6
- description 'Default value extention'
7
-
8
6
  def after_resolve(value:, **_rest)
9
7
  if value.nil?
10
8
  options[:default_value]
@@ -3,8 +3,6 @@
3
3
  module Ibrain::Interfaces::RecordInterface
4
4
  include Ibrain::Types::BaseInterface
5
5
 
6
- description 'Common Interface'
7
-
8
6
  field :created_at, ::GraphQL::Types::ISO8601DateTime, null: false
9
7
  field :updated_at, ::GraphQL::Types::ISO8601DateTime, null: false
10
8
  end
@@ -3,9 +3,9 @@
3
3
  module Ibrain
4
4
  module Resolvers
5
5
  class BaseResolver < GraphQL::Schema::Resolver
6
- argument :filter, Ibrain::Types::FilterType, required: false, default_value: nil, description: 'Filter object for resolver'
7
- argument :limit, Int, required: false, default_value: 10, description: 'Limit records'
8
- argument :offset, Int, required: false, default_value: 0, description: 'Get records from cursor position'
6
+ argument :filter, Ibrain::Types::FilterType, required: false, default_value: nil
7
+ argument :limit, Int, required: false, default_value: 10
8
+ argument :offset, Int, required: false, default_value: 0
9
9
 
10
10
  def current_user
11
11
  context.fetch(:current_user)
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Types
5
+ class AttributeType < BaseScalar
6
+ end
7
+ end
8
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Ibrain::Types::BaseNode < Types::BaseObject
4
- implements ::GraphQL::Relay::Node.interface
4
+ include ::GraphQL::Relay::Node
5
5
  implements ::Ibrain::Interfaces::RecordInterface
6
6
 
7
7
  global_id_field :id
@@ -3,7 +3,7 @@
3
3
  module Ibrain
4
4
  module Types
5
5
  class BaseObject < GraphQL::Schema::Object
6
- implements GraphQL::Relay::Node.interface
6
+ include GraphQL::Relay::Node
7
7
 
8
8
  edge_type_class(Ibrain::Types::BaseEdge)
9
9
  connection_type_class(Ibrain::Types::BaseConnection)
@@ -3,7 +3,6 @@
3
3
  module Ibrain
4
4
  module Types
5
5
  class FilterType < GraphQL::Schema::Scalar
6
- description 'Filter Type'
7
6
  end
8
7
  end
9
8
  end
@@ -1,20 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ IGNORE_ATTRIBUTES = %w(id created_at updated_at)
4
+
3
5
  class Ibrain::Base < Ibrain::ApplicationRecord
4
6
  include ActionView::Helpers::DateHelper
5
7
 
6
8
  self.abstract_class = true
7
9
 
8
- def self.adjust_date_for_cdt(datetime)
9
- datetime.in_time_zone('UTC')
10
- end
11
-
12
10
  def string_id
13
11
  try(:id).try(:to_s)
14
12
  end
15
13
 
16
14
  scope :graphql_ransack, lambda { |params|
17
- ransack(params).result(distinct: true)
15
+ ransack(params).result
18
16
  }
19
17
 
20
18
  scope :reverse_scope, lambda {
@@ -36,12 +34,26 @@ class Ibrain::Base < Ibrain::ApplicationRecord
36
34
  time_ago_in_words(created_at)
37
35
  end
38
36
 
39
- # Provides a scope that should be included any time data
40
- # are fetched with the intention of displaying to the user.
41
- #
42
- # Allows individual stores to include any active record scopes or joins
43
- # when data are displayed.
44
- def self.display_includes
45
- where(nil)
37
+ class << self
38
+ # Provides a scope that should be included any time data
39
+ # are fetched with the intention of displaying to the user.
40
+ #
41
+ # Allows individual stores to include any active record scopes or joins
42
+ # when data are displayed.
43
+ def display_includes
44
+ where(nil)
45
+ end
46
+
47
+ def paginate(args)
48
+ limit(args[:limit]).offset(args[:offset])
49
+ end
50
+
51
+ def adjust_date_for_cdt(datetime)
52
+ datetime.in_time_zone('UTC')
53
+ end
54
+
55
+ def permitted_attributes
56
+ column_names.reject { |k| IGNORE_ATTRIBUTES.include?(k) }
57
+ end
46
58
  end
47
59
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ class BaseRepository
5
+ def initialize(current_user, record)
6
+ @current_user = current_user
7
+ @record = record
8
+ end
9
+
10
+ attr_reader :current_user, :record
11
+
12
+ protected
13
+
14
+ def cryptor
15
+ Ibrain::Encryptor.new
16
+ end
17
+ end
18
+ end
@@ -36,6 +36,10 @@ module Ibrain
36
36
  insert_root_type('query', 'QueryType')
37
37
  end
38
38
 
39
+ def create_repository_root_type
40
+ create_dir("app/repositories")
41
+ end
42
+
39
43
  def schema_file_path
40
44
  "#{options[:directory]}/#{schema_name.underscore}.rb"
41
45
  end
@@ -36,6 +36,10 @@ module Ibrain
36
36
  create_mutation_root_type
37
37
  end
38
38
 
39
+ if options[:model].present?
40
+ system("bundle exec rails generate ibrain:graphql:object #{options[:model].downcase}")
41
+ end
42
+
39
43
  template "mutation.erb", "#{options[:directory]}/mutations/#{file_name}.rb"
40
44
 
41
45
  sentinel = /class .*MutationType\s*<\s*[^\s]+?\n/m
@@ -29,7 +29,10 @@ module Ibrain
29
29
  desc: "Include the Relay Node interface"
30
30
 
31
31
  def create_type_file
32
+ create_dir('app/repositories') unless Dir.exist?('app/repositories')
33
+
32
34
  template "object.erb", "#{options[:directory]}/types/objects/#{type_file_name}.rb"
35
+ template "repository.erb", "app/repositories/#{type_name}_repository.rb"
33
36
  end
34
37
 
35
38
  def fields
@@ -72,6 +75,10 @@ module Ibrain
72
75
  false
73
76
  end
74
77
 
78
+ def model_name
79
+ type_name.capitalize
80
+ end
81
+
75
82
  def klass
76
83
  @klass ||= Module.const_get(type_name.camelize)
77
84
  end
@@ -20,6 +20,10 @@ module Ibrain
20
20
  create_resolver_root_type
21
21
  end
22
22
 
23
+ if options[:model].present?
24
+ system("bundle exec rails generate ibrain:graphql:object #{options[:model].downcase}")
25
+ end
26
+
23
27
  template "resolver.erb", "#{options[:directory]}/resolvers/#{file_name}.rb"
24
28
 
25
29
  sentinel = /class .*QueryType\s*<\s*[^\s]+?\n/m
@@ -36,6 +36,10 @@ module Ibrain
36
36
  create_resolver_root_type
37
37
  end
38
38
 
39
+ if options[:model].present?
40
+ system("bundle exec rails generate ibrain:graphql:object #{options[:model].downcase}")
41
+ end
42
+
39
43
  template "resolvers.erb", "#{options[:directory]}/resolvers/#{file_name}.rb"
40
44
  template "aggregate.erb", "#{options[:directory]}/resolvers/#{file_name}_aggregate.rb"
41
45
 
@@ -6,7 +6,7 @@ module Resolvers
6
6
  description 'Define aggregate to count total records for <%= resolver_name %>'
7
7
  # define resolve method
8
8
  def resolve(args)
9
- <%= model_name.capitalize %>.ransack(args[:filter]).result
9
+ <%= model_name.capitalize %>Repository.aggregate(args)
10
10
  end
11
11
  end
12
12
  end
@@ -14,7 +14,8 @@ module Mutations
14
14
 
15
15
  # TODO: define resolve method
16
16
  def resolve(args)
17
- # TODO: define logic
17
+ # TODO: define logic inside repository
18
+ # Something like Repository.new(current_user, Post.new).create(args)
18
19
  end
19
20
  end
20
21
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ <% module_namespacing_when_supported do -%>
4
+ class <%= model_name %>Repository < Ibrain::BaseRepository
5
+ def initialize(current_user, record = nil)
6
+ super(current_user, record)
7
+ end
8
+
9
+ class << self
10
+ def find(args)
11
+ <%= model_name %>.find args[:id]
12
+ end
13
+
14
+ def aggregate(args)
15
+ <%= model_name %>.ransack(args[:filter]).result
16
+ end
17
+ end
18
+ end
19
+ <% end -%>
@@ -13,7 +13,7 @@ module Resolvers
13
13
 
14
14
  # TODO: define resolve method
15
15
  def resolve(args)
16
- <%= model_name.capitalize %>.find args[:id]
16
+ <%= model_name.capitalize %>Repository.find args[:id]
17
17
  end
18
18
  end
19
19
  end
@@ -11,7 +11,7 @@ module Resolvers
11
11
 
12
12
  # TODO: define resolve method
13
13
  def resolve(args)
14
- super(args).limit(args[:limit]).offset(args[:offset])
14
+ super(args).paginate(args)
15
15
  end
16
16
  end
17
17
  end
@@ -35,17 +35,6 @@ module Ibrain
35
35
  end
36
36
 
37
37
  def configure_application
38
- application <<-RUBY
39
- # Load application's model / class decorators
40
- initializer 'ibrain.decorators' do |app|
41
- config.to_prepare do
42
- Dir.glob(Rails.root.join('app/**/*_decorator*.rb')) do |path|
43
- require_dependency(path)
44
- end
45
- end
46
- end
47
- RUBY
48
-
49
38
  if !options[:enforce_available_locales].nil?
50
39
  application <<-RUBY
51
40
  I18n.enforce_available_locales = #{options[:enforce_available_locales]}
@@ -66,7 +55,7 @@ module Ibrain
66
55
  Would you like to install it? (Y/n)"))
67
56
 
68
57
  @plugins_to_be_installed << 'ibrain-auth' unless system('gem list | grep ibrain-auth')
69
- @plugin_generators_to_run << 'ibrain:auth:install'
58
+ @plugin_generators_to_run << "ibrain:auth:install --with-ridgpole=#{options[:with_ridgepole]}"
70
59
  end
71
60
  end
72
61
 
@@ -135,10 +124,10 @@ module Ibrain
135
124
 
136
125
  def add_files
137
126
  template 'config/initializers/ibrain.rb.tt', 'config/initializers/ibrain.rb', { skip: true }
138
- template 'config/initializers/cors.tt', 'config/initializers/cors.rb', { skip: true }
139
- template 'config/puma.tt', 'config/puma.rb', { skip: true }
140
- yml_template 'config/database.tt', 'config/database.yml', { skip: true }
141
- template 'rubocop.yml.tt', '.rubocop.yml', { skip: true } if options[:with_rubocop]
127
+ template 'config/initializers/cors.tt', 'config/initializers/cors.rb'
128
+ template 'config/puma.tt', 'config/puma.rb'
129
+ yml_template 'config/database.tt', 'config/database.yml'
130
+ template 'rubocop.yml.tt', '.rubocop.yml' if options[:with_rubocop]
142
131
 
143
132
  if options[:with_graphql]
144
133
  template 'graphql/app_schema.rb.tt', 'app/graphql/app_schema.rb', { skip: true }
@@ -7,7 +7,7 @@
7
7
 
8
8
  Rails.application.config.middleware.insert_before 0, Rack::Cors do
9
9
  allow do
10
- origins '*'
10
+ origins ['localhost']
11
11
 
12
12
  resource '*',
13
13
  headers: :any,
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AppSchema < Ibrain::BaseSchema
4
- description 'Define root schema'
5
-
6
4
  mutation(Types::MutationType)
7
5
  query(Types::QueryType)
8
6
  end
@@ -113,6 +113,7 @@ Rails/DynamicFindBy:
113
113
  # It's okay to skip model validations to setup a spec.
114
114
  Rails/SkipsModelValidations:
115
115
  Exclude:
116
+ - db/seeds/**/*.rb
116
117
  - '*/spec/**/*'
117
118
 
118
119
  # We use a lot of
@@ -277,7 +278,7 @@ Metrics/BlockLength:
277
278
  Metrics/CyclomaticComplexity:
278
279
  Enabled: false
279
280
 
280
- Metrics/LineLength:
281
+ Layout/LineLength:
281
282
  Enabled: false
282
283
 
283
284
  Metrics/MethodLength:
@@ -595,4 +596,20 @@ Gemspec/RequiredRubyVersion:
595
596
  Enabled: false
596
597
 
597
598
  GraphQL/FieldDescription:
598
- Enabled: false
599
+ Enabled: false
600
+
601
+ GraphQL/ObjectDescription:
602
+ Enabled: true
603
+ Exclude:
604
+ - app/graphql/policies/*.rb
605
+ - app/graphql/*.rb
606
+
607
+ GraphQL/ExtractType:
608
+ Enabled: false
609
+
610
+ Rails/UnknownEnv:
611
+ Environments:
612
+ - production
613
+ - development
614
+ - test
615
+ - staging
@@ -52,11 +52,15 @@ module Ibrain
52
52
  # This one will be defined by apps looking to hook into Ibrain
53
53
  # As per authentication_helpers.rb
54
54
  if respond_to?(:ibrain_current_user, true)
55
- ibrain_current_user
55
+ try(:ibrain_current_user)
56
56
  # This one will be defined by Devise
57
57
  elsif respond_to?(:current_ibrain_user, true)
58
- current_ibrain_user
58
+ try(:current_ibrain_user)
59
59
  end
60
+ rescue StandardError => e
61
+ Ibrain::Logger.warn e.message.to_s
62
+
63
+ nil
60
64
  end
61
65
  end
62
66
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ibrain
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.6"
5
5
 
6
6
  def self.ibrain_version
7
7
  VERSION
8
8
  end
9
9
 
10
10
  def self.previous_ibrain_minor_version
11
- '0.1.1'
11
+ '0.1.5'
12
12
  end
13
13
 
14
14
  def self.ibrain_gem_version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibrain-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tai Nguyen Van
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-20 00:00:00.000000000 Z
11
+ date: 2021-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord-session_store
@@ -183,6 +183,7 @@ files:
183
183
  - app/graphql/ibrain/resolvers/base_aggregate.rb
184
184
  - app/graphql/ibrain/resolvers/base_resolver.rb
185
185
  - app/graphql/ibrain/types/aggregate_type.rb
186
+ - app/graphql/ibrain/types/attribute_type.rb
186
187
  - app/graphql/ibrain/types/base_argument.rb
187
188
  - app/graphql/ibrain/types/base_connection.rb
188
189
  - app/graphql/ibrain/types/base_edge.rb
@@ -210,6 +211,7 @@ files:
210
211
  - app/models/ibrain/legacy_user.rb
211
212
  - app/models/ibrain/role.rb
212
213
  - app/models/ibrain/role_user.rb
214
+ - app/repositories/ibrain/base_repository.rb
213
215
  - config/initializers/friendly_id.rb
214
216
  - config/locales/en.yml
215
217
  - config/locales/jp.yml
@@ -223,6 +225,7 @@ files:
223
225
  - lib/generators/ibrain/graphql/templates/aggregate.erb
224
226
  - lib/generators/ibrain/graphql/templates/mutation.erb
225
227
  - lib/generators/ibrain/graphql/templates/object.erb
228
+ - lib/generators/ibrain/graphql/templates/repository.erb
226
229
  - lib/generators/ibrain/graphql/templates/resolver.erb
227
230
  - lib/generators/ibrain/graphql/templates/resolvers.erb
228
231
  - lib/generators/ibrain/graphql/type_generator.rb