Skip to content

Commit d3284c4

Browse files
committed
Allow alternative class names
Changed: Pass class_name to the Null() or Void() methods with an alternative class name.
1 parent b0a77f8 commit d3284c4

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ class User < ApplicationRecord
6868
end
6969
```
7070

71+
Customize the null class name:
72+
73+
```ruby
74+
class User < ApplicationRecord
75+
Null(class_name: "Guest")
76+
class << self
77+
alias_method :null, :guest
78+
end
79+
end
80+
81+
User.guest # returns a User::Guest instance
82+
```
83+
7184
### Void Objects
7285

7386
While `Null` objects are singletons (one instance per model), `Void` objects are instantiable null objects that allow creating multiple instances with different attribute values.
@@ -115,6 +128,18 @@ Void objects support the same features as Null objects:
115128
- Custom methods via block syntax
116129
- Association handling
117130
- All ActiveRecord query methods (`null?`, `persisted?`, etc.)
131+
- Custom class names via `class_name:` parameter
132+
133+
```ruby
134+
class Product < ApplicationRecord
135+
Void(class_name: "Placeholder")
136+
class << self
137+
alias_method :void, :placeholder
138+
end
139+
end
140+
141+
Product.placeholder # returns a Product::Placeholder instance
142+
```
118143

119144
Use `Null` when you need a single shared null object instance. Use `Void` when you need multiple null object instances with different attribute values.
120145

lib/activerecord/null.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def initialize_attribute_methods
156156
#
157157
# @param inherit [Class] The class from which the Null object inherits attributes
158158
# @param assignments [Array] The attributes to assign to the null object
159-
def Null(inherit = self, assignments = {}, &)
159+
def Null(inherit = self, class_name: :Null, **assignments, &)
160160
if inherit.is_a?(Hash)
161161
assignments = inherit
162162
inherit = self
@@ -165,7 +165,7 @@ def Null(inherit = self, assignments = {}, &)
165165
null_class = create_null_class(inherit, assignments, singleton: true)
166166
null_class.class_eval(&) if block_given?
167167

168-
inherit.const_set(:Null, null_class)
168+
inherit.const_set(class_name, null_class)
169169
inherit.define_singleton_method(:null) { null_class.instance }
170170
end
171171

@@ -185,7 +185,7 @@ def Null(inherit = self, assignments = {}, &)
185185
#
186186
# @param inherit [Class] The class from which the Void object inherits attributes
187187
# @param assignments [Hash] The default attributes to assign to void objects
188-
def Void(inherit = self, assignments = {}, &)
188+
def Void(inherit = self, class_name: :Void, **assignments, &)
189189
if inherit.is_a?(Hash)
190190
assignments = inherit
191191
inherit = self
@@ -194,7 +194,7 @@ def Void(inherit = self, assignments = {}, &)
194194
void_class = create_null_class(inherit, assignments, singleton: false)
195195
void_class.class_eval(&) if block_given?
196196

197-
inherit.const_set(:Void, void_class)
197+
inherit.const_set(class_name, void_class)
198198
inherit.define_singleton_method(:void) { |attributes = {}| void_class.new(attributes) }
199199
end
200200

0 commit comments

Comments
 (0)