Skip to content
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

Allow passing custom methods #148

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/seed_dump/dump_methods.rb
Expand Up @@ -70,8 +70,7 @@ def open_io(options)
def write_records_to_io(records, io, options)
options[:exclude] ||= [:id, :created_at, :updated_at]

method = options[:import] ? 'import' : 'create!'
io.write("#{model_for(records)}.#{method}(")
io.write("#{model_for(records)}.#{method_name(options)}(")
if options[:import]
io.write("[#{attribute_names(records, options).map {|name| name.to_sym.inspect}.join(', ')}], ")
end
Expand Down Expand Up @@ -125,5 +124,14 @@ def model_for(records)
end
end

private

def method_name(options)
import, method = options.values_at(:import, :method)
return :import if import
return method if method

:create!
end
end
end
7 changes: 7 additions & 0 deletions spec/dump_methods_spec.rb
Expand Up @@ -127,6 +127,13 @@ def expected_output(include_id = false, id_offset = 0)
end
end

context 'passing a custom method' do
it 'should find_or_create_by when given the method name' do
expected_output = "Sample.find_or_create_by([\n {string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n"
SeedDump.dump(Sample, method: :find_or_create_by).should eq(expected_output)
end
end

context 'activerecord-import' do
it 'should dump in the activerecord-import format when import is true' do
SeedDump.dump(Sample, import: true, exclude: []).should eq <<-RUBY
Expand Down