Sha256: 22fd083a6842e9e5e428559d3b293d64d2230d5103401afa783e9a71e5e8b760

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

module AssignableValues
  module ActiveRecord
    module Restriction
      class BelongsToAssociation < Base

        private

        def association_class
          model.reflect_on_association(property).klass
        end

        def association_id_method
          "#{property}_id"
        end

        def error_property
          association_id_method
        end

        def association_id(record)
          record.send(association_id_method)
        end

        def previously_saved_value(record)
          if old_id = record.send("#{association_id_method}_was")
            if old_id == association_id(record)
              current_value(record) # no need to query the database if nothing changed
            else
              association_class.find_by_id(old_id)
            end
          end
        end

        def current_value(record)
          value = record.send(property)
          value = record.send(property, true) if (value && value.id) != association_id(record)
          value
        end

      end
    end
  end
end


Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
assignable_values-0.8.0 lib/assignable_values/active_record/restriction/belongs_to_association.rb
assignable_values-0.7.2 lib/assignable_values/active_record/restriction/belongs_to_association.rb
assignable_values-0.7.1 lib/assignable_values/active_record/restriction/belongs_to_association.rb
assignable_values-0.6.1 lib/assignable_values/active_record/restriction/belongs_to_association.rb
assignable_values-0.6.0 lib/assignable_values/active_record/restriction/belongs_to_association.rb
assignable_values-0.5.3 lib/assignable_values/active_record/restriction/belongs_to_association.rb