-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Associations with default attributes #18
Changes from all commits
f99987c
a891340
424682b
99392f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,31 @@ module Stored; end | |
class Stored::Abstract | ||
def initialize(type) | ||
@type = type | ||
@attributes = {} | ||
end | ||
|
||
def with(**attributes) | ||
if block_given? | ||
previous_attributes, @attributes = @attributes, @attributes.merge(attributes) | ||
yield | ||
else | ||
@attributes = attributes | ||
end | ||
ensure | ||
@attributes = previous_attributes if block_given? | ||
end | ||
|
||
def update(id, **attributes) | ||
self.class.define_method(id) { find(id) } | ||
|
||
klass = nil | ||
attributes = @attributes.merge(attributes) | ||
attributes.transform_values! do |value| | ||
if !value.respond_to?(:call) then value else | ||
klass ||= Struct.new(:id, *attributes.keys).new(id, *attributes.values) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably some of the most bananas code we've got now. It works! And we can always find something more performant later. |
||
klass.instance_exec(&value) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
@@ -35,7 +56,7 @@ def find(id) | |
end | ||
|
||
def update(id, **attributes) | ||
super | ||
attributes = super | ||
objects[id] = @type.new(**attributes) | ||
end | ||
|
||
|
@@ -50,7 +71,7 @@ def find(id) | |
end | ||
|
||
def update(id, **attributes) | ||
super | ||
attributes = super | ||
|
||
if record = @type.find_by(id: id.hash) | ||
record.update!(**attributes) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
users.update :kasper, name: "Kasper" | ||
accounts.update :business, name: "Big Business Co." | ||
|
||
users.with name: -> { id.to_s.capitalize }, accounts: [accounts.business] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I'm still kinda stunned that it just worked. But we're just leaning on Active Record's |
||
users.update :kasper | ||
users.update :coworker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall pretty happy with how relatively simple it turned out to implement default attributes. It's been on my wishlist since day one! And something that Fixtures aren't that suited for.