Sha256: 9207ca3dc5c8b8d22a82e7b9734da10a2a0c27dab64da9cb1588b99c6dcb716f

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module HatTrick
  module ModelMethods
    extend ActiveSupport::Concern
    mattr_accessor :validation_groups

    attr_accessor :_dummy # so the dummy field will have something to set

    included do
      alias_method_chain :perform_validations, :hat_trick if instance_methods.include?(:perform_validations)
      alias_method_chain :as_json, :model_name if instance_methods.include?(:as_json)
    end

    def self.set_current_validation_group_for(klass, validation_group_name)
      self.validation_groups ||= {}
      validation_groups[klass.to_s.underscore] = validation_group_name
    end

    def self.current_validation_group_for(klass)
      return nil unless validation_groups
      validation_groups[klass.to_s.underscore]
    end

    def perform_validations_with_hat_trick(*args, &block)
      enable_current_validation_group
      perform_validations_without_hat_trick(*args, &block)
    end

    def as_json_with_model_name(*args, &block)
      json = as_json_without_model_name(*args, &block)
      json.merge! :__name__ => self.class.to_s.underscore if json.respond_to?(:merge!)
    end

    private

    # don't call this method 'current_validation_group', it conflicts with
    # the gem
    def current_step_validation_group
      HatTrick::ModelMethods.current_validation_group_for(self.class)
    end

    def enable_current_validation_group
      validation_group = current_step_validation_group
      enable_validation_group validation_group if validation_group
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hat-trick-0.1.2 lib/hat_trick/model_methods.rb
hat-trick-0.1.1 lib/hat_trick/model_methods.rb
hat-trick-0.1.0 lib/hat_trick/model_methods.rb