Sha256: 6445b1d8d865bd89dc5ea53c30d0599575aea97215af6c0414eda1142ca451e7

Contents?: true

Size: 1.68 KB

Versions: 7

Compression:

Stored size: 1.68 KB

Contents

class DynamicFormModel < ActiveRecord::Base
  attr_protected :created_at, :updated_at
  
  has_many :dynamic_form_documents
  has_many :dynamic_forms, :dependent => :destroy
  after_create  :create_role

  validates_uniqueness_of :model_name

  def create_role
    SecurityRole.create(:description => self.model_name.titleize.pluralize, :internal_identifier => role_iid) if self.role.nil?
  end

  def role_iid
    "dynamic_form_model_#{self.model_name}"
  end

  def role
    SecurityRole.iid(role_iid)
  end

  def self.sunspot_setup_all
    DynamicFormModel.all.each do |m|
      next if m.model_name == 'DynamicFormDocument'
      next unless ActiveRecord::Base.connection.table_exists? m.get_constant.table_name
      m.get_constant.sunspot_setup unless DynamicFormModel.class_exists?(m.model_name)
    end
  end

  def self.get_constant(klass_name)
  	result = nil
  	begin
      result = klass_name.constantize
    rescue
      DynamicFormDocument.declare(klass_name)
      result = klass_name.constantize
    end
  	result
  end

  # checks to see if class name exists as a static model or has already been declared
  # used with sunspot_setup_all
  def self.class_exists?(class_name)
    result = nil
    begin
      klass = Module.const_get(class_name)
      result = (klass.is_a?(Class) ? ((klass.superclass == ActiveRecord::Base or klass.superclass == DynamicFormModel) ? true : nil) : nil)
    rescue NameError
      result = nil
    end
    result
  end

  def self.get_instance(klass_name)
    DynamicFormModel.get_constant(klass_name).new
  end

  def get_constant
    DynamicFormModel.get_constant(self.model_name)
  end

  def get_instance
    DynamicFormModel.get_instance(self.model_name)
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
erp_forms-3.1.2 app/models/dynamic_form_model.rb
erp_forms-3.1.1 app/models/dynamic_form_model.rb
erp_forms-3.1.0 app/models/dynamic_form_model.rb
erp_forms-3.0.2 app/models/dynamic_form_model.rb
erp_forms-3.0.1 app/models/dynamic_form_model.rb
erp_forms-3.0.0 app/models/dynamic_form_model.rb
erp_forms-2.1.0 app/models/dynamic_form_model.rb