Skip to content

Instantly share code, notes, and snippets.

@hotgazpacho
Last active December 15, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hotgazpacho/5322343 to your computer and use it in GitHub Desktop.
Save hotgazpacho/5322343 to your computer and use it in GitHub Desktop.
module HStoreProperties
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def boolean_h_store_fields_for(property_names)
property_names.each do |key|
attr_accessible key
scope "has_#{key}", lambda { |value| where('properties @> (? => ?)', key, value) }
send :define_method, key do
properties && properties[key].to_bool
end
send :define_method, "#{key}?".to_sym do
send key
end
send :define_method, "#{key}=" do |value|
self.properties = (properties || {}).merge(key => value)
end
end
end
end
end
end
class Pump < Component
include HStoreProperties
boolean_h_store_fields_for %w[variable_speed skid_mounted speed_lock flow_lock press_lock]
end
@jah2488
Copy link

jah2488 commented Apr 5, 2013

For one thing, you've got commas in your space delimitated array. So all of your methods are going to be something like these

> %w[variable_speed, skid_mounted, speed_lock, flow_lock, press_lock]
#=> ["variable_speed,", "skid_mounted,", "speed_lock,", "flow_lock,", "press_lock"]

@hotgazpacho
Copy link
Author

Got it fixed now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment