hai 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f748b48e465a88f0d1ff069c711d9ea21e0ac855e6a261bb29cf0a8e52e75585
4
- data.tar.gz: ad117a23c477fec0ec5494b7bda4d1143a4698fbd7796cffebbb0995d25571db
3
+ metadata.gz: 398bd31884dbdd9428de5a190d7266098c5ffbe0516e9d46587c6986097d3626
4
+ data.tar.gz: 5f1a60c718f37fdae163f36b8575c36826f0fe511410e6981d7ca48d4f2c0256
5
5
  SHA512:
6
- metadata.gz: bfa8f4be0c3bbeb2719113b19a25a8b5846ff0013daa93f8f9c965463c2a26776ef1044b2628c7e39d0d77be069089be729a1351a5d697a2d71f8b1b2c91df1a
7
- data.tar.gz: 0a07c759c76f2fc8dd347be3f3e5b1760a7eacead3dd199abae296bf0aa608e172916d0a419f4021732f33d7db3f8c5de9cb196dcd14aacd56f7277e723a510e
6
+ metadata.gz: 4db91d3fccbeec97c69aaee403a9d861f4a7ab5837f66279e0102041ae51d58505bc21f9bfebd26b148c2ae41de1c8b53479885bd73eb1297995a437683ebb2d
7
+ data.tar.gz: f6468efb72bdbf3c1e04f00de6778ba43b4bbf839f1c17bec32eee0918fc2ac23049ef2c4579fff6c8827fbbfb761596430310e0a1243b0fea3437d7fbbf6ab3
@@ -0,0 +1,61 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/base'
3
+
4
+ module Hai
5
+ module Install
6
+ class GraphqlGenerator < Rails::Generators::Base
7
+ def rails_preload
8
+ Rails.application.eager_load!
9
+ end
10
+
11
+ def install_graphql_ruby
12
+ gem 'graphql'
13
+ run "bundle install"
14
+ run "rails generate graphql:install"
15
+ end
16
+
17
+ def add_types
18
+ hai_types = "hay_types(#{model_names.join(', ')})"
19
+ inject_into_file "app/graphql/#{app_name.underscore}_schema.rb", after: "class #{app_name}Schema < GraphQL::Schema" do <<~RUBY.indent(4)
20
+
21
+ include Hai::GraphQL::Types
22
+ hai_types(#{model_names.join(', ')})
23
+ RUBY
24
+ end
25
+ end
26
+
27
+ def add_queries
28
+ inject_into_file "app/graphql/types/query_type.rb", after: "include GraphQL::Types::Relay::HasNodesField" do <<~RUBY.indent(4)
29
+
30
+ include Hai::GraphQL
31
+ hai_query(#{model_names.join(', ')})
32
+ RUBY
33
+ end
34
+ end
35
+
36
+ def add_mutations
37
+ inject_into_file "app/graphql/types/mutation_type.rb", after: "class MutationType < Types::BaseObject" do <<~RUBY.indent(4)
38
+
39
+ include Hai::GraphQL
40
+ hai_mutation(#{model_names.join(', ')})
41
+ RUBY
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def model_names
48
+ ApplicationRecord.descendants.map(&:name)
49
+ end
50
+
51
+ def app_name
52
+ require File.expand_path("config/application", destination_root)
53
+ if Rails.application.class.respond_to?(:module_parent_name)
54
+ Rails.application.class.module_parent_name
55
+ else
56
+ Rails.application.class.parent_name
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/base'
3
+
4
+ module Hai
5
+ module Rest
6
+ class InstallGenerator < Rails::Generators::Base
7
+ def mount_engine
8
+ route <<-RUBY
9
+ mount Hai::Rest::Engine => "/rest"
10
+ RUBY
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/hai/create.rb CHANGED
@@ -14,7 +14,7 @@ module Hai
14
14
 
15
15
  return unauthorized_error unless check_policy(instance)
16
16
 
17
- instance.assign_attributes(**attrs)
17
+ instance.assign_attributes(**attrs) unless attrs.empty?
18
18
 
19
19
  run_action_modification(instance)
20
20
 
@@ -15,18 +15,21 @@ module Hai
15
15
  klass.description("Attributes for creating or updating a #{model}.")
16
16
  model.attribute_types.each do |attr, type|
17
17
  next if %w[id created_at updated_at].include?(attr)
18
+ next if attr.blank? # if the model has no other attributes
19
+
20
+ presence_validators = get_presence_validators(model)
18
21
 
19
22
  klass.argument(
20
23
  attr,
21
24
  Hai::GraphQL::TYPE_CAST[type.class] ||
22
25
  Hai::GraphQL::TYPE_CAST[type.class.superclass],
23
- required: false
26
+ required: presence_validators.include?(attr),
24
27
  )
25
28
  end
26
29
 
27
30
  klass.field(:result, ::Types.const_get("#{model}Type"))
28
31
 
29
- klass.define_method(:resolve) do |args|
32
+ klass.define_method(:resolve) do |args = {}|
30
33
  Hai::Create.new(model, context).execute(**args)
31
34
  end
32
35
  Hai::GraphQL::Types.const_set("Create#{model}", klass)
@@ -38,7 +41,18 @@ module Hai
38
41
  mutation: Hai::GraphQL::Types.const_get("Create#{model}")
39
42
  )
40
43
  end
44
+
45
+ private
46
+
47
+ def get_presence_validators(model)
48
+ model.validators.select do |v|
49
+ v.kind == :presence
50
+ end
51
+ .flat_map(&:attributes)
52
+ .map(&:to_s)
53
+ end
41
54
  end
42
55
  end
43
56
  end
44
57
  end
58
+
@@ -45,6 +45,7 @@ module Hai
45
45
 
46
46
  klass = Class.new(::Types::BaseObject)
47
47
  model.attribute_types.each do |attr, type|
48
+ next if defined?(model.get_restricted_attributes) && model.get_restricted_attributes.include?(attr.to_sym) # add test plz
48
49
  klass.send(:field, attr, Hai::GraphQL::TYPE_CAST[type.class] || Hai::GraphQL::TYPE_CAST[type.class.superclass])
49
50
  rescue ArgumentError => e
50
51
  binding.pry
@@ -0,0 +1,17 @@
1
+ module Hai
2
+ module RestrictedAttributes
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def restricted_attributes(*attrs)
9
+ @@restricted_attributes = attrs
10
+ end
11
+
12
+ def get_restricted_attributes
13
+ @@restricted_attributes
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/hai/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hai
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
data/lib/hai.rb CHANGED
@@ -12,15 +12,18 @@ require_relative "hai/read"
12
12
  require_relative "hai/create"
13
13
  require_relative "hai/update"
14
14
  require_relative "hai/delete"
15
+ require_relative "hai/restricted_attributes"
15
16
 
16
17
  require_relative "hai/policies"
17
18
  require_relative "hai/action_mods"
18
19
  require "hai/railtie" if defined?(Rails)
20
+ require "generators/rest/install_generator" if defined?(Rails)
21
+ require "generators/install/graphql_generator" if defined?(Rails)
19
22
 
20
23
  module Hai
21
24
  class Error < StandardError
22
25
  end
23
- class Rest
26
+ module Rest
24
27
  class Engine < ::Rails::Engine
25
28
  isolate_namespace Hai
26
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - basicbrogrammer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-07 00:00:00.000000000 Z
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -82,6 +82,8 @@ files:
82
82
  - Rakefile
83
83
  - app/controllers/hai/rest_controller.rb
84
84
  - config/routes.rb
85
+ - lib/generators/install/graphql_generator.rb
86
+ - lib/generators/rest/install_generator.rb
85
87
  - lib/hai.rb
86
88
  - lib/hai/action_mods.rb
87
89
  - lib/hai/create.rb
@@ -97,6 +99,7 @@ files:
97
99
  - lib/hai/policies.rb
98
100
  - lib/hai/railtie.rb
99
101
  - lib/hai/read.rb
102
+ - lib/hai/restricted_attributes.rb
100
103
  - lib/hai/tasks/graphql/filter_type.rake
101
104
  - lib/hai/tasks/hai.rake
102
105
  - lib/hai/types/arel/boolean_input_type.rb