-
-
Save dmoulton/2919670 to your computer and use it in GitHub Desktop.
Hstore ActiveRecord::Base method_missing
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
## Hstore Method Missing Extension | |
module HstoreModel | |
def method_missing(method, *args, &block) | |
key = method.to_s.gsub('=','').to_sym | |
sym_options = options.symbolize_keys | |
if !self.class.attribute_methods_generated? | |
self.class.define_attribute_methods | |
if respond_to_without_attributes?(method) | |
send(method, *args, &block) | |
else | |
super | |
end | |
elsif sym_options[key] | |
if method[-1] == "=" #setter | |
self.class.send :define_method, method do | |
binding.pry | |
sym_options[key] = args | |
end | |
else # getter | |
self.class.send :define_method, method do | |
sym_options[key] | |
end | |
end | |
self.send(method, *args, &block) | |
else | |
super | |
end | |
end | |
end | |
## Thing.rb | |
class Thing < ActiveRecord::Base | |
include HstoreModel | |
end | |
# I can now get/set Thing's hstore attributes ('data' is an hstore attribute on Thing) as though they were "native". Yay it works with Formtastic now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment