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(sInstance = nil) ⇒ 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.
-
#raw_cookie ⇒ Object
readonly
Returns the value of attribute raw_cookie.
-
#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
- #cookie!(sInstance = nil) ⇒ Object
-
#current_page ⇒ Object
pagination options ##.
-
#initialize(klass, controller, options = {}, &block) ⇒ Proxy
constructor
A new instance of Proxy.
- #load_cookie!(sInstance = nil) ⇒ Object
- #load_defaults!(defaults = {}) ⇒ 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.
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, = {}, &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.symbolize_keys @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:
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
#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(sInstance = nil) ⇒ 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 |
#raw_cookie ⇒ Object (readonly)
Returns the value of attribute raw_cookie.
10 11 12 |
# File 'lib/mongoid/data_table/proxy.rb', line 10 def @raw_cookie 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
183 184 185 |
# File 'lib/mongoid/data_table/proxy.rb', line 183 def as_json( = nil, &inline_block) to_hash(&inline_block).to_json() 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 |
#conditions ⇒ Object
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 (sInstance = nil) @cookie = nil sInstance ||= [:sInstance] @raw_cookie = [(sInstance)] begin @cookie = ::JSON.load() if .kind_of?(String) rescue JSON::ParserError .delete (sInstance) @cookie = {} ensure (@cookie ||= {}).symbolize_keys! end end |
#current_page ⇒ Object
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 (sInstance = nil) tap do (sInstance) if .present? and .kind_of?(Hash) load_defaults!() elsif (defaults = [: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_page ⇒ Object
158 159 160 |
# File 'lib/mongoid/data_table/proxy.rb', line 158 def per_page (params[:iDisplayLength] || [:per_page] || klass.per_page rescue 10).to_i end |
#reload ⇒ Object
50 51 52 |
# File 'lib/mongoid/data_table/proxy.rb', line 50 def reload (@collection = nil).nil? end |
#search_fields ⇒ Object
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 .blank? or not .kind_of?(Hash) [].tap do |array| if .has_key?(:aoSearchCols) [*[: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 [*[: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 |