active_record-associated_object 0.8.0 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +17 -0
- data/lib/active_record/associated_object/object_association.rb +4 -1
- data/lib/active_record/associated_object/version.rb +1 -1
- data/lib/generators/associated/USAGE +15 -0
- data/lib/generators/associated/associated_generator.rb +28 -0
- data/lib/generators/associated/templates/associated.rb.tt +5 -0
- data/lib/generators/associated/templates/associated_test.rb.tt +8 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5816013890fc0f6868149661030643f47028edbb6a188d5014e7fd699e0138bd
|
4
|
+
data.tar.gz: ef0c4be8afb96727e5f379c988632ec44f0f77d105b8aadde944ac48362b7385
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f80f20322bf4661515fb6235c9d034e7ac68a22bc17e269971c8b2a1f5782affb28471316b65402bab66deedc9f63f322032fa216100c6966cd514c7421a100
|
7
|
+
data.tar.gz: 9def815025bf60d33b06ba6f7c9c185a59fca016f3e88a72d65fb64408dd0b4e7a34aea63c5a75de61004a64a80b86bd26e368ed021d105ed32bf6cca3a3f037
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -85,6 +85,23 @@ class Post::Publisher < ActiveRecord::AssociatedObject
|
|
85
85
|
end
|
86
86
|
```
|
87
87
|
|
88
|
+
### See Associated Objects in action
|
89
|
+
|
90
|
+
The team at [Flipper](https://www.flippercloud.io) used Associated Objects to help keep their new billing structure clean.
|
91
|
+
|
92
|
+
You can see real life examples in these blog posts:
|
93
|
+
|
94
|
+
- [Organizing Rails Code with ActiveRecord Associated Objects](https://garrettdimon.com/journal/posts/organizing-rails-code-with-activerecord-associated-objects)
|
95
|
+
- [Data Modeling Entitlements and Pricing for SaaS Applications](https://garrettdimon.com/journal/posts/data-modeling-saas-entitlements-and-pricing)
|
96
|
+
|
97
|
+
If your team is using Associated Objects, we're more than happy to feature any write ups here.
|
98
|
+
|
99
|
+
### Use the generator to help write Associated Objects
|
100
|
+
|
101
|
+
To set up the `Post::Publisher` from above, you can call `bin/rails generate associated Post::Publisher`.
|
102
|
+
|
103
|
+
See `bin/rails generate associated --help` for more info.
|
104
|
+
|
88
105
|
### Forwarding callbacks onto the associated object
|
89
106
|
|
90
107
|
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
|
-
|
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|
|
@@ -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,28 @@
|
|
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
|
+
|
12
|
+
raise "Record class '#{record_klass}' does not exist" unless File.exist?(record_file)
|
13
|
+
|
14
|
+
inject_into_class record_file, record_klass do
|
15
|
+
optimize_indentation "has_object :#{associated_object_path}", 2
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# The `:name` argument can handle model names, but associated object class names aren't singularized.
|
22
|
+
# So these record and associated_object methods prevent that.
|
23
|
+
def record_path = record_klass.downcase.underscore
|
24
|
+
def record_klass = name.camelize.deconstantize
|
25
|
+
|
26
|
+
def associated_object_path = associated_object_class.underscore
|
27
|
+
def associated_object_class = name.camelize.demodulize
|
28
|
+
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.8.
|
4
|
+
version: 0.8.2
|
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-
|
11
|
+
date: 2024-10-11 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.
|
72
|
+
rubygems_version: 3.5.18
|
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
|