rhino_project_core 0.20.0.beta.70 → 0.20.0.beta.73

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: 6aa4e226b98c3223abcfd81ba47f3a0069235b73e123d11e7eebbcf0dccc875c
4
- data.tar.gz: f6a76ba023f0a4712376d6f65836bb0245e5bb478648b7633e9bb8533f5518ce
3
+ metadata.gz: 7edee6ca00a405ca149d1004bb4fb03bd9438e6059b69d8350ac64cabc5b6795
4
+ data.tar.gz: e57202cf8a02573508e90da85e4b576f2ad171b715edc12ea4762a6e85923028
5
5
  SHA512:
6
- metadata.gz: 474029ecb6f2f4802b06e4cb9da5ba5d6150f3bac88e78a4ac4edb93745299f33e189e1d2abbf663e0c5375e8233b35c8cb9ef2861d8eb204bc044b6685eaefd
7
- data.tar.gz: 263b6c2ab28f52b465fb04b39ab7315f06522be318dd5e898895dc9878f71ae9031b1230c89660bf1844c24403635cad14010c533a1745913c86823cec66418c
6
+ metadata.gz: 8a5b00c842efa26aba2efc4a7d6ebb5ba2342dec4adb3da278389a8bb7f3c7dd7938042801e88c2392aadef98f0a1081e65af1e45539b4ee58c66390678a4e03
7
+ data.tar.gz: 5f3c43b60b19c931575d62c55ae4e0b2da88e0058a1b9101e721c4556f9257aacd5fb64a0d5c2d6d0ed97c723f9346259871da689b7c1f3c1b41c16f89a38a24
@@ -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.70"
13
+ PRE = "beta.73"
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.70
4
+ version: 0.20.0.beta.73
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-10 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