Module: Offroad::ModelExtensions::CommonInstanceMethods

Defined in:
lib/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#validate_changed_id_columnsObject

:nodoc:#



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/model_extensions.rb', line 146

def validate_changed_id_columns
  changes.each do |colname, arr|
    orig_val = arr[0]
    new_val = arr[1]
    
    raise DataError.new("Cannot change id of offroad-tracked records (orig #{orig_val.inspect}, new #{new_val.inspect}") if colname == self.class.primary_key && orig_val != nil
    
    # FIXME : Use association reflection instead
    next unless colname.end_with? "_id"
    accessor_name = colname[0, colname.size-3]
    next unless respond_to? accessor_name
    obj = send(accessor_name)
    next unless obj
    
    raise DataError.new("Mirrored data cannot hold a foreign key to unmirrored data") unless obj.class.acts_as_offroadable?
    
    if !new_record? and offroad_mode == :group_owned and colname == offroad_parent_assoc.primary_key_name
      # obj is our parent
      # FIXME: What if we can't find orig_val?
      if obj.owning_group != obj.class.find(orig_val).owning_group
        raise DataError.new("Group-owned data cannot be transferred between groups")
      end
    end

    if self.class.offroad_group_data?
      if obj.class.offroad_group_data? && obj.owning_group && obj.owning_group.id != owning_group.id
        raise DataError.new("Invalid #{colname}: Group data cannot hold a foreign key to data owned by another group")
      end
    elsif self.class.offroad_global_data?
      unless obj.class.offroad_global_data?
        raise DataError.new("Invalid #{colname}: Global mirrored data cannot hold a foreign key to group data")
      end
    end
  end
end