Created
July 27, 2012 16:52
-
-
Save jonsgreen/3189117 to your computer and use it in GitHub Desktop.
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 HstoreSerializer | |
extend ActiveSupport::Concern | |
module ClassMethods def hstore_array(*attributes) | |
attributes.each do |attr| | |
define_method "#{attr}=" do |values| | |
self[attr] = convert_to_hash(values) | |
end | |
define_method attr do | |
self[attr].try(:keys) || [] | |
end | |
end | |
end | |
end | |
private | |
def convert_to_hash(values) | |
values.select(&:present?).inject({}) {|hash, s| hash.merge({ s => true}) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment