Skip to content

Commit

Permalink
[changeset] fix binding mapping functions in Update changesets
Browse files Browse the repository at this point in the history
  • Loading branch information
timriley authored and flash-gordon committed May 7, 2020
1 parent d2d88eb commit 4b7d7a6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion changeset/lib/rom/changeset/pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def [](*args)

def bind(context)
if processor.is_a?(Proc)
new(self.class[-> *args { context.instance_exec(*args, &processor) }])
bound_processor = self[-> *args { context.instance_exec(*args, &processor) }]
bound_diff_processor = self[-> *args { context.instance_exec(*args, &diff_processor) }]

new(bound_processor, diff_processor: bound_diff_processor)
else
self
end
Expand Down
49 changes: 49 additions & 0 deletions changeset/spec/integration/changeset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ class User < Dry::Struct
expect(result.updated_at).to be_instance_of(Time)
end

it 'preprocesses data using map blocks in a custom class' do
changeset_class = Class.new(ROM::Changeset::Create) do
map do |tuple|
extend_tuple(tuple)
end

private

def extend_tuple(tuple)
tuple.merge(title: tuple[:title] + ", yes really")
end
end

changeset = books.changeset(changeset_class, title: 'rom-rb is awesome')
command = books.command(:create)
result = command.(changeset)

expect(result.id).to_not be(nil)
expect(result.title).to eql('rom-rb is awesome, yes really')

result = books.changeset(changeset_class, title: 'rom-rb is awesome').commit

expect(result.id).to_not be(nil)
expect(result.title).to eql('rom-rb is awesome, yes really')
end

it 'preserves relation mappers with create' do
changeset = users.map_with(:user).changeset(:create, name: 'Joe Dane')

Expand Down Expand Up @@ -160,6 +186,29 @@ class User < Dry::Struct
expect(result.updated_at).to be_instance_of(Time)
end

it 'preprocesses data using map blocks in a custom class' do
book = books.command(:create).call(title: 'rom-rb is awesome')

changeset_class = Class.new(ROM::Changeset::Update) do
map do |tuple|
extend_tuple(tuple)
end

private

def extend_tuple(tuple)
tuple.merge(title: tuple[:title] + ', yes really')
end
end

result = books.by_pk(book.id)
.changeset(changeset_class, title: 'rom-rb is awesome for real')
.commit

expect(result.id).to be(book.id)
expect(result.title).to eql('rom-rb is awesome for real, yes really')
end

it 'works with command plugins' do
configuration.commands(:books) do
define(:update) do
Expand Down

0 comments on commit 4b7d7a6

Please sign in to comment.