Sha256: ad098edc47ad26a600f79aa84f4df2dadb9d7a232be3c8323d2b0f9e6e4f54bc

Contents?: true

Size: 703 Bytes

Versions: 4

Compression:

Stored size: 703 Bytes

Contents

module Mks
  module Edm
    module Defaults
      extend ActiveSupport::Concern
      included do
        before_validation :apply_default_values
      end

      def apply_default_values
        self.class.defaults.each do |key, value|
          next unless self.send(key).nil?
          val = value.respond_to?(:call) ? value.call(self) : value
          self[key] = val
        end
      end

      class_methods do
        def default(attribute, value = nil, &block)
          defaults[attribute] = value
          # Allow the passing of blocks
          defaults[attribute] = block if block_given?
        end

        def defaults
          @defaults ||= {}
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mks_edm-1.0.5 app/models/concerns/mks/edm/defaults.rb
mks_edm-1.0.4 app/models/concerns/mks/edm/defaults.rb
mks_edm-1.0.2 app/models/concerns/mks/edm/defaults.rb
mks_edm-1.0.1 app/models/concerns/mks/edm/defaults.rb