Sha256: 9ffc8e8e1a8132897f8a9a3bfab97c2e0f7e8e47d144d43e30ed5118a3614068

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

require 'forwardable'

module Locomotive
  module Steam
    module Models
      module Concerns

        module Validation

          def errors
            @errors ||= Errors.new(self)
          end

          def valid?
            true
          end

          class Errors

            include Enumerable
            extend Forwardable

            attr_accessor :messages

            def_delegators :@messages, :[], :clear, :empty?, :each, :size

            alias_method :blank?, :empty?

            def initialize(base)
              @base     = base
              @messages = HashWithIndifferentAccess.new({})
            end

            def add_on_blank(attribute)
              value = @base.send(attribute)
              add(attribute, :blank) if value.blank?
            end

            def add(attribute, message)
              (@messages[attribute] ||= []) << generate_message(message)
            end

            def generate_message(message)
              case message
              when :blank, :unique then I18n.t(message, scope: 'errors.messages')
              else
                message
              end
            end

          end

        end

      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.beta.3 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.beta.2 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.beta.1 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.alpha.3 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.alpha.2 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.alpha.1 lib/locomotive/steam/models/concerns/validation.rb
locomotivecms_steam-1.0.0.pre.alpha lib/locomotive/steam/models/concerns/validation.rb