Module: ActiveRecord::Base::CompositeInstanceMethods
- Defined in:
- lib/composite_primary_keys/base.rb
Instance Method Summary collapse
- #==(comparison_object) ⇒ Object
- #can_change_primary_key_values? ⇒ Boolean
-
#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
-
#to_key ⇒ Object
Returns this record’s primary keys values in an Array if any value is available.
- #to_param ⇒ Object
Instance Method Details
#==(comparison_object) ⇒ Object
116 117 118 119 |
# File 'lib/composite_primary_keys/base.rb', line 116 def ==(comparison_object) return true if equal? comparison_object ids.is_a?(Array) ? super(comparison_object) && ids.all? {|id| !id.nil?} : super(comparison_object) end |
#can_change_primary_key_values? ⇒ Boolean
121 122 123 |
# File 'lib/composite_primary_keys/base.rb', line 121 def can_change_primary_key_values? false 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.
87 88 89 90 |
# File 'lib/composite_primary_keys/base.rb', line 87 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.
107 108 109 110 111 112 113 114 |
# File 'lib/composite_primary_keys/base.rb', line 107 def id=(ids) ids = CompositePrimaryKeys::CompositeKeys.parse(ids) unless ids.length == self.class.primary_keys.length raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids" end [self.class.primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)} id end |
#id_before_type_cast ⇒ Object
100 101 102 103 104 |
# File 'lib/composite_primary_keys/base.rb', line 100 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
93 94 95 96 97 98 |
# File 'lib/composite_primary_keys/base.rb', line 93 def ids_hash self.class.primary_key.zip(ids).inject(Hash.new) do |hash, (key, value)| hash[key] = value hash end end |
#to_key ⇒ Object
Returns this record’s primary keys values in an Array if any value is available
127 128 129 |
# File 'lib/composite_primary_keys/base.rb', line 127 def to_key ids.to_a if !ids.compact.empty? # XXX Maybe use primary_keys with send instead of ids end |
#to_param ⇒ Object
131 132 133 |
# File 'lib/composite_primary_keys/base.rb', line 131 def to_param persisted? ? to_key.join(CompositePrimaryKeys::ID_SEP) : nil end |