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

Add option INCLUDE=id #96

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ Exclude `name` and `age` from the dump:
irb(main):004:0> SeedDump.dump(User, exclude: [:name, :age])
```

Include `id` to the dump:
```ruby
irb(main):005:0> SeedDump.dump(User, include: [:created_at, :updated_at])
```

Options are specified as a Hash for the second argument.

In the console, any relation of ActiveRecord rows can be dumped (not individual objects though)
Expand All @@ -118,6 +123,8 @@ Options are common to both the Rake task and the console, except where noted.

`exclude`: Attributes to be excluded from the dump. Pass a comma-separated list to the Rake task (i.e. `name,age`) and an array on the console (i.e. `[:name, :age]`). Default: `[:id, :created_at, :updated_at]`.

`include`: Attributes to be excluded from the default excluded colmuns such as `[:id, :created_at, :updated_at]`. Pass a comma-separated list to the Rake task (i.e. `created_at,updated_at`) and an array on the console (i.e. `[:created_at, :updated_at]`). Default: `[]`.

`file`: Write to the specified output file. The Rake task default is `db/seeds.rb`. The console returns the dump as a string by default.

`import`: If `true`, output will be in the format needed by the [activerecord-import](https://github.com/zdennis/activerecord-import) gem, rather than the default format. Default: `false`.
Expand Down
11 changes: 10 additions & 1 deletion lib/seed_dump/dump_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,18 @@ def open_io(options)
end
end

def write_records_to_io(records, io, options)
def set_inclusions(options)
return unless options[:include]
options[:exclude] = (options[:exclude] - options[:include])
end

def set_exclusions(options)
options[:exclude] ||= [:id, :created_at, :updated_at]
end

def write_records_to_io(records, io, options)
set_exclusions(options)
set_inclusions(options)
method = options[:import] ? 'import' : 'create!'
io.write("#{model_for(records)}.#{method}(")
if options[:import]
Expand Down
1 change: 1 addition & 0 deletions lib/seed_dump/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def dump_using_environment(env = {})
model = model.limit(env['LIMIT'].to_i) if env['LIMIT']

SeedDump.dump(model,
include: (env['INCLUDE'] ? env['INCLUDE'].split(',').map {|i| i.strip.to_sym} : []),
append: append,
batch_size: (env['BATCH_SIZE'] ? env['BATCH_SIZE'].to_i : nil),
exclude: (env['EXCLUDE'] ? env['EXCLUDE'].split(',').map {|e| e.strip.to_sym} : nil),
Expand Down
81 changes: 81 additions & 0 deletions spec/dump_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,87 @@ def expected_output(include_id = false, id_offset = 0)
output + data.join(",\n ") + "\n])\n"
end

describe '::set_inclusions' do
context '"include" is not given' do
let(:options) do
{ exclude: [:id, :created_at, :updated_at] }
end

it 'does not change "options"' do
SeedDump.send(:set_inclusions, options)
options.should eq exclude: [:id, :created_at, :updated_at]
end
end

context '"include" values is empty' do
let(:options) do
{ exclude: [:id, :created_at, :updated_at], include: [] }
end

it 'does not change "options' do
SeedDump.send(:set_inclusions, options)
options.should eq(exclude: [:id, :created_at, :updated_at], include: [])
end
end

context '"include" values are on the "exclude" values list' do
let(:options) do
{ exclude: [:id, :created_at, :updated_at],
include: [:id, :created_at] }
end

it 'returns value not on the "include" values list' do
SeedDump.send(:set_inclusions, options)
options.should eq(
exclude: [:updated_at],
include: [:id, :created_at])
end
end

context '"include" values are not on the "exclude" values list' do
let(:options) do
{ exclude: [:id, :created_at, :updated_at],
include: [:hoge, :fuga] }
end

it 'does not change "option"' do
SeedDump.send(:set_inclusions, options)
options.should eq(
exclude: [:id, :created_at, :updated_at],
include: [:hoge, :fuga])
end
end
end

describe '::set_exclusions' do
context 'exclude key has no value' do
let(:options) { { exclude: nil } }

it '"exclude" values is default' do
SeedDump.send(:set_exclusions, options)
options.should eq(exclude: [:id, :created_at, :updated_at])
end
end

context '"exclude" key has only one value' do
let(:options) { { exclude: [:foo] } }

it 'returns given options' do
SeedDump.send(:set_exclusions, options)
options.should eq(exclude: [:foo])
end
end

context '"exclude" key has more than one value' do
let(:options) { { exclude: [:foo, :bar] } }

it 'returns given options' do
SeedDump.send(:set_exclusions, options)
options.should eq(exclude: [:foo, :bar])
end
end
end

describe '.dump' do
before do
Rails.application.eager_load!
Expand Down