lib/ripple/callbacks.rb in ripple-0.8.0.beta2 vs lib/ripple/callbacks.rb in ripple-0.8.0
- old
+ new
@@ -12,38 +12,43 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require 'ripple'
module Ripple
+ # Adds lifecycle callbacks to {Ripple::Document} models, in the typical
+ # ActiveModel fashion.
module Callbacks
extend ActiveSupport::Concern
included do
extend ActiveModel::Callbacks
define_model_callbacks :create, :update, :save, :destroy
define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name]
end
module ClassMethods
+ # Defines a callback to be run before validations.
def before_validation(*args, &block)
options = args.last
if options.is_a?(Hash) && options[:on]
options[:if] = Array(options[:if])
options[:if] << "@_on_validate == :#{options[:on]}"
end
set_callback(:validation, :before, *args, &block)
end
+ # Defines a callback to be run after validations.
def after_validation(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array(options[:if])
options[:if] << "!halted && value != false"
options[:if] << "@_on_validate == :#{options[:on]}" if options[:on]
set_callback(:validation, :after, *(args << options), &block)
end
end
+ # @private
module InstanceMethods
# @private
def save(*args, &block)
state = new? ? :create : :update
run_callbacks(:save) do