Created
February 1, 2013 13:41
-
-
Save zsalzbank/4691376 to your computer and use it in GitHub Desktop.
what I am using to allow foreign keys for associations to be allowed in an hstore for use with https://github.com/engageis/activerecord-postgres-hstore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HstoreAccessor | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def uses_hstore? | |
false | |
end | |
def valid_for_hstore?(hstore_attribute, key) | |
self.hstore_attributes[hstore_attribute].include? key if self.hstore_attributes[hstore_attribute] | |
end | |
def hstore_accessor(hstore_attribute, *keys) | |
if !uses_hstore? | |
class_attribute :hstore_attributes, :belongs_to_hstore_attributes | |
self.hstore_attributes = {} | |
self.belongs_to_hstore_attributes = [] | |
class_eval do | |
def self.uses_hstore? | |
true | |
end | |
def [](attr_name) | |
if self.belongs_to_hstore_attributes.include? attr_name.to_sym | |
send("#{attr_name}") | |
else | |
super(attr_name) | |
end | |
end | |
def []=(attr_name, value) | |
if self.belongs_to_hstore_attributes.include? attr_name.to_sym | |
send("#{attr_name}=", value) | |
else | |
super(attr_name, value) | |
end | |
end | |
end | |
end | |
if !self.hstore_attributes.has_key? hstore_attribute.to_sym | |
self.hstore_attributes[hstore_attribute.to_sym] = [] | |
end | |
Array(keys).flatten.each do |key| | |
self.hstore_attributes[hstore_attribute.to_sym] |= [key] | |
attr_writer key.to_sym | |
define_method("#{key}=") do |value| | |
send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value)) | |
send("#{hstore_attribute}_will_change!") | |
end | |
define_method(key) do | |
send(hstore_attribute) && send(hstore_attribute)[key.to_s] | |
end | |
end | |
end | |
def belongs_to(name, options={}) | |
hstore = options.delete :hstore | |
f_key = options[:foreign_key] || "#{name.to_s}_id" | |
belongs_to_hstore hstore.to_s, f_key.to_sym if hstore | |
relation = super(name, options) | |
end | |
def belongs_to_hstore(hstore_attribute, *keys) | |
hstore_accessor(hstore_attribute, *keys) | |
self.belongs_to_hstore_attributes |= Array(keys) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment