query_report 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in query_report.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 A.K.M. Ashrafuzzaman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # QueryReport
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'query_report'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install query_report
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = QueryReport
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ <% if filter.type == :user %>
2
+ <% search_key = "#{filter.column.to_s}_id_eq"
3
+ user_id = @record.search.send(search_key)
4
+ user_name = User.find(user_id).name rescue '' %>
5
+ <%= user_search_field_tag "#{search_key}_search_field", user_name, :placeholder => filter.column.to_s.humanize, :'sync-id' => "q_#{search_key}" %>
6
+ <%= f.hidden_field search_key %>
7
+ <% end %>
@@ -0,0 +1,62 @@
1
+ <%= search_form_for @record.search, :url => url_for, :html => {:method => :get, :class => 'form-inline'} do |f| %>
2
+ <% @record.filters.each do |filter| %>
3
+ <% if filter.class.supported_types.include?(filter.type) %>
4
+ <% filter.comparators.each do |key, hint| %>
5
+ <% search_key = "#{filter.column}_#{key}" %>
6
+ <% if filter.date? %>
7
+ <%= f.date_field search_key, :placeholder => hint %>
8
+ <% elsif filter.text? %>
9
+ <%= f.text_field search_key, placeholder: hint %>
10
+ <% end %>
11
+ <% end %>
12
+ <% else %>
13
+ <%= render :partial => "query_report/custom_filters", locals: {f: f, filter: filter} %>
14
+ <% end %>
15
+ <% end %>
16
+
17
+ <%= f.submit 'Search', :class => 'btn' %>
18
+ <% end %>
19
+
20
+ <br/>
21
+ <%= link_to_download_report_pdf %>
22
+ <%= link_to_download_report_csv %>
23
+ <br/>
24
+ <br/>
25
+
26
+ <script type="text/javascript" src="https://www.google.com/jsapi"></script>
27
+ <div id='chart' style="margin-left:auto;margin-right:auto;width:600px;"></div>
28
+ <%= render_chart(@record.chart.prepare, 'chart') if @record.chart %>
29
+
30
+ <br/>
31
+ <% if @record.scopes.size > 0 %>
32
+ <%= link_to_with_scope('all', @record.current_scope) %>
33
+ <% @record.scopes.each do |scope| %>
34
+ <%= link_to_with_scope(scope, @record.current_scope) %>
35
+ <% end %>
36
+ <% end %>
37
+ <br/>
38
+ <br/>
39
+
40
+ <% if @record.column_names %>
41
+ <table class="table table-bordered table-striped">
42
+ <thead>
43
+ <% @record.column_names.each do |column| %>
44
+ <th><%= column.humanize %></th>
45
+ <% end %>
46
+ </thead>
47
+
48
+ <tbody>
49
+ <% @record.records.each do |record| %>
50
+ <tr>
51
+ <% @record.column_names.each do |column| %>
52
+ <td><%= record[column] %></td>
53
+ <% end %>
54
+ </tr>
55
+ <% end %>
56
+ </tbody>
57
+ </table>
58
+ <% else %>
59
+ <p>No record found</p>
60
+ <% end %>
61
+
62
+ <%= paginate @record.query %>
@@ -0,0 +1,41 @@
1
+ module QueryReport
2
+ module Chart
3
+ class BasicChart
4
+ attr_reader :title, :columns, :type, :data, :options
5
+
6
+ def initialize(type, name, columns, data, options={})
7
+ @type = type
8
+ @name = name
9
+ @columns = []
10
+ columns.each_with_index do |column, i|
11
+ @columns << QueryReport::Column.new(column, {type: (i == 0 ? 'string' : 'number')})
12
+ end
13
+ @data = data
14
+ @options = options
15
+ end
16
+
17
+ def prepare
18
+ data_table = GoogleVisualr::DataTable.new
19
+ columns.each do |column|
20
+ data_table.new_column(column.type, column.name)
21
+ end
22
+
23
+ rows = []
24
+ @data.each do |record|
25
+ row = []
26
+ columns.each do |column|
27
+ row << record[column.name]
28
+ end
29
+ rows << row
30
+ end
31
+ data_table.add_rows(rows)
32
+
33
+ opts = {:width => 400, :height => 240, :title => title, :hAxis => {:title => columns[0].name}}.merge(options)
34
+
35
+ chart_type = "#{type}_chart".classify
36
+ chart_type = "GoogleVisualr::Interactive::#{chart_type}".constantize
37
+ chart_type.new(data_table, opts)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,82 @@
1
+ module QueryReport
2
+ module Chart
3
+ class ChartWithTotal
4
+ attr_reader :title, :columns, :type, :data, :options
5
+
6
+ def initialize(type, name, columns, data, options={})
7
+ @type = type
8
+ @name = name
9
+ @columns = []
10
+ columns.each do |column|
11
+ @columns << QueryReport::Column.new(column, 'number')
12
+ end
13
+ @data = data
14
+ @options = options
15
+ end
16
+
17
+ def prepare
18
+ data_table = GoogleVisualr::DataTable.new
19
+ columns.each do |column|
20
+ data_table.new_column(column.type, column.name)
21
+ end
22
+
23
+ row = []
24
+ columns.each do |column|
25
+ total = 0
26
+ @data.each do |r|
27
+ total += r[column].to_f
28
+ end
29
+ row << total
30
+ end
31
+
32
+ data_table.add_row(row)
33
+
34
+ opts = {:width => 400, :height => 240, :title => title}.merge(options)
35
+
36
+ chart_type = "#{type}_chart".classify
37
+ chart_type = "GoogleVisualr::Interactive::#{chart_type}".constantize
38
+ chart_type.new(data_table, opts)
39
+ end
40
+ end
41
+ end
42
+
43
+ #class ColumnChartWithTotal
44
+ # attr_reader :title, :columns, :type, :data, :options
45
+ #
46
+ # def initialize(type, name, columns, data, options={})
47
+ # @type = type
48
+ # @name = name
49
+ # @columns = []
50
+ # columns.each do |column|
51
+ # @columns << Report::Column.new(column, 'number')
52
+ # end
53
+ # @data = data
54
+ # @options = options
55
+ # end
56
+ #
57
+ # def prepare
58
+ # data_table = GoogleVisualr::DataTable.new
59
+ # columns.each do |column|
60
+ # data_table.new_column(column.type, column.name)
61
+ # end
62
+ #
63
+ # row = []
64
+ # columns.each do |column|
65
+ # total = 0
66
+ # @data.each do |r|
67
+ # total += r[column].to_f
68
+ # end
69
+ # row << total
70
+ # end
71
+ #
72
+ # data_table.add_row(row)
73
+ #
74
+ # opts = {:width => 400, :height => 240, :title => title}.merge(options)
75
+ #
76
+ # chart_type = "#{type}_chart".classify
77
+ # chart_type = "GoogleVisualr::Interactive::#{chart_type}".constantize
78
+ # chart_type.new(data_table, opts)
79
+ # end
80
+ #end
81
+
82
+ end
@@ -0,0 +1,35 @@
1
+ module QueryReport
2
+ module Chart
3
+ class CustomChart
4
+ attr_reader :title, :type, :options, :data_table, :row
5
+
6
+ def initialize(type, title, query, options={})
7
+ @type = type
8
+ @title = title
9
+ @query = query
10
+ @options = options
11
+ @row = []
12
+ @data_table = GoogleVisualr::DataTable.new
13
+ end
14
+
15
+ def add_column(title)
16
+ @data_table.new_column('string', title)
17
+ @row << title.humanize
18
+ end
19
+
20
+ def add(column_title, &block)
21
+ val = block.call(@query)
22
+ @data_table.new_column(val.kind_of?(String) ? 'string' : 'number', column_title)
23
+ @row << val
24
+ end
25
+
26
+ def prepare
27
+ data_table.add_row(@row)
28
+ opts = {:width => 500, :height => 240, :title => @title}.merge(options)
29
+ chart_type = "#{type}_chart".classify
30
+ chart_type = "GoogleVisualr::Interactive::#{chart_type}".constantize
31
+ chart_type.new(data_table, opts)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module QueryReport
2
+ module Chart
3
+ class PieChart
4
+ attr_reader :title, :options, :data_table, :query, :rows
5
+
6
+ def initialize(title, query, options={})
7
+ @title = title
8
+ @options = options
9
+ @rows = []
10
+ @query = query
11
+ @data_table = GoogleVisualr::DataTable.new
12
+ @data_table.new_column('string', 'Item')
13
+ @data_table.new_column('number', 'Value')
14
+ end
15
+
16
+ def add(column_title, &block)
17
+ val = block.call(@query)
18
+ @rows << [column_title, val]
19
+ end
20
+
21
+ def prepare
22
+ @data_table.add_rows(@rows)
23
+ opts = {:width => 500, :height => 240, :title => @title, :is3D => true}.merge(options)
24
+ GoogleVisualr::Interactive::PieChart.new(@data_table, opts)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module QueryReport
2
+ class Column
3
+ attr_reader :name, :options, :type, :data
4
+
5
+ def initialize(name, options={}, block = nil)
6
+ @name = name
7
+ @options = options
8
+ @type = (options.kind_of?(Hash) ? options[:type] : options) || 'string'
9
+ @data = block || name.to_sym
10
+ end
11
+
12
+ def humanize
13
+ options[:as] || name.to_s.humanize
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,41 @@
1
+ module QueryReport
2
+ class Filter
3
+ attr_reader :column, :type, :comparators, :block, :custom
4
+
5
+ def initialize(column, options, &block)
6
+ @column = column
7
+ @type = options if options.kind_of? String
8
+ if options.kind_of? Hash
9
+ @type = options[:type]
10
+ @comparators = options[:comp] || detect_comparators(@type)
11
+ end
12
+ @block = block
13
+ @custom = @block ? true : false
14
+ end
15
+
16
+ def self.supported_types
17
+ [:date, :text]
18
+ end
19
+
20
+ def keys
21
+ @keys ||= (@comparators || {}).keys.map { |comp| "#{column.to_s}_#{comp}" }
22
+ end
23
+
24
+ supported_types.each do |supported_type|
25
+ define_method("#{supported_type.to_s}?") do
26
+ @type == supported_type
27
+ end
28
+ end
29
+
30
+ private
31
+ def detect_comparators(type)
32
+ case type
33
+ when :date
34
+ return {gteq: 'From', lteq: 'To'}
35
+ when :text
36
+ return {cont: @column.to_s.humanize}
37
+ end
38
+ {eq: 'Equal'}
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ require 'query_report/record'
2
+
3
+ module QueryReport
4
+ module Helper
5
+ def reporter(query, &block)
6
+ @record ||= QueryReport::Record.new(params)
7
+ @record.set_query(query)
8
+ @record.instance_eval &block
9
+ render_report
10
+ end
11
+
12
+ def render_report
13
+ respond_to do |format|
14
+ format.html { render 'query_report/list' }
15
+ format.json { render json: @record.records }
16
+ format.csv { send_data generate_csv_for_report(@record.all_records), :disposition => "attachment;" }
17
+ format.pdf { render_pdf(ReportPdf.new.list(@record.all_records)) }
18
+ end
19
+ end
20
+
21
+ def generate_csv_for_report(records)
22
+ if records.size > 0
23
+ columns = records.first.keys
24
+ CSV.generate do |csv|
25
+ csv << columns
26
+ records.each do |record|
27
+ csv << record.values
28
+ end
29
+ end
30
+ else
31
+ nil
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,124 @@
1
+ require 'query_report/filter'
2
+ require 'query_report/column'
3
+ require 'query_report/chart/basic_chart'
4
+ require 'query_report/chart/chart_with_total'
5
+
6
+ module QueryReport
7
+ class Record
8
+ attr_accessor :params, :query, :query_without_pagination, :chart,
9
+ :filters, :search, :scopes, :current_scope
10
+
11
+ def initialize(params, options={}, &block)
12
+ @params = params
13
+ @columns = []
14
+ @filters = []
15
+ @scopes = []
16
+ @column_separator = options.delete(:separator)
17
+ @current_scope = @params[:scope] || 'all'
18
+ @options = options.delete(:options)
19
+ instance_eval &block if block_given?
20
+ end
21
+
22
+ def column(name, options={}, &block)
23
+ @columns << Column.new(name, options, block)
24
+ end
25
+
26
+ def set_query(query)
27
+ @query_cache = query
28
+ #apply ransack
29
+ @search = query.search(@params[:q])
30
+ @query_cache = @search.result
31
+ end
32
+
33
+ def columns
34
+ @columns
35
+ end
36
+
37
+ def column_names
38
+ @column_names ||= (@columns||[]).collect(&:humanize)
39
+ end
40
+
41
+ def query
42
+ apply_filters_and_pagination
43
+ @query_cache
44
+ end
45
+
46
+ def query_without_pagination
47
+ apply_filters_and_pagination
48
+ @query_without_pagination_cache
49
+ end
50
+
51
+ def records
52
+ @cached_records ||= map_record(query)
53
+ end
54
+
55
+ def all_records
56
+ @cached_all_records ||= map_record(query_without_pagination)
57
+ end
58
+
59
+ def map_record(query)
60
+ query.clone.map do |record|
61
+ array = @columns.collect { |column| [column.humanize,
62
+ (column.data.kind_of?(Symbol) ? record.send(column.name) : column.data.call(record))] }
63
+ Hash[*array.flatten]
64
+ end
65
+ end
66
+
67
+ def filter(column, options, &block)
68
+ @filters << Filter.new(column, options, &block)
69
+ end
70
+
71
+ def column_chart(title, columns)
72
+ @chart = QueryReport::Chart::BasicChart.new(:column, title, columns, all_records)
73
+ end
74
+
75
+ def compare_with_column_chart(title, x_axis, &block)
76
+ @chart = QueryReport::Chart::CustomChart.new(:column, title, query_without_pagination)
77
+ @chart.add_column x_axis
78
+ @chart.instance_eval &block if block_given?
79
+ end
80
+
81
+ def pie_chart(title, &block)
82
+ @chart = QueryReport::Chart::PieChart.new(title, query_without_pagination)
83
+ @chart.instance_eval &block if block_given?
84
+ end
85
+
86
+ def pie_chart_on_total(title, columns)
87
+ @chart = QueryReport::Chart::ChartWithTotal.new(:pie, title, columns, all_records, {:is3D => true})
88
+ end
89
+
90
+ def scope(scope)
91
+ @scopes << scope
92
+ @scopes = @scopes.uniq
93
+ end
94
+
95
+ private
96
+ def apply_filters_and_pagination
97
+ return if @applied_filters_and_pagination
98
+ if @current_scope and !['all', 'delete_all', 'destroy_all'].include?(@current_scope)
99
+ @query_cache = @query_cache.send(@current_scope)
100
+ end
101
+
102
+ @filters.each do |filter|
103
+ if filter.custom
104
+ param = @params[:custom_search]
105
+ #Rails.logger.debug "@params[:custom_search] :: #{@params[:custom_search].inspect}"
106
+ #Rails.logger.debug "param :: #{param.inspect}"
107
+ first_val = param[filter.keys.first] rescue nil
108
+ last_val = param[filter.keys.last] rescue nil
109
+ case filter.keys.size
110
+ when 1
111
+ @query_cache = filter.block.call(@query_cache, first_val) if first_val.present?
112
+ break
113
+ when 2
114
+ @query_cache = filter.block.call(@query_cache, first_val, last_val) if first_val.present? and last_val.present?
115
+ break
116
+ end
117
+ end
118
+ end
119
+ @query_without_pagination_cache = @query_cache
120
+ @query_cache = @query_without_pagination_cache.page(@params[:page])
121
+ @applied_filters_and_pagination = true
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ module QueryReport
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "query_report/version"
2
+ require "query_report/record"
3
+ require 'query_report/chart/pie_chart'
4
+ require 'query_report/chart/custom_chart'
5
+
6
+ module QueryReport
7
+ autoload :VERSION, 'query_report/version'
8
+ autoload :Helper, 'query_report/helper'
9
+ autoload :Views, 'query_report/views'
10
+ autoload :Record, 'query_report/record'
11
+ autoload :Filter, 'query_report/filter'
12
+ autoload :Column, 'query_report/column'
13
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'query_report/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "query_report"
8
+ gem.version = QueryReport::VERSION
9
+ gem.authors = ["A.K.M. Ashrafuzzaman"]
10
+ gem.email = ["ashrafuzzaman.g2@gmail.com"]
11
+ gem.description = %q{This is a gem to help you to structure common reports of you application just by writing in the controller}
12
+ gem.summary = %q{Structure you reports}
13
+ gem.homepage = "https://github.com/ashrafuzzaman/query_report"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib", "app"]
19
+
20
+ gem.add_dependency 'ransack'
21
+ gem.add_dependency 'google_visualr', '>= 2.1'
22
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - A.K.M. Ashrafuzzaman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ransack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: google_visualr
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '2.1'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '2.1'
46
+ description: This is a gem to help you to structure common reports of you application
47
+ just by writing in the controller
48
+ email:
49
+ - ashrafuzzaman.g2@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - README.rdoc
59
+ - Rakefile
60
+ - app/views/query_report/_custom_filters.html.erb
61
+ - app/views/query_report/list.html.erb
62
+ - lib/query_report.rb
63
+ - lib/query_report/chart/basic_chart.rb
64
+ - lib/query_report/chart/chart_with_total.rb
65
+ - lib/query_report/chart/custom_chart.rb
66
+ - lib/query_report/chart/pie_chart.rb
67
+ - lib/query_report/column.rb
68
+ - lib/query_report/filter.rb
69
+ - lib/query_report/helper.rb
70
+ - lib/query_report/record.rb
71
+ - lib/query_report/version.rb
72
+ - query_report.gemspec
73
+ homepage: https://github.com/ashrafuzzaman/query_report
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ - app
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.25
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Structure you reports
98
+ test_files: []