Sha256: 6111563a878829edc08fbc62be9db61f67877d63421c0dbdcafa6b9871469b06

Contents?: true

Size: 839 Bytes

Versions: 2

Compression:

Stored size: 839 Bytes

Contents

module Trax
  module Model
    module Freezable
      include ::Trax::Model::Mixin

      included do
        class_attribute :freezable_fields
        self.freezable_fields = ::ActiveSupport::OrderedOptions.new
      end

      module ClassMethods
        def freezable_by_enum(options = {})
          freezable_fields.merge!(options)
          define_frozen_validators_for_enum(options)
        end

        def define_frozen_validators_for_enum(options)
          self.class_eval do
            options.each_pair do |enum_method, frozen_states|
              validates_with ::FrozenValidator, :if => lambda { |record|
                frozen_states.any?{ |state| state == :"#{record.send(enum_method)}" } && !record.changed.include?("#{enum_method}")
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trax_model-0.0.91 lib/trax/model/freezable.rb
trax_model-0.0.9 lib/trax/model/freezable.rb