Sha256: 435b4e1caf2011f3d3fb9f18548f178df686d588aa4699304005fa939bbc1c56

Contents?: true

Size: 1.32 KB

Versions: 81

Compression:

Stored size: 1.32 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[:base] = "bad model name"
        return
      end

      [
        entry.cleaner_function,
        entry.validation_function,
        entry.preprocess_function,
      ].each { |func|
        entry.errors[: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

  delorean_fn :lookup, sig: 1 do
    |name|
    self.find_by_name(name)
  end

  delorean_fn :get_all, sig: 0 do
    self.all
  end
end

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
marty-1.2.9 app/models/marty/import_type.rb
marty-1.2.8 app/models/marty/import_type.rb
marty-1.2.7 app/models/marty/import_type.rb
marty-1.2.6 app/models/marty/import_type.rb
marty-1.2.5 app/models/marty/import_type.rb
marty-1.2.4 app/models/marty/import_type.rb
marty-1.2.3 app/models/marty/import_type.rb
marty-1.2.2 app/models/marty/import_type.rb
marty-1.2.1 app/models/marty/import_type.rb
marty-1.2.0 app/models/marty/import_type.rb
marty-1.1.9 app/models/marty/import_type.rb
marty-1.1.8 app/models/marty/import_type.rb
marty-1.1.7 app/models/marty/import_type.rb
marty-1.1.6 app/models/marty/import_type.rb
marty-1.1.5 app/models/marty/import_type.rb
marty-1.1.4 app/models/marty/import_type.rb
marty-1.1.3 app/models/marty/import_type.rb
marty-1.1.2 app/models/marty/import_type.rb
marty-1.1.1 app/models/marty/import_type.rb
marty-1.0.54 app/models/marty/import_type.rb