Class: SimpleDrilldown::DrilldownController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- SimpleDrilldown::DrilldownController
- Defined in:
- lib/simple_drilldown/drilldown_controller.rb
Defined Under Namespace
Classes: ScopeHolder
Constant Summary collapse
- LIST_LIMIT =
10_000
Class Method Summary collapse
- .base_condition(base_condition) ⇒ Object
- .base_group(base_group) ⇒ Object
- .base_includes(base_includes) ⇒ Object
- .default_fields(fields) ⇒ Object
- .dimension(name, select_expression = name, options = {}) ⇒ Object
- .field(name, **options) ⇒ Object
- .legal_values_for(field, preserve_filter = false) ⇒ Object
- .list_includes(list_includes) ⇒ Object
- .list_order(list_order) ⇒ Object
- .make_conditions(search_filter) ⇒ Object
- .make_join(joins, model, include, model_class = model.to_s.camelize.constantize) ⇒ Object
- .select(select) ⇒ Object
- .summary_fields(*summary_fields) ⇒ Object
- .target_class(target_class) ⇒ Object
Instance Method Summary collapse
- #choices ⇒ Object
- #excel_export ⇒ Object
- #excel_export_transactions ⇒ Object
- #html_export ⇒ Object
-
#index(do_render = true) ⇒ Object
?dimension=supplier&dimension=transaction_type& filter=2009&filter[0]=Shell&filter[1]=Statoil.
-
#initialize ⇒ DrilldownController
constructor
A new instance of DrilldownController.
- #xml_export ⇒ Object
Constructor Details
#initialize ⇒ DrilldownController
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 255 def initialize super() @fields = @@fields @default_fields = @@default_fields @default_select_value = SimpleDrilldown::Search::SelectValue::COUNT @target_class = @@target_class @select = @@select @@base_condition = '1 = 1' unless defined?(@@base_condition) @base_condition = @@base_condition @@base_includes = [] unless defined?(@@base_includes) @base_includes = @@base_includes @base_group = defined?(@@base_group) ? @@base_group : [] @@list_includes = [] unless defined?(@@list_includes) @list_includes = @@list_includes @list_order = @@list_order @dimension_defs = @@dimension_defs @@summary_fields = [] unless defined?(@@summary_fields) @summary_fields = @@summary_fields @history_fields = @fields.select { |_k, v| v[:list_change_times] }.map { |k, _v| k.to_s } end |
Class Method Details
.base_condition(base_condition) ⇒ Object
13 14 15 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 13 def base_condition(base_condition) @@base_condition = base_condition end |
.base_group(base_group) ⇒ Object
21 22 23 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 21 def base_group(base_group) @@base_group = base_group end |
.base_includes(base_includes) ⇒ Object
17 18 19 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 17 def base_includes(base_includes) @@base_includes = base_includes end |
.default_fields(fields) ⇒ Object
25 26 27 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 25 def default_fields(fields) @@default_fields = fields end |
.dimension(name, select_expression = name, options = {}) ⇒ Object
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 91 92 93 94 95 96 97 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 54 def dimension(name, select_expression = name, = {}) interval = .delete(:interval) label_method = .delete(:label_method) legal_values = .delete(:legal_values) || legal_values_for(name) reverse = .delete(:reverse) row_class = .delete(:row_class) if select_expression.is_a?(Array) queries = select_expression else includes = .delete(:includes) conditions = .delete(:where) queries = [{ select: select_expression, includes: includes, where: conditions }] end raise "Unexpected options: #{options.inspect}" if .present? queries.each do |query_opts| raise "Unknown options: #{query_opts.keys.inspect}" unless (query_opts.keys - i[select includes where]).empty? end @@dimension_defs ||= Concurrent::Hash.new @@dimension_defs[name.to_s] = { includes: queries.inject([]) do |a, e| i = e[:includes] next a unless i next a if a.include?(i) a + [i] end, interval: interval, label_method: label_method, legal_values: legal_values, pretty_name: I18n.t(name), queries: queries, reverse: reverse, select_expression: queries.size > 1 ? "COALESCE(#{queries.map { |q| q[:select] }.join(',')})" : queries[0][:select], row_class: row_class, url_param_name: name.to_s } end |
.field(name, **options) ⇒ Object
45 46 47 48 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 45 def field(name, **) @@fields ||= {} @@fields[name] = end |
.legal_values_for(field, preserve_filter = false) ⇒ Object
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 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 99 def legal_values_for(field, preserve_filter = false) lambda do |search| my_filter = search.filter.dup my_filter.delete(field.to_s) unless preserve_filter filter_conditions, _t, includes = make_conditions(my_filter) dimension_def = @@dimension_defs[field.to_s] result_sets = dimension_def[:queries].map do |query| if query[:includes] if query[:includes].is_a?(Array) includes += query[:includes] else includes << query[:includes] end includes.uniq! end rows = @@target_class.unscoped.where(@@base_condition) .select("#{query[:select]} AS value") .where(filter_conditions) .where(query[:where]) .joins(make_join([], @@target_class.name.underscore.to_sym, includes)) .order('value') .group(:value) .to_a filter_fields = search.filter[field.to_s] filter_fields&.each do |selected_value| next if rows.find { |r| r[:value].to_s == selected_value } # FIXME(uwe): Convert the selected value to same data type as legal values rows << { value: selected_value } # EMXIF end rows.map { |r| [dimension_def[:label_method]&.call(r[:value]) || r[:value], r[:value]] } end values = result_sets.inject(&:+).uniq values.sort! if dimension_def[:queries].size > 1 values.reverse! if dimension_def[:reverse] values end end |
.list_includes(list_includes) ⇒ Object
37 38 39 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 37 def list_includes(list_includes) @@list_includes = list_includes end |
.list_order(list_order) ⇒ Object
41 42 43 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 41 def list_order(list_order) @@list_order = list_order end |
.make_conditions(search_filter) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 139 def make_conditions(search_filter) includes = @@base_includes.dup if search_filter condition_strings = [] condition_values = [] filter_texts = [] search_filter.each do |field, values| dimension_def = @@dimension_defs[field] raise "Unknown filter field: #{field.inspect}" if dimension_def.nil? values = [*values] if dimension_def[:interval] values *= 2 if values.size == 1 raise "Need 2 values for interval filter: #{values.inspect}" if values.size != 2 if values[0].present? && values[1].present? condition_strings << "#{dimension_def[:select_expression]} BETWEEN ? AND ?" condition_values += values filter_texts << " \#{dimension_def[:pretty_name]} \#{dimension_def[:label_method]&.call(values) || \"from \#{values[0]} to \#{values[1]}\"}\n TEXT\n elsif values[0].present?\n condition_strings << \"\#{dimension_def[:select_expression]} >= ?\"\n condition_values << values[0]\n filter_texts <<\n \"\#{dimension_def[:pretty_name]} \#{dimension_def[:label_method]&.call(values) || \"from \#{values[0]}\"}\"\n elsif values[1].present?\n condition_strings << \"\#{dimension_def[:select_expression]} <= ?\"\n condition_values << values[1]\n filter_texts <<\n \"\#{dimension_def[:pretty_name]} \#{dimension_def[:label_method]&.call(values) || \"to \#{values[1]}\"}\"\n end\n includes << dimension_def[:includes] if dimension_def[:includes]\n else\n condition_strings << values.map do |value|\n if dimension_def[:condition_values_method]\n condition_values += dimension_def[:condition_values_method].call(value)\n else\n condition_values << value\n end\n filter_texts <<\n \"\#{dimension_def[:pretty_name]} \#{dimension_def[:label_method]&.call(value) || value}\"\n includes << dimension_def[:includes] if dimension_def[:includes]\n \"(\#{dimension_def[:select_expression]}) = ?\"\n end.join(' OR ')\n end\n end\n filter_text = filter_texts.join(' and ')\n conditions = [condition_strings.map { |c| \"(\#{c})\" }.join(' AND '), *condition_values]\n includes.keep_if(&:present?).uniq!\n else\n filter_text = nil\n conditions = nil\n end\n conditions = nil if conditions == ['']\n [conditions, filter_text, includes]\nend\n" |
.make_join(joins, model, include, model_class = model.to_s.camelize.constantize) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 198 def make_join(joins, model, include, model_class = model.to_s.camelize.constantize) case include when Array include.map { |i| make_join(joins, model, i) }.join(' ') when Hash sql = +'' include.each do |parent, child| sql << make_join(joins, model, parent) + ' ' ass = model.to_s.camelize.constantize.reflect_on_association parent sql << make_join(joins, parent, child, ass.class_name.constantize) end sql when Symbol return '' if joins.include?(include) joins = joins.dup joins << include ass = model_class.reflect_on_association include raise "Unknown association: #{model} => #{include}" unless ass model_table = model.to_s.pluralize include_table = ass.table_name include_alias = include.to_s.pluralize case ass.macro when :belongs_to "LEFT JOIN #{include_table} #{include_alias} ON #{include_alias}.id = #{model_table}.#{include}_id" when :has_one, :has_many fk_col = ass.[:foreign_key] || "#{model}_id" sql = +"LEFT JOIN #{include_table} #{include_alias} ON #{include_alias}.#{fk_col} = #{model_table}.id" sql << " AND #{include_alias}.deleted_at IS NULL" if ass.klass.paranoid? if ass.scope && (ass_order = ScopeHolder.new(ass.scope).to_s) ass_order = ass_order.sub(/ DESC\s*$/i, '') ass_order_prefixed = ass_order.dup ActiveRecord::Base.connection.columns(include_table).map(&:name).each do |cname| ass_order_prefixed.gsub!(/\b#{cname}\b/, "#{include_alias}.#{cname}") end paranoid_clause = 'AND t2.deleted_at IS NULL' if ass.klass.paranoid? # FIXME(uwe): Should we add "where" from the ScopeHolder here as well? Ref: DrilldownChanges#changes_for min_query = " SELECT MIN(\#{ass_order}) FROM \#{include_table} t2 WHERE t2.\#{fk_col} = \#{model_table}.id \#{paranoid_clause}\n SQL\n sql << \" AND \#{ass_order_prefixed} = (\#{min_query})\"\n end\n sql\n else\n raise \"Unknown association type: \#{ass.macro}\"\n end\n when String\n include\n when nil\n ''\n else\n raise \"Unknown join class: \#{include.inspect}\"\n end\nend\n" |
.select(select) ⇒ Object
33 34 35 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 33 def select(select) @@select = select end |
.summary_fields(*summary_fields) ⇒ Object
50 51 52 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 50 def summary_fields(*summary_fields) @@summary_fields = summary_fields end |
.target_class(target_class) ⇒ Object
29 30 31 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 29 def target_class(target_class) @@target_class = target_class end |
Instance Method Details
#choices ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 352 def choices @search = new_search_object dimension_name = params[:dimension_name] dimension = @dimension_defs[dimension_name] selected = @search.filter[dimension_name] || [] raise "Unknown dimension #{dimension_name.inspect}: #{@dimension_defs.keys.inspect}" unless dimension choices = [[t(:all), nil]] + (dimension[:legal_values]&.call(@search)&.map { |o| o.is_a?(Array) ? o[0..1].map(&:to_s) : o.to_s } || []) choices_html = choices.map do |c| %(<option value="#{c[1]}"#{' SELECTED' if selected.include?(c[1])}>#{c[0]}</option>) end.join("\n") render html: choices_html.html_safe end |
#excel_export ⇒ Object
372 373 374 375 376 377 378 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 372 def excel_export index(false) headers['Content-Type'] = 'application/vnd.ms-excel' headers['Content-Disposition'] = 'attachment; filename="transactions.xml"' headers['Cache-Control'] = '' render template: '/drilldown/excel_export', layout: false end |
#excel_export_transactions ⇒ Object
380 381 382 383 384 385 386 387 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 380 def excel_export_transactions params[:search][:list] = '1' index(false) @transactions = get_transactions(@result) headers['Content-Type'] = 'application/vnd.ms-excel' headers['Content-Disposition'] = 'attachment; filename="transactions.xml"' render template: '/drilldown/excel_export_transactions', layout: false end |
#html_export ⇒ Object
367 368 369 370 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 367 def html_export index(false) render template: '/drilldown/html_export', layout: 'print' end |
#index(do_render = true) ⇒ Object
?dimension=supplier&dimension=transaction_type& filter=2009&filter[0]=Shell&filter[1]=Statoil
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 279 def index(do_render = true) @search = new_search_object @transaction_fields = (@search.fields + (@fields.keys.map(&:to_s) - @search.fields)) @transaction_fields_map = @fields select = @select.dup includes = @base_includes.dup @dimensions = [] select << ", 'All'::text as value0" @dimensions += @search.dimensions.map do |dn| raise "Unknown distribution field: #{dn.inspect}" if @dimension_defs[dn].nil? @dimension_defs[dn] end @dimensions.each_with_index do |d, i| select << ", #{d[:select_expression]} as value#{i + 1}" includes << d[:includes] if d[:includes] end conditions, @filter_text, filter_includes = self.class.make_conditions(@search.filter) includes += filter_includes includes.keep_if(&:present?).uniq! if @search.order_by_value && @dimensions.size <= 1 order = case @search.select_value when DrilldownSearch::SelectValue::VOLUME 'volume DESC' when DrilldownSearch::SelectValue::VOLUME_COMPENSATED 'volume_compensated DESC' when DrilldownSearch::SelectValue::COUNT 'count DESC' else 'count DESC' end else order = (1..@dimensions.size).map { |i| "value#{i}" }.join(',') order = nil if order.empty? end group = (@base_group + (1..@dimensions.size).map { |i| "value#{i}" }).join(',') group = nil if group.empty? joins = self.class.make_join([], @target_class.name.underscore.to_sym, includes) rows = @target_class.unscoped.where(@base_condition).select(select).where(conditions) .joins(joins) .group(group) .order(order).to_a if rows.empty? @result = { value: 'All', count: 0, row_count: 0, nodes: 0, rows: [] } @summary_fields.each { |f| @result[f] = 0 } else if do_render && @search.list && rows.inject(0) { |sum, r| sum + r[:count].to_i } > LIST_LIMIT @search.list = false flash.now[:notice] = "More than #{LIST_LIMIT} records. List disabled." end @result = result_from_rows(rows, 0, 0, ['All']) end remove_duplicates(@result) unless @base_group.empty? @remaining_dimensions = @dimension_defs.dup @remaining_dimensions.each_key do |dim_name| if (@search.filter[dim_name] && @search.filter[dim_name].size == 1) || (@dimensions.any? { |d| d[:url_param_name] == dim_name }) @remaining_dimensions.delete(dim_name) end end populate_list(conditions, includes, @result, []) if @search.list render template: '/drilldown/index' if do_render end |
#xml_export ⇒ Object
389 390 391 392 393 394 395 396 |
# File 'lib/simple_drilldown/drilldown_controller.rb', line 389 def xml_export params[:search][:list] = '1' index(false) @transactions = get_transactions(@result) headers['Content-Type'] = 'text/xml' headers['Content-Disposition'] = 'attachment; filename="transactions.xml"' render template: '/drilldown/xml_export', layout: false end |