Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-postgres-hstore/activerecord.rb

Overview

Adds methods for deleting keys in your hstore columns

Class Method Summary collapse

Instance Method Summary collapse

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');


11
12
13
14
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 11

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');


20
21
22
23
24
25
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 20

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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 74

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 value && ((self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time))) || value.is_a?(Hash) || value.is_a?(Array))
          if self.class.columns_hash[name].type == :hstore
            value = value.to_hstore # Done!
          else
            value = value.to_yaml
          end
        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.



31
32
33
34
35
36
37
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 31

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.



43
44
45
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 43

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.



51
52
53
54
55
56
57
58
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 51

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.



64
65
66
67
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 64

def destroy_keys! attribute, *keys
  raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s)
  destroy_keys(attribute, *keys).save
end