Sha256: b1c6e2df2598b7a13941665cc893462380eaa0cf0bf07d939b0d1ce2f203e3ab

Contents?: true

Size: 843 Bytes

Versions: 3

Compression:

Stored size: 843 Bytes

Contents

module CustomFields
  module ProxyClassEnabler

    extend ActiveSupport::Concern

    included do

      cattr_accessor :klass_with_custom_fields

      def self.to_klass_with_custom_fields(fields)
        return klass_with_custom_fields unless klass_with_custom_fields.nil?

        klass = Class.new(self)
        klass.class_eval <<-EOF
          cattr_accessor :custom_fields, :_parent, :association_name

          def self.model_name
            @_model_name ||= ActiveModel::Name.new(self.superclass)
          end

          def custom_fields
            self.class.custom_fields
          end

          def self.hereditary?
            false
          end
        EOF

        klass.custom_fields = fields

        [*fields].each { |field| field.apply(klass) }

        klass_with_custom_fields = klass
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
custom_fields-1.0.0.beta2 lib/custom_fields/proxy_class_enabler.rb
custom_fields-1.0.0.beta lib/custom_fields/proxy_class_enabler.rb
custom_fields-0.0.0.1 lib/custom_fields/proxy_class_enabler.rb