Class: Mongoid::DataTable::Proxy
- Inherits:
-
Relations::Proxy
- Object
- Relations::Proxy
- Mongoid::DataTable::Proxy
- Defined in:
- lib/mongoid/data_table/proxy.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#cookie_prefix ⇒ Object
readonly
Returns the value of attribute cookie_prefix.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#locals ⇒ Object
readonly
Returns the value of attribute locals.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#unscoped ⇒ Object
readonly
Returns the value of attribute unscoped.
Instance Method Summary collapse
- #as_json(options = nil, &inline_block) ⇒ Object
- #collection(force = false) ⇒ Object
- #conditions ⇒ Object
-
#current_page ⇒ Object
pagination options ##.
-
#initialize(klass, controller, options = {}, &block) ⇒ Proxy
constructor
A new instance of Proxy.
- #load_cookie!(sInstance = nil) ⇒ Object
- #per_page ⇒ Object
- #reload ⇒ Object
- #search_fields ⇒ Object
- #to_hash(&inline_block) ⇒ Object
- #to_json(*args, &inline_block) ⇒ Object
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, = {}, &block) @klass = klass @controller = controller @cookies = @controller.send(:cookies) @options = klass..merge() @extension = block || [:dataset] || klass.data_table_dataset || default_data_table_dataset @params = [:params] || (controller.params.dup rescue {}) @criteria = [:criteria] || klass.criteria @unscoped = [:unscoped] || klass.unscoped @fields = [:fields] || klass.data_table_fields @aliases = [:aliases] || @fields @locals = [:locals] || {} params[:iDisplayLength] = conditions.count if (params[:iDisplayLength].to_i rescue 0) == -1 @cookie_prefix = [: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
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def aliases @aliases end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def controller @controller end |
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def @cookie end |
#cookie_prefix ⇒ Object (readonly)
Returns the value of attribute cookie_prefix.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def @cookie_prefix end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def @cookies end |
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def criteria @criteria end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def extension @extension end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def fields @fields end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def klass @klass end |
#locals ⇒ Object (readonly)
Returns the value of attribute locals.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def locals @locals end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def params @params end |
#unscoped ⇒ Object (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( = nil, &inline_block) to_hash(&inline_block).to_json() 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 |
#conditions ⇒ Object
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_page ⇒ Object
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 (sInstance = nil) sInstance ||= [:sInstance] @cookie = @cookies[(sInstance)] begin @cookie = ::JSON.load() if .is_a?(String) rescue @cookie = nil @cookies.delete (sInstance) return self end if .present? state_params = HashWithIndifferentAccess.new.tap do |h| h[:iDisplayStart] = ['iStart'] h[:iDisplayLength] = ['iLength'] h[:sSearch] = ['sFilter'] h[:iSortCol_0], h[:sSortDir_0] = (['aaSorting'].first rescue [ nil, nil ]) begin ['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_page ⇒ Object
111 112 113 |
# File 'lib/mongoid/data_table/proxy.rb', line 111 def per_page (params[:iDisplayLength] || [:per_page] || klass.per_page rescue 10).to_i end |
#reload ⇒ Object
49 50 51 |
# File 'lib/mongoid/data_table/proxy.rb', line 49 def reload (@collection = nil).nil? end |
#search_fields ⇒ Object
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 .blank? array = [] begin ['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 |