File: active_model_helpers.rb

package info (click to toggle)
ruby-shoulda-matchers 7.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 34,046; sh: 280; makefile: 9
file content (27 lines) | stat: -rw-r--r-- 841 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module UnitTests
  module ActiveModelHelpers
    def self.configure_example_group(example_group)
      example_group.include(self)
    end

    def custom_validation(options = {}, &block)
      attribute_name = options.fetch(:attribute_name, :attr)
      attribute_type = options.fetch(:attribute_type, :integer)
      column_options = options.fetch(:column_options, {})
      attribute_options = { type: attribute_type, options: column_options }

      define_model(:example, attribute_name => attribute_options) do
        validate :custom_validation

        define_method(:custom_validation, &block)
      end.new
    end
    alias record_with_custom_validation custom_validation

    def validating_format(options)
      define_model :example, attr: :string do
        validates_format_of :attr, options
      end.new
    end
  end
end