Skip to content

Instantly share code, notes, and snippets.

@jah2488
Forked from hotgazpacho/h_store_properties.rb
Last active December 15, 2015 20:59
Show Gist options
  • Save jah2488/5322627 to your computer and use it in GitHub Desktop.
Save jah2488/5322627 to your computer and use it in GitHub Desktop.
I had to comment out the scope and attr_accessible as I did this sans rails, but it defines the methods correctly now on the instances of the Pump class.
module HStoreProperties
extend self
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) }
define_method(key) do
properties && properties[key].to_bool
end
define_method("#{key}?") do
alias_method key, "#{key}?".to_s
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
end
end
class Pump
extend HStoreProperties
boolean_h_store_fields_for %w[variable_speed skid_mounted speed_lock flow_lock press_lock]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment