Class: Puffer::Filters

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion
Defined in:
lib/puffer/filters.rb

Defined Under Namespace

Classes: Diapason

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fieldset, attributes = {}) ⇒ Filters

Returns a new instance of Filters.



39
40
41
42
43
44
45
# File 'lib/puffer/filters.rb', line 39

def initialize fieldset, attributes = {}
  @attributes = {}
  @fieldset = fieldset
  generate_attribute_methods

  self.attributes = attributes
end

Instance Attribute Details

#fieldsetObject (readonly)

Returns the value of attribute fieldset.



37
38
39
# File 'lib/puffer/filters.rb', line 37

def fieldset
  @fieldset
end

Class Method Details

.special_attributesObject



24
25
26
# File 'lib/puffer/filters.rb', line 24

def self.special_attributes
  %w(puffer_search puffer_order puffer_per_page)
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/puffer/filters.rb', line 61

def any?
  attributes.except(:puffer_per_page).values.any?
end

#attributesObject



65
66
67
68
69
# File 'lib/puffer/filters.rb', line 65

def attributes
  (fieldset.map(&:field_name) + special_attributes).reduce(ActiveSupport::HashWithIndifferentAccess.new()) do |res, attribute|
    res.merge attribute => send(attribute)
  end
end

#attributes=(attributes = {}) ⇒ Object



71
72
73
74
75
# File 'lib/puffer/filters.rb', line 71

def attributes= attributes = {}
  attributes.each do |(key, value)|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
end

#conditionsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puffer/filters.rb', line 96

def conditions
  fieldset.reduce(ActiveSupport::HashWithIndifferentAccess.new()) do |res, field|
    attribute = field.field_name
    value = send(attribute)

    unless value.blank?
      if field.column_type == :boolean
        value = true if Puffer::TRUE_VALUES.include?(value)
        value = false if Puffer::FALSE_VALUES.include?(value)
      end

      value = case value
      when 'puffer_nil' then nil
      when 'puffer_blank' then ''
      else value
      end
      
      res[attribute] = value
    end


    res
  end
end

#generate_attribute_methodsObject



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

def generate_attribute_methods
  fieldset.each do |field|
    define_singleton_method :"#{field}" do
      read_attribute field
    end
    define_singleton_method :"#{field}=" do |value|
      write_attribute field, value
    end

    if %(date, time, datetime, date_time, timestamp).include?(field.type.to_s)
      @attributes[field.to_s] = Puffer::Filters::Diapason.new

      define_singleton_method :"#{field}_attributes=" do |value|
        write_attribute field, value
      end
    end
  end
end

#orderObject



125
126
127
# File 'lib/puffer/filters.rb', line 125

def order
  puffer_order.to_s.split(' ').map(&:to_sym)
end

#per_pageObject



129
130
131
# File 'lib/puffer/filters.rb', line 129

def per_page
  puffer_per_page.to_i
end

#persisted?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/puffer/filters.rb', line 142

def persisted?
  false
end

#queryObject



133
134
135
136
137
138
139
140
# File 'lib/puffer/filters.rb', line 133

def query
  (fieldset.map(&:field_name) + special_attributes).reduce(ActiveSupport::HashWithIndifferentAccess.new()) do |res, attribute|
    value = send(attribute)
    attribute = "#{attribute}_attributes" if respond_to?("#{attribute}_attributes=")
    res[attribute] = value if value.present?
    res
  end
end

#read_attribute(name) ⇒ Object



47
48
49
# File 'lib/puffer/filters.rb', line 47

def read_attribute name
  @attributes[name.to_s]
end

#searchObject



121
122
123
# File 'lib/puffer/filters.rb', line 121

def search
  puffer_search
end

#write_attribute(name, value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/puffer/filters.rb', line 51

def write_attribute name, value
  if value.is_a? Hash
    value.each do |key, subvalue|
      @attributes[name.to_s][key] = subvalue if subvalue.present?
    end
  else
    @attributes[name.to_s] = value
  end if value.present?
end