Module: ActiveRecord::Base::CompositeInstanceMethods
- Defined in:
- lib/composite_primary_keys/base.rb
Instance Method Summary collapse
- #==(comparison_object) ⇒ Object
-
#id ⇒ Object
(also: #ids)
A model instance’s primary keys is always available as model.ids whether you name it the default ‘id’ or set it to something else.
-
#id=(ids) ⇒ Object
Sets the primary ID.
- #id_before_type_cast ⇒ Object
- #ids_hash ⇒ Object
-
#initialize_copy(other) ⇒ Object
Cloned objects have no id assigned and are treated as new records.
-
#quoted_id ⇒ Object
:nodoc:.
Instance Method Details
#==(comparison_object) ⇒ Object
142 143 144 |
# File 'lib/composite_primary_keys/base.rb', line 142 def ==(comparison_object) ids.is_a?(Array) ? super(comparison_object) && ids.all? {|id| id.present?} : super(comparison_object) end |
#id ⇒ Object Also known as: ids
A model instance’s primary keys is always available as model.ids whether you name it the default ‘id’ or set it to something else.
106 107 108 109 |
# File 'lib/composite_primary_keys/base.rb', line 106 def id attr_names = self.class.primary_keys ::CompositePrimaryKeys::CompositeKeys.new(attr_names.map { |attr_name| read_attribute(attr_name) }) end |
#id=(ids) ⇒ Object
Sets the primary ID.
132 133 134 135 136 137 138 139 140 |
# File 'lib/composite_primary_keys/base.rb', line 132 def id=(ids) ids = ids.split(CompositePrimaryKeys::ID_SEP) if ids.is_a?(String) ids.flatten! unless ids.is_a?(Array) and ids.length == self.class.primary_keys.length raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids" end [primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)} id end |
#id_before_type_cast ⇒ Object
119 120 121 122 123 |
# File 'lib/composite_primary_keys/base.rb', line 119 def id_before_type_cast self.class.primary_keys.map do |key| self.send("#{key.to_s}_before_type_cast") end end |
#ids_hash ⇒ Object
112 113 114 115 116 117 |
# File 'lib/composite_primary_keys/base.rb', line 112 def ids_hash self.class.primary_key.zip(ids).inject(Hash.new) do |hash, (key, value)| hash[key] = value hash end end |
#initialize_copy(other) ⇒ Object
Cloned objects have no id assigned and are treated as new records. Note that this is a “shallow” clone as it copies the object’s attributes only, not its associations. The extent of a “deep” clone is application specific and is therefore left to the application to implement according to its need.
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 |
# File 'lib/composite_primary_keys/base.rb', line 149 def initialize_copy(other) # Think the assertion which fails if the after_initialize callback goes at the end of the method is wrong. The # deleted clone method called new which therefore called the after_initialize callback. It then went on to copy # over the attributes. But if it's copying the attributes afterwards then it hasn't finished initializing right? # For example in the test suite the topic model's after_initialize method sets the author_email_address to # [email protected]. I would have thought this would mean that all cloned models would have an author email address # of [email protected]. However the test_clone test method seems to test that this is not the case. As a result the # after_initialize callback has to be run *before* the copying of the atrributes rather than afterwards in order # for all tests to pass. This makes no sense to me. callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast) # CPK #cloned_attributes.delete(self.class.primary_key) self.class.primary_key.each {|key| cloned_attributes.delete(key.to_s)} @attributes = cloned_attributes clear_aggregation_cache @attributes_cache = {} @new_record = true ensure_proper_type if scope = self.class.send(:current_scoped_methods) create_with = scope.scope_for_create create_with.each { |att,value| self.send("#{att}=", value) } if create_with end end |
#quoted_id ⇒ Object
:nodoc:
125 126 127 128 129 |
# File 'lib/composite_primary_keys/base.rb', line 125 def quoted_id #:nodoc: [self.class.primary_keys, ids]. transpose. map {|attr_name,id| quote_value(id, column_for_attribute(attr_name))} end |