Skip to content

Commit

Permalink
Simplify backend resetters
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Apr 24, 2018
1 parent 2282934 commit 77696e2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/mobility/active_model/backend_resetter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module ActiveModel
class BackendResetter < Mobility::BackendResetter

# (see Mobility::BackendResetter#initialize)
def initialize(attribute_names)
def initialize(attribute_names, &block)
super

model_reset_method = @model_reset_method
Expand Down
11 changes: 5 additions & 6 deletions lib/mobility/active_record/backend_resetter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ module ActiveRecord
class BackendResetter < Mobility::ActiveModel::BackendResetter

# @param [Class] model_class Class of model to which backend resetter will be applied
def included(model_class)
def initialize(attribute_names, &block)
super

model_reset_method = @model_reset_method

mod = Module.new do
define_method :reload do |*args|
super(*args).tap { instance_eval(&model_reset_method) }
end
define_method :reload do |*args|
super(*args).tap { instance_eval(&model_reset_method) }
end
model_class.include mod
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/mobility/sequel/backend_resetter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ module Sequel
class BackendResetter < Mobility::BackendResetter

# @param [Class] model_class Class of model to which backend resetter will be applied
def included(model_class)
def initialize(attribute_names, &block)
super

model_reset_method = @model_reset_method

mod = Module.new do
define_method :refresh do
super().tap { instance_eval(&model_reset_method) }
end
define_method :refresh do
super().tap { instance_eval(&model_reset_method) }
end
model_class.include mod
end
end
end
Expand Down

0 comments on commit 77696e2

Please sign in to comment.