-
-
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.
This file contains 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 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 |
This file contains 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
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