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.



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

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 rescue {})
  @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:



215
216
217
# File 'lib/mongoid/data_table/proxy.rb', line 215

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

#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



136
137
138
# File 'lib/mongoid/data_table/proxy.rb', line 136

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

#collection(force = false) ⇒ Object



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

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

#conditionsObject



115
116
117
# File 'lib/mongoid/data_table/proxy.rb', line 115

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

#current_pageObject

pagination options ##



107
108
109
# File 'lib/mongoid/data_table/proxy.rb', line 107

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mongoid/data_table/proxy.rb', line 53

def load_cookie!(sInstance = nil)
  sInstance ||= options[:sInstance]
  @cookie = @cookies[cookie_name(sInstance)]
  begin
    @cookie = ::JSON.load(cookie) if cookie.is_a?(String)
  rescue
    @cookie = nil
    @cookies.delete cookie_name(sInstance)
    return self
  end
  if cookie.present?

    state_params = HashWithIndifferentAccess.new.tap do |h|

      h[:iDisplayStart]  = cookie['iStart']
      h[:iDisplayLength] = cookie['iLength']
      h[:sSearch]        = cookie['sFilter']

      h[:iSortCol_0], h[:sSortDir_0] = (cookie['aaSorting'].first rescue [ nil, nil ])

      begin
        cookie['aaSearchCols'].each_with_index do |(value,regex),index|
          h[:"bRegex_#{index}"]  = !regex
          h[:"sSearch_#{index}"] = value
        end
      rescue
        # do nothing
      end

    end

    params.merge!(state_params)

    @collection = nil

  end
  return self
end

#per_pageObject



111
112
113
# File 'lib/mongoid/data_table/proxy.rb', line 111

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

#reloadObject



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

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

#search_fieldsObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid/data_table/proxy.rb', line 92

def search_fields
  return [] if cookie.blank?
  array = []
  begin
    cookie['aaSearchCols'].each_with_index do |(value,regex),index|
      array.push(URI.decode(value.to_s))
    end
  rescue
    # do nothing
  end
  return array
end

#to_hash(&inline_block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mongoid/data_table/proxy.rb', line 119

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 do
          controller.instance_eval(&render_data_table_block(klass, item, object, result))
        end
      end
    end
  }
end

#to_json(*args, &inline_block) ⇒ Object



140
141
142
# File 'lib/mongoid/data_table/proxy.rb', line 140

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