Sha256: 94d4ad68669e4dc9a05f36ecfa99274864d42868435896844811923fd267853c

Contents?: true

Size: 1.19 KB

Versions: 42

Compression:

Stored size: 1.19 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 { |func|
        entry.errors.add(:base, "unknown class method #{func}") if
        func && !klass.respond_to?(func.to_sym)
      }
    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 self.cleaner_function.blank?
    self.validation_function = nil if self.validation_function.blank?
    self.preprocess_function = nil if self.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

42 entries across 42 versions & 1 rubygems

Version Path
marty-2.5.2 app/models/marty/import_type.rb
marty-2.5.1 app/models/marty/import_type.rb
marty-2.5.0 app/models/marty/import_type.rb
marty-2.4.9 app/models/marty/import_type.rb
marty-2.4.8 app/models/marty/import_type.rb
marty-2.4.7 app/models/marty/import_type.rb
marty-2.4.6 app/models/marty/import_type.rb
marty-2.4.5 app/models/marty/import_type.rb
marty-2.4.4 app/models/marty/import_type.rb
marty-2.4.3 app/models/marty/import_type.rb
marty-2.4.2 app/models/marty/import_type.rb
marty-2.4.1 app/models/marty/import_type.rb
marty-2.4.0 app/models/marty/import_type.rb
marty-2.3.15 app/models/marty/import_type.rb
marty-2.3.14 app/models/marty/import_type.rb
marty-2.3.13 app/models/marty/import_type.rb
marty-2.3.12 app/models/marty/import_type.rb
marty-2.3.11 app/models/marty/import_type.rb
marty-2.3.10 app/models/marty/import_type.rb
marty-2.3.9 app/models/marty/import_type.rb