Skip to content

Commit

Permalink
using primary_key instead of literal 'id' so custom primary key works
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Sep 2, 2008
1 parent 8d2b625 commit 03bab61
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ Features
- get single table inheritance working.

Possible
- iterate through array instead of selecting at random to ensure all values are chosen
- if hash is passed, apply given value that number of times: { 'foo' => 3, 'bar' => 2 }
- randomly fill every column if no block is passed to populate
- add random_foo method to record for randomly generating content

Expand Down
7 changes: 4 additions & 3 deletions lib/populator/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Record
# * <tt>created_on</tt> - defaults to current date
# * <tt>updated_on</tt> - defaults to current date
def initialize(model_class, id)
@attributes = { :id => id }
@attributes = { model_class.primary_key.to_sym => id }
@columns = model_class.column_names
@columns.each do |column|
if column == 'created_at' || column == 'updated_at'
Expand Down Expand Up @@ -39,9 +39,10 @@ def attribute_values

def method_missing(sym, *args, &block)
name = sym.to_s
if @columns.include?(name.sub('=', ''))
name_without_equal = name.sub('=', '')
if @columns.include?(name_without_equal) || @attributes.has_key?(name_without_equal.to_sym)
if name.include? '='
@attributes[name.sub('=', '').to_sym] = Populator.interpret_value(args.first)
@attributes[name_without_equal.to_sym] = Populator.interpret_value(args.first)
else
@attributes[sym]
end
Expand Down
5 changes: 5 additions & 0 deletions spec/populator/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@
record.created_on.should == Date.today
record.updated_on.should == Date.today
end

it "should use custom primary_key for auto-increment if specified" do
Product.stubs(:primary_key).returns('foo')
Populator::Record.new(Product, 123).foo.should == 123
end
end

0 comments on commit 03bab61

Please sign in to comment.