Sha256: 10561713834832279d68100c5901fe96025141ab0309650e491b86a0275a4a46

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module Granite
  module Form
    module Model
      # == Callbacks for Granite::Form::Model lifecycle
      #
      # Provides ActiveModel callbacks support for lifecycle
      # actions.
      #
      #   class Book
      #     include Granite::Form::Model
      #
      #     attribute :id, Integer
      #     attribute :title, String
      #
      #     define_save do
      #       REDIS.set(id, attributes.to_json)
      #     end
      #
      #     define_destroy do
      #       REDIS.del(instance.id)
      #     end
      #
      #     after_initialize :setup_id
      #     before_save :do_something
      #     around_update do |&block|
      #       ...
      #       block.call
      #       ...
      #     end
      #     after_destroy { ... }
      #   end
      #
      module Callbacks
        extend ActiveSupport::Concern

        included do
          extend ActiveModel::Callbacks

          include ActiveModel::Validations::Callbacks
          include Lifecycle
          prepend PrependMethods

          define_model_callbacks :initialize, only: :after
          define_model_callbacks :save, :create, :update, :destroy
        end

        module PrependMethods
          def initialize(*_)
            super
            run_callbacks :initialize
          end

          def save_object(&block)
            run_callbacks(:save) { super(&block) }
          end

          def create_object(&block)
            run_callbacks(:create) { super(&block) }
          end

          def update_object(&block)
            run_callbacks(:update) { super(&block) }
          end

          def destroy_object(&block)
            run_callbacks(:destroy) { super(&block) }
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
granite-form-0.2.0 lib/granite/form/model/callbacks.rb
granite-form-0.1.1 lib/granite/form/model/callbacks.rb
granite-form-0.1.0 lib/granite/form/model/callbacks.rb