Sha256: b7d69f0933da539ffc3886ed5204e3e92445749274a6d1f40928ef20ec3dbec3

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

module RussianPhone
  module ActiveRecord
    extend ActiveSupport::Concern

    module ClassMethods
      def russian_phone(field, *args)
        name = field.to_s

        options = args.extract_options! || {}
        instance_variable_set "@#{name}_phone_options", options

        if options[:validate]
          validates_with(RussianPhone::FormatValidator, fields: [name])
        end

        if options[:required]
          validates_with(RussianPhone::PresenceValidator, fields: [name])
        end

        define_method name do
          options = self.class.instance_variable_get("@#{name}_phone_options")
          RussianPhone::Number.new(read_attribute(name), options)
        end
        define_method "#{name}=" do |value|
          instance_variable_set("@#{name}_phone_before_type_cast", value)
          options = self.class.instance_variable_get("@#{name}_phone_options")
          self[name] = RussianPhone::Number.new(value, options).mongoize
        end
        define_method "#{name}_phone_before_type_cast" do
          instance_variable_get "@#{name}_phone_before_type_cast"
        end
        after_save do
          instance_variable_set "@#{name}_phone_before_type_cast", nil
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
russian_phone-0.8.0 lib/russian_phone/active_record.rb
russian_phone-0.7.0 lib/russian_phone/active_record.rb
russian_phone-0.6.1 lib/russian_phone/active_record.rb
russian_phone-0.6.0 lib/russian_phone/active_record.rb
russian_phone-0.5.3 lib/russian_phone/active_record.rb
russian_phone-0.5.2 lib/russian_phone/active_record.rb
russian_phone-0.5.1 lib/russian_phone/active_record.rb
russian_phone-0.5.0 lib/russian_phone/active_record.rb