Sha256: 48376a069dfc30165f51542dc497204678a05a02ec947a77f7b9c9ae7e9d3219

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

module AMEE
  module Rails

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def has_amee_profile(options = {})
        # Include the instance methods for creation and desctruction
        include InstanceMethods
        # Install callbacks
        before_validation_on_create :amee_create
        alias_method_chain :save, :amee
        before_destroy :amee_destroy
        # Check that this object has an AMEE profile UID when saving
        validates_presence_of :amee_profile        
      end
    end

    module InstanceMethods

      def save_with_amee
        save_without_amee
        amee_save
      end

      def amee_create
        # Create profile
        profile = AMEE::Profile::Profile.create(amee_connection)
        self.amee_profile = profile.uid
      end

      def amee_save
        # This is only here to be overridden
      end

      def amee_destroy
        # Delete profile
        AMEE::Profile::Profile.delete(amee_connection, amee_profile)        
      end

      def amee_connection
        # Should be overridden by code which doesn't use the global AMEE connection
        $amee
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
Floppy-amee-0.4.25 lib/amee/rails.rb
Floppy-amee-0.4.26 lib/amee/rails.rb
Floppy-amee-0.4.27 lib/amee/rails.rb