Sha256: 02de943bb24a0806d69d95517afb974656b5e629ff1ed3fedaa97e33f4914f0e

Contents?: true

Size: 985 Bytes

Versions: 2

Compression:

Stored size: 985 Bytes

Contents

module ModelMacros
  # Create a new organization model
  def spawn_organization_model(klass_name = 'Organization', &block)
    spawn_model klass_name, ActiveRecord::Base do
      acts_as_organization
      instance_exec(&block) if block
    end
  end

  # Create a new user model
  def spawn_user_model(klass_name = 'User', &block)
    spawn_model klass_name, ActiveRecord::Base do
      acts_as_user
      instance_exec(&block) if block
    end
  end

  def spawn_membership_model(klass_name = 'Membership', &block)
    spawn_model 'Membership', ActiveRecord::Base do
      acts_as_membership
      instance_exec(&block) if block
    end
  end

  protected

  # Create a new model class
  def spawn_model(klass_name, parent_klass, &block)
    Object.instance_eval { remove_const klass_name } if Object.const_defined?(klass_name)
    @spawned_models << Object.const_set(klass_name, Class.new(parent_klass))
    Object.const_get(klass_name).class_eval(&block) if block_given?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
camaraderie-0.2.1 spec/support/macros/model_macros.rb
camaraderie-0.2 spec/support/macros/model_macros.rb