Sha256: 73035d32ec0e1c1be3d320960af94904e117653dea3949006b3ad4e39c3d160f

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 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)
      rescue
        puts "Couldn't remove profile #{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

4 entries across 4 versions & 1 rubygems

Version Path
Floppy-amee-2.0.20 lib/amee/rails.rb
Floppy-amee-2.0.21 lib/amee/rails.rb
Floppy-amee-2.0.22 lib/amee/rails.rb
Floppy-amee-2.0.24 lib/amee/rails.rb