active_record-associated_object 0.7.1 → 0.8.1

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: 4b86335fcc0e7c367775d2f21fef7537a39c847022ef83feebc1bd68ed21baf1
4
- data.tar.gz: b2031efb700cccd28eb7b4414ee4bb8a4e45c4bd0912658f4d54301188987cec
3
+ metadata.gz: 76dd7495e0e2e855e708737a875ff879a8ec840d68f5c80ad4733ece3b72dda6
4
+ data.tar.gz: '09b38711104836c6c222a1663ddf4e7248ae784b727b597ed61f6436ab2bf17b'
5
5
  SHA512:
6
- metadata.gz: 664b086f82fcaa6c7029d7e3b44cc4fb8cbeaa581761927054cf80bfddad26cd63b12504a380f00109926e23310ef7bcef4970f99acb5c6b7397080d0410b64d
7
- data.tar.gz: 0b3053a708426b83fcbe233394aed806347e8dc74d6305b2a19221de21b30f25e70a8c0e9918b62255639d6d6ac4e3f77a01a514efb98522bd1db78a5a217216
6
+ metadata.gz: 06fc0e959dedecab41a3c0d48d846eda454bd69c48fe8042cad1378c408fd919f688c2e07d5a26d675316556210ae25796585bc56438c8c66e3092c52fe3b1f6
7
+ data.tar.gz: bd35d99fc113f26d2b49e1fde335db7c28af90e7985ea0d2c60dc9dd8188e05cc2c4102bbe4edf5c45998d2b5cc96cd8f00bcd8974acf858450db479432c84f8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_record-associated_object (0.7.1)
4
+ active_record-associated_object (0.8.1)
5
5
  activerecord (>= 6.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -62,6 +62,9 @@ We've fixed this so you don't need to care, but this is what's happening.
62
62
  > [!TIP]
63
63
  > You can pass multiple names too: `has_object :publisher, :classified, :fortification`. I recommend `-[i]er`, `-[i]ed` and `-ion` as the general naming conventions for your Associated Objects.
64
64
 
65
+ > [!TIP]
66
+ > Plural Associated Object names are also supported: `Account.has_object :seats` will look up `Account::Seats`.
67
+
65
68
  See how we're always expecting a link to the model, here `post`?
66
69
 
67
70
  Because of that, you can rely on `post` from the associated object:
@@ -82,6 +85,12 @@ class Post::Publisher < ActiveRecord::AssociatedObject
82
85
  end
83
86
  ```
84
87
 
88
+ ### Use the generator to help write Associated Objects
89
+
90
+ To set up the `Post::Publisher` from above, you can call `bin/rails generate associated Post::Publisher`.
91
+
92
+ See `bin/rails generate associated --help` for more info.
93
+
85
94
  ### Forwarding callbacks onto the associated object
86
95
 
87
96
  To further help illustrate how your collaborator Associated Objects interact with your domain model, you can forward callbacks.
@@ -14,7 +14,10 @@ module ActiveRecord::AssociatedObject::ObjectAssociation
14
14
  module ClassMethods
15
15
  def has_object(*names, **callbacks)
16
16
  extend_source_from(names) do |name|
17
- "def #{name}; (@associated_objects ||= {})[:#{name}] ||= #{const_get(name.to_s.classify)}.new(self); end"
17
+ const_get object_name = name.to_s.camelize
18
+ "def #{name}; (@associated_objects ||= {})[:#{name}] ||= #{object_name}.new(self); end"
19
+ rescue NameError
20
+ raise "The #{self}::#{object_name} associated object referenced from #{self} doesn't exist"
18
21
  end
19
22
 
20
23
  extend_source_from(names) do |name|
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  class AssociatedObject
5
- VERSION = "0.7.1"
5
+ VERSION = "0.8.1"
6
6
  end
7
7
  end
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Create a PORO collaborator associated object inheriting from `ActiveRecord::AssociatedObject` that's associated with an Active Record record class.
3
+
4
+ It'll be associated on the record with `has_object`.
5
+
6
+ Note: associated object names support pluralized class names. So "Seats" remain "seats" in all cases, and "Seat" remains "seat" in all cases.
7
+ Example:
8
+ bin/rails generate associated Organization::Seats
9
+
10
+ This will create:
11
+ app/models/organization/seats.rb
12
+ test/models/organization/seats_test.rb
13
+
14
+ And in Organization, this will insert:
15
+ has_object :seats
@@ -0,0 +1,27 @@
1
+ class AssociatedGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("templates", __dir__)
3
+
4
+ def generate_associated_object_files
5
+ template "associated.rb", "app/models/#{name.underscore}.rb"
6
+ template "associated_test.rb", "test/models/#{name.underscore}_test.rb"
7
+ end
8
+
9
+ def connect_associated_object
10
+ record_file = "#{destination_root}/app/models/#{record_path}.rb"
11
+ raise "Record class '#{record_klass}' does not exist" unless File.exist?(record_file)
12
+
13
+ inject_into_class record_file, record_klass do
14
+ optimize_indentation "has_object :#{associated_object_path}", 2
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ # The `:name` argument can handle model names, but associated object class names aren't singularized.
21
+ # So these record and associated_object methods prevent that.
22
+ def record_path = record_klass.downcase.underscore
23
+ def record_klass = name.deconstantize
24
+
25
+ def associated_object_path = associated_object_class.downcase.underscore
26
+ def associated_object_class = name.demodulize
27
+ end
@@ -0,0 +1,5 @@
1
+ class <%= name %> < ActiveRecord::AssociatedObject
2
+ extension do
3
+ # Extend <%= record_klass %> here
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "test_helper"
2
+
3
+ class <%= name %>Test < ActiveSupport::TestCase
4
+ setup do
5
+ # @<%= record_path %> = <%= record_path.pluralize %>(:TODO_fixture_name)
6
+ # @<%= associated_object_path %> = @<%= record_path %>.<%= associated_object_path %>
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-associated_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Timm Hansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-24 00:00:00.000000000 Z
11
+ date: 2024-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -43,6 +43,10 @@ files:
43
43
  - lib/active_record/associated_object/object_association.rb
44
44
  - lib/active_record/associated_object/railtie.rb
45
45
  - lib/active_record/associated_object/version.rb
46
+ - lib/generators/associated/USAGE
47
+ - lib/generators/associated/associated_generator.rb
48
+ - lib/generators/associated/templates/associated.rb.tt
49
+ - lib/generators/associated/templates/associated_test.rb.tt
46
50
  homepage: https://github.com/kaspth/active_record-associated_object
47
51
  licenses:
48
52
  - MIT
@@ -65,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
69
  - !ruby/object:Gem::Version
66
70
  version: '0'
67
71
  requirements: []
68
- rubygems_version: 3.5.4
72
+ rubygems_version: 3.5.6
69
73
  signing_key:
70
74
  specification_version: 4
71
75
  summary: Associate a Ruby PORO with an Active Record class and have it quack like