Module: Filters::Resource

Defined in:
lib/dm-filters.rb

Overview

Filters::Resource can be included in a model to enable the :filtered_to => (name of other property) option for properties. This adds a property name “filters”, which is a semi-colon delimited list of filters through which to process the original property.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Set a few ‘magic’ properties



69
70
71
72
# File 'lib/dm-filters.rb', line 69

def included(klass) # Set a few 'magic' properties
  klass.extend(ClassMethods)
  klass.before :save, :process_filters
end

Instance Method Details

#process_filtersObject

Process and filters for @filtered_properties.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dm-filters.rb', line 76

def process_filters
  return if self.class.filtered_properties.nil?
  self.class.filtered_properties.each do |f|
    if attribute_dirty?(f[:name])
      filters = nil
      if !f[:with].blank?
        if f[:with].kind_of?(Symbol)
          filters = self.__send__(f[:with])
        else
          filters = f[:with]
        end
      end
      if filters.blank?
        filters = f[:default]
      end
      if filters == :site
        filters = self.site.__send__("#{self.class.name.snake_case}_filter")
      end
      attribute_set(f[:to], Filters.process(filters, self.__send__(f[:name])))
    end
  end
end