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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f13577d209148f4c104609f05463dd060d6fecc329f98b27a7a7dc869a33440
4
- data.tar.gz: 552c0702277834751c83fd12cbed771f1951e0e06b2a30b26fe4ea787de41cb8
3
+ metadata.gz: 55f44848432065a0054e00b388a1cc124ef98011f7030ab993bc11a367740478
4
+ data.tar.gz: 15e60a4bfedd8cbb6abf085fbd46492ac48e8a6716146f043ef7247fd86c540f
5
5
  SHA512:
6
- metadata.gz: ff5f9e6f704d612ab0fbb0716871a6181dcd470e7483d2218e9c3a1f0292bcf33b04a756b2ca970eb48d07187d6e25bf1efd9eb7d8c07cdca07b65a77fd54f10
7
- data.tar.gz: f55e963d1d6f2f16e79551210b755a78e174579410883cb30fd1945be3d94b1962daeba8064d2afbfd230a045a8c286ce4a16fa7e701bd2ca21b2d1da92b1491
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
@@ -10,7 +10,7 @@ module Rhino
10
10
  MAJOR = 0
11
11
  MINOR = 20
12
12
  TINY = 0
13
- PRE = "beta.68"
13
+ PRE = "beta.72"
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
16
  end
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.68
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-26 00:00:00.000000000 Z
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