Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix Row#cell nil assignment bug #8

Merged
merged 1 commit into from

3 participants

@paulspringett

Fixed bug where passed a nil value to Row#cell would be treated as though
there was no second value

@travisbot

This pull request passes (merged cd70ded into 13b138b).

@paulspringett paulspringett merged commit 63813cc into from
@brendon

Hey just had a thought, would this not still fail if the model has a column (say building_name) but we're also calculating that manually, and that calculation returns nil. This would mistakenly return the model's building_name instead of an empty string.

@paulspringett

@brendon I'm not going to take this issue any further.

The main use case for CSV Shaper is working with models. If you want to add cells manually you can create a row without a model using

csv.row do |csv|
  # no model issues here
end

Thanks you your help.

@brendon

Hi Paul, in my case I'm wanting to include columns from an associated model (sometimes the associated models column evaluates to nil). I guess provided I make sure that I don't chose a column name that matches an existing method on the model then all will be well :) I still think that using a block to calculate a manual cell value is more explicit and has less confusing edge cases. Though this would break backward compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 18, 2012
  1. Fixed bug where passed a nil value to Row#cell would be treated

    authored
    as though there was no second value
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 1 deletion.
  1. +1 −1  lib/csv_shaper/row.rb
  2. +20 −0 spec/row_spec.rb
View
2  lib/csv_shaper/row.rb
@@ -60,7 +60,7 @@ def cells(*args)
def cell(column, value = nil)
column = column.to_sym
- if @model && value.nil?
+ if @model && @model.respond_to?(column) && value.nil?
@cells[column] = @model.send(column)
else
@cells[column] = value
View
20 spec/row_spec.rb
@@ -22,4 +22,24 @@
csv.should be_kind_of(CsvShaper::Row)
}
end
+
+ describe "cells" do
+ it "should send parse an attribute of the model" do
+ row = CsvShaper::Row.new(user, :gender)
+ row.cell :name
+ row.cells.should eq({ name: 'Paul', gender: 'Male' })
+ end
+
+ it "should send assign an unrelated value" do
+ row = CsvShaper::Row.new(user, :gender)
+ row.cell :foo, 'bar'
+ row.cells.should eq({ foo: 'bar', gender: 'Male' })
+ end
+
+ it "ignore nil values passed" do
+ row = CsvShaper::Row.new(user, :gender)
+ row.cell :foo, nil
+ row.cells.should eq({ foo: nil, gender: 'Male' })
+ end
+ end
end
Something went wrong with that request. Please try again.