Sha256: ce436919fbdc7ee0fb27dd2dc168974f13fb69818ac9f26cc4641e302e599d7b

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module ActiveAdminAssociations
  class AssociationConfig
    include Enumerable
    
    class Association
      attr_reader :name
      attr_reader :fields

      def initialize(name, fields = [], &block)
        @name = name.to_sym
        @fields = fields
        instance_exec(&block) if block_given?
      end
      
      def field(name)
        @fields << name
      end
      
      def fields(*names)
        @fields += names
      end
    end
    
    attr_reader :association_configs
    delegate :each, :to => :association_configs

    def initialize(&block)
      @association_configs = []
      instance_exec(&block)
    end
    
    def blank?
      @association_configs.blank?
    end
    
    def [](association_name)
      @association_configs.detect {|a| a.name == association_name.to_sym }
    end
    
    def association(name, fields = [], &block)
      @association_configs << Association.new(name, fields, &block)
    end
    
    def associations(*names)
      names.each do |name|
        @association_configs << Association.new(name)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
activeadmin_associations-0.1.3 lib/active_admin_associations/association_config.rb
aa_associations-0.1.2 lib/active_admin_associations/association_config.rb
aa_associations-0.1.1 lib/active_admin_associations/association_config.rb
aa_associations-0.1.0 lib/active_admin_associations/association_config.rb