rhino_project_core 0.20.0.beta.68 → 0.20.0.beta.72
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/rhino/model/model_generator.rb +91 -0
- data/lib/rhino/engine.rb +2 -2
- data/lib/rhino/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55f44848432065a0054e00b388a1cc124ef98011f7030ab993bc11a367740478
|
4
|
+
data.tar.gz: 15e60a4bfedd8cbb6abf085fbd46492ac48e8a6716146f043ef7247fd86c540f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b6e895a81b17d14ba3a368eef707d75340bed3d3412b06c0de5249fec809cd672256c3be68d9e3a0c8a663794aff190c6f168a4b106545cbc0c0e562669cc0b
|
7
|
+
data.tar.gz: bb603a1b7d4b1fc122b427b855f884cf9868f1f68f46f9037130435a53d65257110af6cc341281093a3d5aa9031944bfa3278e2e36239f223f9c58851e4a0015
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
require "rails/generators/active_record/model/model_generator"
|
5
|
+
|
6
|
+
module Rhino
|
7
|
+
class ModelGenerator < ::ActiveRecord::Generators::ModelGenerator
|
8
|
+
# This is the path to the templates used by the original model generator
|
9
|
+
# I think this can be removed in Rails 7.1 https://github.com/rails/rails/pull/47181
|
10
|
+
source_root File.join(File.dirname(::ActiveRecord::Generators::ModelGenerator.instance_method(:create_migration_file).source_location.first),
|
11
|
+
"templates")
|
12
|
+
|
13
|
+
class_option :owner, type: :string, desc: "The model is owned by the reference attribute", group: :owner
|
14
|
+
class_option :base_owner, type: :boolean, default: false, desc: "The model is owned by the base owner", group: :owner
|
15
|
+
class_option :global_owner, type: :boolean, default: false, desc: "The model is globally owned", group: :owner
|
16
|
+
|
17
|
+
def create_migration_file
|
18
|
+
# We check the ownership of the reference attributes before creating the migration
|
19
|
+
check_ownership
|
20
|
+
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
def inject_into_rhino_initializer
|
25
|
+
# FIXME: Can we do better on indentation?
|
26
|
+
inject_into_file "config/initializers/rhino.rb", before: /^end\s*$/ do
|
27
|
+
" config.resources += ['#{class_name}']\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def inject_rhino_owner_into_model
|
32
|
+
# Write 'rhino_owner :<attribute name>' to the model file
|
33
|
+
model_file = File.join("app/models", "#{file_path}.rb")
|
34
|
+
|
35
|
+
# FIXME: Can we do better on indentation?
|
36
|
+
inject_into_file model_file, before: /^end\s*$/ do
|
37
|
+
"\n #{owner_call}\n"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Global owned don't need a reference
|
41
|
+
return if global_owner?
|
42
|
+
|
43
|
+
inject_into_file model_file, after: /#{owner_call}/ do
|
44
|
+
"\n rhino_references %i[#{owner_name}]"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
def check_ownership
|
50
|
+
raise Thor::Error, "Exactly one owner must be defined on a reference or globally" unless base_owner? ^ global_owner? ^ reference_owner?
|
51
|
+
|
52
|
+
return if global_owner? || reference_attributes.find { |attr| attr.name == owner_name }
|
53
|
+
|
54
|
+
raise Thor::Error, "The owner attribute must be a reference attribute"
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def global_owner?
|
59
|
+
@global_owner = options[:global_owner]
|
60
|
+
end
|
61
|
+
|
62
|
+
def base_owner?
|
63
|
+
@global_owner = options[:base_owner] || Rhino.base_owner.model_name.singular == options[:owner]
|
64
|
+
end
|
65
|
+
|
66
|
+
def reference_owner?
|
67
|
+
@reference_owner = options[:owner].present? && !base_owner?
|
68
|
+
end
|
69
|
+
|
70
|
+
def owner_name
|
71
|
+
@owner_name ||= if global_owner?
|
72
|
+
"global"
|
73
|
+
elsif base_owner?
|
74
|
+
Rhino.base_owner.model_name.singular
|
75
|
+
else
|
76
|
+
options[:owner]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def owner_call
|
81
|
+
return "rhino_owner_global" if global_owner?
|
82
|
+
return "rhino_owner_base" if base_owner?
|
83
|
+
|
84
|
+
"rhino_owner :#{owner_name}"
|
85
|
+
end
|
86
|
+
|
87
|
+
def reference_attributes
|
88
|
+
@reference_attributes ||= attributes.filter(&:reference?)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/rhino/engine.rb
CHANGED
@@ -131,11 +131,11 @@ module Rhino
|
|
131
131
|
end
|
132
132
|
|
133
133
|
def self.run_from_dev_setup?
|
134
|
-
Rake.application.top_level_tasks == ["rhino:dev:setup"]
|
134
|
+
defined?(Rake.application) && Rake.application.top_level_tasks == ["rhino:dev:setup"]
|
135
135
|
end
|
136
136
|
|
137
137
|
def self.run_from_package?
|
138
|
-
Rake.application.top_level_tasks == ["package"]
|
138
|
+
defined?(Rake.application) && Rake.application.top_level_tasks == ["package"]
|
139
139
|
end
|
140
140
|
end
|
141
141
|
end
|
data/lib/rhino/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhino_project_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.20.0.beta.
|
4
|
+
version: 0.20.0.beta.72
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JP Rosevear
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05
|
11
|
+
date: 2024-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -178,6 +178,7 @@ files:
|
|
178
178
|
- lib/generators/rhino/install/templates/account.rb
|
179
179
|
- lib/generators/rhino/install/templates/rhino.rb
|
180
180
|
- lib/generators/rhino/install/templates/user.rb
|
181
|
+
- lib/generators/rhino/model/model_generator.rb
|
181
182
|
- lib/generators/rhino/module/module_generator.rb
|
182
183
|
- lib/generators/rhino/module/templates/engine.rb.tt
|
183
184
|
- lib/generators/rhino/module/templates/install_generator.rb.tt
|