Module: Something::ClassMethods

Defined in:
lib/hstore_attribute_support/snippets.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.hstore_attributesObject

Returns the value of attribute hstore_attributes.



26
27
28
# File 'lib/hstore_attribute_support/snippets.rb', line 26

def hstore_attributes
  @hstore_attributes
end

Class Method Details

.hstore_attr_accessor(attribute_name, hstore_column, type = nil, default = nil) ⇒ Object

this class method allows descendant classes to set up attributes, which are stored in a hstore column. this solves two problems:

  1. by providing a cast hint, it will take care of the data type, which is lost by the hstore and was a pure string otherwise

  2. it provides an easy way of setting up defaults, as rails AR mechanisms fail here (no database defaults available for virtual attributes)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hstore_attribute_support/snippets.rb', line 69

def self.hstore_attr_accessor(attribute_name, hstore_column, type  = nil, default = nil)
  self.hstore_attributes ||= {}

  self.hstore_attributes[attribute_name.to_s] = {
    :column  => hstore_column.to_s,
    :type    => type,
    :default => default
  }

  class_eval %Q{
    def #{attribute_name}
      return read_hstore_attribute("#{attribute_name}")
    end

    def #{attribute_name}?
      return read_hstore_attribute("#{attribute_name}").present?
    end

    def #{attribute_name}=(value)
      return write_hstore_attribute("#{attribute_name}", value)
    end
  }
end