Sha256: a242bfade1c1ad02b72c7f0992481cbfcd016fb96952e61d8581a9d3c11976b4

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

class DynamicDatum < ActiveRecord::Base
  DYNAMIC_ATTRIBUTE_PREFIX = 'dyn_'
  
  has_dynamic_attributes :dynamic_attribute_prefix => DYNAMIC_ATTRIBUTE_PREFIX, :destroy_dynamic_attribute_for_nil => false

  belongs_to :reference, :polymorphic => true
  belongs_to :created_with_form, :class_name => "DynamicForm"
  belongs_to :updated_with_form, :class_name => "DynamicForm"
  belongs_to :created_by, :class_name => "User"
  belongs_to :updated_by, :class_name => "User"
  
  def dynamic_attributes_without_prefix
    attrs = {}
    self.dynamic_attributes.each do |k,v|
      attrs[k[DYNAMIC_ATTRIBUTE_PREFIX.length..(k.length)]] = v
    end

    attrs
  end

=begin
  # this was used for sorting dynamic sttributes under Ruby 1.8.7 which did not have ordered hashes
  # it is now obsolete under Ruby 1.9  
  # leave it here for reference for the time being in case we find that dynamic attributes are stored in the wrong order in the DB
  # using ordered hashes assumes that dynamic attributes are stored in order in the database
  def sorted_dynamic_attributes(with_prefix=true)    
    if !self.updated_with_form.nil?
      form = self.updated_with_form
    elsif !self.created_with_form.nil?
      form = self.created_with_form
    else
      form = nil
    end
    
    unless form.nil?
      if with_prefix
        keys = form.definition_object.collect{|f| DYNAMIC_ATTRIBUTE_PREFIX + f[:name]}
      else
        keys = form.definition_object.collect{|f| f[:name]}
      end

      sorted = []
      keys.each do |key|
        attribute = {}      
        if with_prefix
          attribute[key] = self.dynamic_attributes[key]
        else
          attribute[key] = self.dynamic_attributes_without_prefix[key]
        end
        
        sorted << attribute
      end
      
      return sorted
    else
      if with_prefix
        return self.dynamic_attributes
      else
        return self.dynamic_attributes_without_prefix
      end
    end
  end
=end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
erp_forms-2.0.2 app/models/dynamic_datum.rb
erp_forms-2.0.1 app/models/dynamic_datum.rb
erp_forms-2.0.0 app/models/dynamic_datum.rb