Skip to content

Commit

Permalink
Allow default scopes for model validations
Browse files Browse the repository at this point in the history
  • Loading branch information
swhopkins committed Feb 26, 2014
1 parent 2abccfe commit 254c531
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.gem
*.rbc
.bundle
/.idea
.config
.yardoc
Gemfile.lock
Expand Down
3 changes: 2 additions & 1 deletion lib/broken_record/config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module BrokenRecord
module Config
extend self
attr_accessor :classes_to_skip, :before_scan_callbacks
attr_accessor :classes_to_skip, :before_scan_callbacks, :default_scopes
self.before_scan_callbacks = []
self.default_scopes = {}

def before_scan(&block)
self.before_scan_callbacks << block
Expand Down
8 changes: 7 additions & 1 deletion lib/broken_record/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ def validate_model(model)
logger.log_message "Validating model #{model}... ".ljust(70)

begin
model.unscoped.all.each do |r|
if BrokenRecord::Config.default_scopes[model]
model_scope = BrokenRecord::Config.default_scopes[model].call
else
model_scope = model.unscoped
end

model_scope.find_each do |r|
begin
if !r.valid?
message = " Invalid record in #{model} id=#{r.id}."
Expand Down

0 comments on commit 254c531

Please sign in to comment.