Class: Mongoid::DataTable::Proxy

Inherits:
Relations::Proxy
  • Object
show all
Defined in:
lib/mongoid/data_table/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, controller, options = {}, &block) ⇒ Proxy

Returns a new instance of Proxy.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mongoid/data_table/proxy.rb', line 25

def initialize(klass, controller, options = {}, &block)
  @klass      = klass
  @controller = controller
  @cookies    = @controller.send(:cookies)
  @options    = klass.data_table_options.merge(options)
  @extension  = block || options[:dataset] || klass.data_table_dataset || default_data_table_dataset

  @params   = (options[:params]  || controller.params || {}).dup.symbolize_keys
  @criteria = options[:criteria] || klass.criteria
  @unscoped = options[:unscoped] || klass.unscoped
  @fields   = options[:fields]   || klass.data_table_fields
  @aliases  = options[:aliases]  || @fields

  @locals = options[:locals] || {}

  params[:iDisplayLength] = conditions.count if (params[:iDisplayLength].to_i rescue 0) == -1

  @cookie_prefix = options[:cookie_prefix] || 'SpryMedia_DataTables_'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

:nodoc:



262
263
264
# File 'lib/mongoid/data_table/proxy.rb', line 262

def method_missing(method, *args, &block) #:nodoc:
  collection.send(method, *args, &block)
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def aliases
  @aliases
end

#controllerObject (readonly)

Returns the value of attribute controller.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def controller
  @controller
end

Returns the value of attribute cookie.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def cookie
  @cookie
end

Returns the value of attribute cookie_prefix.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def cookie_prefix
  @cookie_prefix
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def cookies
  @cookies
end

#criteriaObject (readonly)

Returns the value of attribute criteria.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def criteria
  @criteria
end

#extensionObject (readonly)

Returns the value of attribute extension.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def extension
  @extension
end

#fieldsObject (readonly)

Returns the value of attribute fields.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def klass
  @klass
end

#localsObject (readonly)

Returns the value of attribute locals.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def locals
  @locals
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def params
  @params
end

Returns the value of attribute raw_cookie.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def raw_cookie
  @raw_cookie
end

#unscopedObject (readonly)

Returns the value of attribute unscoped.



10
11
12
# File 'lib/mongoid/data_table/proxy.rb', line 10

def unscoped
  @unscoped
end

Instance Method Details

#as_json(options = nil, &inline_block) ⇒ Object



183
184
185
# File 'lib/mongoid/data_table/proxy.rb', line 183

def as_json(options = nil, &inline_block)
  to_hash(&inline_block).to_json(options)
end

#collection(force = false) ⇒ Object



45
46
47
48
# File 'lib/mongoid/data_table/proxy.rb', line 45

def collection(force = false)
  reload if force
  @collection ||= conditions.page(current_page).per(per_page)
end

#conditionsObject



162
163
164
# File 'lib/mongoid/data_table/proxy.rb', line 162

def conditions
  criteria.order_by(order_by_conditions).where(filter_conditions).where(filter_field_conditions)
end

#cookie!(sInstance = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongoid/data_table/proxy.rb', line 58

def cookie!(sInstance = nil)
  @cookie = nil
  sInstance ||= options[:sInstance]
  @raw_cookie = cookies[cookie_name(sInstance)]
  begin
    @cookie = ::JSON.load(raw_cookie) if raw_cookie.kind_of?(String)
  rescue JSON::ParserError
    cookies.delete cookie_name(sInstance)
    @cookie = {}
  ensure
    (@cookie ||= {}).symbolize_keys!
  end
end

#current_pageObject

pagination options ##



154
155
156
# File 'lib/mongoid/data_table/proxy.rb', line 154

def current_page
  params[:page].present? ? params[:page].to_i : (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i rescue 0)+1
end

#load_cookie!(sInstance = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mongoid/data_table/proxy.rb', line 72

def load_cookie!(sInstance = nil)
  tap do

    cookie!(sInstance)

    if cookie.present? and cookie.kind_of?(Hash)
      load_defaults!(cookie)
    elsif (defaults = options[:defaults]).present? and defaults.kind_of?(Hash)
      load_defaults!(defaults)
    end

  end
end

#load_defaults!(defaults = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mongoid/data_table/proxy.rb', line 86

def load_defaults!(defaults = {})
  tap do
    state_params = Hash.new.tap do |hash|
      hash[:iDisplayStart]  = defaults[:iStart]  if defaults.has_key?(:iStart)
      hash[:iDisplayLength] = defaults[:iLength] if defaults.has_key?(:iLength)
      hash[:sSearch]        = defaults[:sFilter] if defaults.has_key?(:sFilter)

      if defaults.has_key?(:aaSorting)
        sorting = [*defaults[:aaSorting]].first

        hash[:iSortCol_0] = [*sorting][0]
        hash[:sSortDir_0] = [*sorting][1]
      end

      if defaults.has_key?(:aoSearchCols)
        [*defaults[:aoSearchCols]].each_with_index do |oSearch, index|
          if oSearch.kind_of?(Hash)
            oSearch.symbolize_keys!
            hash[:"bRegex_#{index}"]  = oSearch[:bRegex]
            hash[:"sSearch_#{index}"] = URI.decode(oSearch[:sSearch])
          else
            hash[:"sSearch_#{index}"] = URI.decode(oSearch.to_s)
          end
        end
      elsif defaults.has_key?(:aaSearchCols)
        begin
          [*defaults[:aaSearchCols]].each_with_index do |(value,regex),index|
            hash[:"bRegex_#{index}"]  = !regex
            hash[:"sSearch_#{index}"] = URI.decode(value)
          end
        rescue
          # do nothing
        end
      end
    end

    params.merge!(state_params)

    @collection = nil
  end
end

#per_pageObject



158
159
160
# File 'lib/mongoid/data_table/proxy.rb', line 158

def per_page
  (params[:iDisplayLength] || options[:per_page] || klass.per_page rescue 10).to_i
end

#reloadObject



50
51
52
# File 'lib/mongoid/data_table/proxy.rb', line 50

def reload
  (@collection = nil).nil?
end

#search_fieldsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mongoid/data_table/proxy.rb', line 128

def search_fields
  return [] if cookie.blank? or not cookie.kind_of?(Hash)
  [].tap do |array|

    if cookie.has_key?(:aoSearchCols)
      [*cookie[:aoSearchCols]].each_with_index do |oSearch, index|
        if oSearch.kind_of?(Hash)
          oSearch.symbolize_keys!
          array.push(URI.decode(oSearch[:sSearch].to_s))
        end
      end
    else
      begin
        [*cookie[:aaSearchCols]].each_with_index do |(value,regex),index|
          array.push(URI.decode(value.to_s))
        end
      rescue
        # do nothing
      end
    end

  end
end

#to_hash(&inline_block) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/mongoid/data_table/proxy.rb', line 166

def to_hash(&inline_block)
  inline_block = extension unless block_given?
  {
    :sEcho => params[:sEcho].to_i,
    :iTotalRecords => unscoped.count,
    :iTotalDisplayRecords => conditions.count,
    :aaData => collection.map do |object|
      data = controller.instance_eval { inline_block.call(object) }
      data.inject(data.is_a?(Hash) ? {} : []) do |result, item|
        Rails.logger.silence_warnings do
          controller.instance_eval(&render_data_table_block(klass, item, object, result))
        end
      end
    end
  }
end

#to_json(*args, &inline_block) ⇒ Object



187
188
189
# File 'lib/mongoid/data_table/proxy.rb', line 187

def to_json(*args, &inline_block)
  as_json(*args, &inline_block)
end