Module: Filters::Resource::ClassMethods

Defined in:
lib/dm-filters.rb

Instance Method Summary collapse

Instance Method Details

#filtered_propertiesObject



131
132
133
134
135
136
137
138
# File 'lib/dm-filters.rb', line 131

def filtered_properties
  begin
    # This is to work with STI models. It's not a very good solution.
    @filtered_properties || self.superclass.filtered_properties
  rescue
    nil
  end
end

#property(name, type, options = {}) ⇒ Object

Override DataMapper’s property class method to accept as an option filter. filter Hash with the following pairs:

  • :to - Name of property to filter to; this should not be explicitly declared as a property.

  • :with - Either 1) Name of the property (as a symbol) that designates filter (does not need to be explicitly declared as a property) or 2) A semi-colon delimited String represented filters to use (or an Array of strings).

  • :default - A semi-colon delimited String represented filters to use (or an Array of strings) if the filter column is blank.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dm-filters.rb', line 110

def property(name, type, options = {})
  if filter = options.delete(:filter)
    @filtered_properties ||= []
    @filtered_properties << filter.merge({:name => name})
    begin
      self.properties[filter[:to].to_s]
    rescue
      self.property(filter[:to], type)
    end
    if filter[:with].kind_of?(Symbol)
      begin
        self.properties[filter[:with].to_s]
      rescue
        self.property(filter[:with], String)
      end
    end
  end
  
  super(name, type, options)
end