Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/activerecord_postgres_hstore_core/activerecord.rb
Overview
Adds methods for deleting keys in your hstore columns
Class Method Summary collapse
-
.delete_key(attribute, key) ⇒ Object
Deletes all keys from a specific column in a model.
-
.delete_keys(attribute, *keys) ⇒ Object
Deletes many keys from a specific column in a model.
Instance Method Summary collapse
-
#arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) ⇒ Object
This method is replaced for Rails 3 compatibility.
-
#destroy_key(attribute, key) ⇒ Object
Deletes a key in a record.
-
#destroy_key!(attribute, key) ⇒ Object
Deletes a key in a record.
-
#destroy_keys(attribute, *keys) ⇒ Object
Deletes many keys in a record.
-
#destroy_keys!(attribute, *keys) ⇒ Object
Deletes many keys in a record.
Class Method Details
.delete_key(attribute, key) ⇒ Object
Deletes all keys from a specific column in a model. E.g.
Person.delete_key(:info, :father)
The SQL generated will be:
UPDATE "people" SET "info" = delete("info",'father');
12 13 14 15 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 12 def self.delete_key attribute, key raise "invalid attribute #{attribute}" unless column_names.include?(attribute.to_s) update_all([%(#{attribute} = delete("#{attribute}",?)),key]) end |
.delete_keys(attribute, *keys) ⇒ Object
Deletes many keys from a specific column in a model. E.g.
Person.delete_key(:info, :father, :mother)
The SQL generated will be:
UPDATE "people" SET "info" = delete(delete("info",'father'),'mother');
21 22 23 24 25 26 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 21 def self.delete_keys attribute, *keys raise "invalid attribute #{attribute}" unless column_names.include?(attribute.to_s) delete_str = "delete(#{attribute},?)" (keys.count-1).times{ delete_str = "delete(#{delete_str},?)" } update_all(["#{attribute} = #{delete_str}", *keys]) end |
Instance Method Details
#arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) ⇒ Object
This method is replaced for Rails 3 compatibility. All I do is add the condition when the field is a hash that converts the value to hstore format. IMHO this should be delegated to the column, so it won’t be necessary to rewrite all this method.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 75 def arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) attrs = {} attribute_names.each do |name| if (column = column_for_attribute(name)) && (include_primary_key || !column.primary) if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) value = read_attribute(name) if self.class.columns_hash[name].type == :hstore && value && value.is_a?(Hash) value = value.to_hstore # Done! elsif value && self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time) || value.is_a?(Hash) || value.is_a?(Array)) value = value.to_yaml end attrs[self.class.arel_table[name]] = value end end end attrs end |
#destroy_key(attribute, key) ⇒ Object
Deletes a key in a record. E.g.
witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_key(:info, :father)
It does not save the record, so you’ll have to do it.
32 33 34 35 36 37 38 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 32 def destroy_key attribute, key raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s) new_value = send(attribute) new_value.delete(key.to_s) send("#{attribute}=", new_value) self end |
#destroy_key!(attribute, key) ⇒ Object
Deletes a key in a record. E.g.
witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_key(:info, :father)
It does save the record.
44 45 46 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 44 def destroy_key! attribute, key destroy_key(attribute, key).save end |
#destroy_keys(attribute, *keys) ⇒ Object
Deletes many keys in a record. E.g.
witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_keys(:info, :father, :mother)
It does not save the record, so you’ll have to do it.
52 53 54 55 56 57 58 59 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 52 def destroy_keys attribute, *keys for key in keys new_value = send(attribute) new_value.delete(key.to_s) send("#{attribute}=", new_value) end self end |
#destroy_keys!(attribute, *keys) ⇒ Object
Deletes many keys in a record. E.g.
witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_keys!(:info, :father, :mother)
It does save the record.
65 66 67 68 |
# File 'lib/activerecord_postgres_hstore_core/activerecord.rb', line 65 def destroy_keys! attribute, *keys raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s) destroy_keys(attribute, *keys).save end |