Sha256: 067bc58a41dd4730606e535c27d0b95069cee9a739034facc549391e1433c603

Contents?: true

Size: 998 Bytes

Versions: 2

Compression:

Stored size: 998 Bytes

Contents

module BestBoy
  module Eventable
    extend ActiveSupport::Concern

    module ClassMethods
      def has_a_best_boy
        # meta programming
        #
        #
        include InstanceMethods
        
        # associations
        #
        #
        has_many :best_boy_events, :as => :owner, :dependent => :nullify
        
        # callbacks
        #
        #
        after_create :trigger_create_event
        before_destroy :trigger_destroy_event
      end
    end

    module InstanceMethods
      def trigger_create_event
        create_best_boy_event_with_type "create"
      end

      def trigger_destroy_event
        create_best_boy_event_with_type "destroy"
      end

      def trigger_custom_event type
        create_best_boy_event_with_type(type) if type.present? 
      end

      def create_best_boy_event_with_type type
        best_boy_event = BestBoyEvent.new(:event => type)
        best_boy_event.owner = self
        best_boy_event.save
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
best_boy-0.0.3 lib/best_boy/models/active_record/best_boy/eventable.rb
best_boy-0.0.2 lib/best_boy/models/active_record/best_boy/eventable.rb