Sha256: e2df75287fe579ded8749f28d60f469622c43e98d053545d87d5e61118d2eaee

Contents?: true

Size: 1.18 KB

Versions: 23

Compression:

Stored size: 1.18 KB

Contents

class Marty::ImportType < Marty::Base
  class ImportTypeValidator < ActiveModel::Validator
    def validate(entry)
      klass = entry.get_model_class

      unless klass.is_a?(Class) && klass < ActiveRecord::Base
        entry.errors.add :base, 'bad model name'
        return
      end

      [
        entry.cleaner_function,
        entry.validation_function,
        entry.preprocess_function,
      ].each do |func|
        entry.errors.add(:base, "unknown class method #{func}") if
        func && !klass.respond_to?(func.to_sym)
      end
    end
  end

  before_validation do
    # Fix issue with blank strings in popup edit form or grid
    # being interpreted as a function
    self.cleaner_function = nil if cleaner_function.blank?
    self.validation_function = nil if validation_function.blank?
    self.preprocess_function = nil if preprocess_function.blank?
  end

  belongs_to :role

  validates_presence_of :name, :db_model_name, :role_id
  validates_uniqueness_of :name
  validates_with ImportTypeValidator

  def get_model_class
    db_model_name.constantize if db_model_name
  end

  def allow_import?
    Mcfly.whodunnit && Mcfly.whodunnit.roles.pluck(:id).include?(role_id)
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
marty-2.9.3 app/models/marty/import_type.rb
marty-2.9.2 app/models/marty/import_type.rb
marty-2.9.1 app/models/marty/import_type.rb
marty-2.8.0 app/models/marty/import_type.rb
marty-2.7.3 app/models/marty/import_type.rb
marty-2.7.2 app/models/marty/import_type.rb
marty-2.7.1 app/models/marty/import_type.rb
marty-2.7.0 app/models/marty/import_type.rb
marty-2.6.8 app/models/marty/import_type.rb
marty-2.6.7 app/models/marty/import_type.rb
marty-2.6.6 app/models/marty/import_type.rb
marty-2.6.5 app/models/marty/import_type.rb
marty-2.6.4 app/models/marty/import_type.rb
marty-2.6.3 app/models/marty/import_type.rb
marty-2.6.2 app/models/marty/import_type.rb
marty-2.6.1 app/models/marty/import_type.rb
marty-2.6.0 app/models/marty/import_type.rb
marty-2.5.9 app/models/marty/import_type.rb
marty-2.5.8 app/models/marty/import_type.rb
marty-2.5.7 app/models/marty/import_type.rb