Sha256: 116cd4a76e0f55cc9317f47dd209f06cedc1645d083c8f45a537d9a0e84821fc

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module AMEE
  module Rails

    def self.establish_connection(server, username, password)
      # Connect
      $amee = AMEE::Connection.new(server, username, password)
      # Authenticate now to get it out of the way and to check settings
      $amee.authenticate
    end

    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
        return true
      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

2 entries across 2 versions & 1 rubygems

Version Path
Floppy-amee-2.0.18 lib/amee/rails.rb
Floppy-amee-2.0.19 lib/amee/rails.rb