Skip to content

Commit

Permalink
Add tests, examples to README, switch unscoped default for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Jun 18, 2024
1 parent 5267e2b commit 6bc83be
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ EvilSeed.configure do |config|

# Or for certain association only
root.limit_associations_size(10, 'forum.questions')

# Limit the depth of associations to be dumped from the root level
# All traverses through has_many, belongs_to, etc are counted
# So forum.subforums.subforums.questions.answers will be 5 levels deep
root.limit_deep(10)
end

# Everything you can pass to +where+ method will work as constraints:
Expand Down Expand Up @@ -95,6 +100,20 @@ EvilSeed.configure do |config|
# This will remove the columns even if the model is not a root node and is
# dumped via an association.
config.ignore_columns("Profile", :name)

# Disable foreign key nullification for records that are not included in the dump
# By default, EvilSeed will nullify foreign keys for records that are not included in the dump
config.dont_nullify = true

# Unscope relations to include soft-deleted records etc
# This is useful when you want to include all records, including those that are hidden by default
# By default, EvilSeed will abide default scope of models
config.unscoped = true

# Verbose mode will print out the progress of the dump to the console along with writing the file
# By default, verbose mode is off
config.verbose = true
config.verbose_sql = true
end
```

Expand Down
2 changes: 1 addition & 1 deletion lib/evil_seed/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize
@record_dumper_class = RecordDumper
@verbose = false
@verbose_sql = false
@unscoped = true
@unscoped = false
@dont_nullify = false
@ignored_columns = Hash.new { |h, k| h[k] = [] }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/evil_seed/configuration/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def exclude(*association_patterns)
@exclusions += association_patterns
end

# Exclude some of associations from the dump
# Include some excluded associations back to the dump
# @param association_patterns Array<String, Regex> Patterns to exclude associated models from dump
def include(*association_patterns)
@inclusions += association_patterns
Expand Down
2 changes: 2 additions & 0 deletions test/db/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Answer < ActiveRecord::Base
has_many :votes, as: :votable

has_many :voters, through: :votes, source: :user

default_scope { where(deleted_at: nil) }
end

class TrackingPixel < ActiveRecord::Base
Expand Down
1 change: 1 addition & 0 deletions test/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def create_schema!
t.boolean :best, default: false
t.text :text
t.references :author
t.datetime :deleted_at
t.timestamps null: false
end
add_foreign_key :answers, :users, column: :author_id, on_delete: :nullify
Expand Down
6 changes: 6 additions & 0 deletions test/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
author: User.find_by!(login: 'bob'),
)

answer = question.answers.create!(
text: 'Oops, I was wrong',
author: User.find_by!(login: 'eva'),
deleted_at: Time.current,
)

answer.votes.create!(user: User.find_by!(login: 'eva'))

question_attrs = %w[first second third fourth fifth].map { |name| { name: name, forum: forums.first } }
Expand Down
18 changes: 18 additions & 0 deletions test/evil_seed/dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_it_dumps_tree_structures_with_foreign_keys
assert_match(/'fourth'/, result)
assert_match(/'fifth'/, result)
assert result.index(/'One'/) < result.index(/'Descendant forum'/)
refute_match(/'Oops, I was wrong'/, result)
end

def test_limits_being_applied
Expand All @@ -59,5 +60,22 @@ def test_limits_being_applied
assert_match(/'fourth'/, result)
refute_match(/'fifth'/, result)
end

def test_it_applies_unscoping_and_inclusions
configuration = EvilSeed::Configuration.new
configuration.root('Forum', name: 'Descendant forum') do |root|
root.include(/forum(\.parent(\.questions(\.answers)?)?)?\z/)
root.exclude(/.\..+/)
end
configuration.unscoped = true

io = StringIO.new
EvilSeed::Dumper.new(configuration).call(io)
result = io.string
File.write(File.join('tmp', "#{__method__}.sql"), result)
assert io.closed?
assert_match(/'Descendant forum'/, result)
assert_match(/'Oops, I was wrong'/, result)
end
end
end

0 comments on commit 6bc83be

Please sign in to comment.