dynamic_query 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +17 -9
- data/Rakefile +0 -1
- data/app/helpers/_dynamic_result.html.erb +48 -1
- data/lib/dynamic_query.rb +43 -3
- data/lib/dynamic_query/{combine_query.rb → combined_query.rb} +1 -1
- data/lib/dynamic_query/version.rb +2 -2
- metadata +38 -6
data/README.md
CHANGED
@@ -17,9 +17,7 @@ rails g dynamic_query:helper
|
|
17
17
|
|
18
18
|
``` ruby
|
19
19
|
## controller of Rails 3
|
20
|
-
dq = dynamic_query(
|
21
|
-
# Since version 0.4.0, you can use model's names directly.
|
22
|
-
dq = dynamic_query(List, Entry)
|
20
|
+
dq = dynamic_query(List, Entry) # put models you wish to be queried here.
|
23
21
|
|
24
22
|
@panel = dq.panel(params[:query])
|
25
23
|
@lists = List.includes(:entries).where(dq.statement(params[:query])).all
|
@@ -36,20 +34,30 @@ dq = dynamic_query(List, Entry)
|
|
36
34
|
``` ruby
|
37
35
|
# columns with name 'id' or name ended with '_id' are hided by default
|
38
36
|
# :reveal_keys => true reveals those columns in the query panel
|
39
|
-
dq = dynamic_query(:list, :entry, :reveal_keys => true)
|
40
|
-
# Works for version >= 0.4.0
|
41
37
|
dq = dynamic_query(List, Entry, :reveal_keys => true)
|
42
38
|
|
43
39
|
# a white list or a black list can be defined by following options
|
44
|
-
dq = dynamic_query(:list, :entry, :accept => { :list => [:name], :entry => [:title, :priority] }, :reject => { :entry => [:title] })
|
45
|
-
# only lists.name and entries.priority can be seen on the query panel because the white list gets higher precedence than the black list
|
46
|
-
# Works for version >= 0.4.0
|
47
40
|
dq = dynamic_query(List, Entry, :accept => { List => [:name], Entry => [:title, :priority] }, :reject => { Entry => [:title] })
|
41
|
+
# only lists.name and entries.priority can be seen on the query panel because the white list gets higher precedence than the black list
|
48
42
|
|
49
43
|
# column display names can be defined by following options
|
50
|
-
dq = dynamic_query(
|
44
|
+
dq = dynamic_query(List, Entry, :alias => { 'lists.name' => 'Full Name' })
|
51
45
|
# those names are used in the html select tag
|
52
46
|
|
47
|
+
## join tables with arbitrary columns and query
|
48
|
+
dq = dynamic_query(Model1, Mode2)
|
49
|
+
@panel = dq.panel(params[:query])
|
50
|
+
@records = Model1.
|
51
|
+
select(all_columns_in(Model1, Model2)).
|
52
|
+
# all_columns_in is a method to help user select all columns within models
|
53
|
+
# you must select columns of tables you joined, otherwise columns won't show in the result
|
54
|
+
joins(tables_joined_by(Model1, [Model2, [:col1, :col1], [:my_col, :your_col], :col3, :col4])).
|
55
|
+
# tables_joined_by is a method to help user build inner join SQL statement between tables
|
56
|
+
# this demonstrates how to join two tables by following correspondence columns
|
57
|
+
# :col1 -> :col1, :my_col -> :your_col, :col3 -> :col3, :col4 -> :col4
|
58
|
+
# if columns have the same name in 2 tables, you can simply only enter there common name
|
59
|
+
where(dq.statement(params[:query])).all
|
60
|
+
|
53
61
|
## query panel is simply a rails form_tag which means it can accept the same hash options
|
54
62
|
<%= dynamic_query @panel, :remote => true %>
|
55
63
|
```
|
data/Rakefile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
<center>
|
2
|
-
<table id="dynamic_result_table">
|
2
|
+
<table id="dynamic_result_table" style="text-align: center">
|
3
3
|
<% if result.first %>
|
4
4
|
<tr>
|
5
5
|
<%= result.first.attributes.map { |k, _| "<th>#{k}</th>" }.join.html_safe %>
|
@@ -15,6 +15,53 @@
|
|
15
15
|
<button id="table2csv">Download CSV</button>
|
16
16
|
|
17
17
|
<script>
|
18
|
+
$( function() {
|
19
|
+
if ( sessionStorage.getItem( 'toggle_columns' ) == null ) {
|
20
|
+
sessionStorage.setItem( 'toggle_columns', '[]' )
|
21
|
+
}
|
22
|
+
console.log( sessionStorage.getItem( 'toggle_columns' ) );
|
23
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
24
|
+
|
25
|
+
$.each( hide_ary, function(index, value) {
|
26
|
+
var header = $("#dynamic_result_table tr > th:eq(" + value + ")");
|
27
|
+
header.attr( 'title', header.text() );
|
28
|
+
header.text( '●' );
|
29
|
+
$('#dynamic_result_table tr').find( "td:eq(" + value +")" ).each( function() {
|
30
|
+
$( this ).attr( 'title', $( this ).text() );
|
31
|
+
$( this ).text( '' );
|
32
|
+
});
|
33
|
+
});
|
34
|
+
|
35
|
+
$( '#dynamic_result_table th' ).each( function(index) {
|
36
|
+
$( this ).on( 'click', function() {
|
37
|
+
var header = $("#dynamic_result_table tr > th:eq(" + index + ")");
|
38
|
+
if ( header.text() == '●' ) {
|
39
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
40
|
+
hide_ary.splice( hide_ary.indexOf(index), 1 );
|
41
|
+
sessionStorage.setItem( 'toggle_columns', JSON.stringify( hide_ary ) )
|
42
|
+
|
43
|
+
header.text( header.attr( 'title' ) );
|
44
|
+
header.attr( 'title', '' );
|
45
|
+
$('#dynamic_result_table tr').find( "td:eq(" + index +")" ).each( function() {
|
46
|
+
$( this ).text( $( this ).attr( 'title' ) );
|
47
|
+
$( this ).attr( 'title', '' );
|
48
|
+
});
|
49
|
+
} else {
|
50
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
51
|
+
hide_ary.push( index );
|
52
|
+
sessionStorage.setItem( 'toggle_columns', JSON.stringify( hide_ary ) )
|
53
|
+
|
54
|
+
header.attr( 'title', header.text() );
|
55
|
+
header.text( '●' );
|
56
|
+
$('#dynamic_result_table tr').find( "td:eq(" + index +")" ).each( function() {
|
57
|
+
$( this ).attr( 'title', $( this ).text() );
|
58
|
+
$( this ).text( '' );
|
59
|
+
});
|
60
|
+
}
|
61
|
+
});
|
62
|
+
});
|
63
|
+
});
|
64
|
+
|
18
65
|
var regulateCSVCell = function( cell ) {
|
19
66
|
if ( cell.indexOf( ',' ) == -1 ) {
|
20
67
|
return cell;
|
data/lib/dynamic_query.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'dynamic_query/railtie' if defined?(Rails)
|
2
2
|
require 'generators/helper_generator' if defined?(Rails)
|
3
|
-
require 'dynamic_query/
|
3
|
+
require 'dynamic_query/combined_query'
|
4
4
|
require 'dynamic_query/validator'
|
5
|
+
require 'arel'
|
5
6
|
|
6
7
|
module DynamicQuery
|
7
8
|
OPERATOR = ['=', '>', '>=', '<', '<=', '!=',
|
@@ -11,9 +12,48 @@ module DynamicQuery
|
|
11
12
|
def dynamic_query(*models, opt)
|
12
13
|
DynamicQueryInstance.new(*models, opt)
|
13
14
|
end
|
14
|
-
|
15
|
+
|
16
|
+
def tables_joined_by(from, *to_tables)
|
17
|
+
sql = ''
|
18
|
+
from_table = from.table_name
|
19
|
+
to_tables_count = Hash.new(0)
|
20
|
+
to_tables.each do |to|
|
21
|
+
to_table = to.first.table_name
|
22
|
+
if to_tables_count[to_table] == 0
|
23
|
+
sql << "INNER JOIN `#{to_table}` ON "
|
24
|
+
mappings = []
|
25
|
+
to.drop(1).each do |mapping|
|
26
|
+
if mapping.kind_of? Array
|
27
|
+
mappings << "`#{from_table}`.`#{mapping.first}` = `#{to_table}`.`#{mapping.last}`"
|
28
|
+
else
|
29
|
+
mappings << "`#{from_table}`.`#{mapping}` = `#{to_table}`.`#{mapping}`"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
sql << "(#{mappings.join(' AND ')})"
|
33
|
+
to_tables_count[to_table] += 1
|
34
|
+
else
|
35
|
+
sql << "INNER JOIN `#{to_table}` `#{to_table}_#{to_tables_count[to_table]}` ON "
|
36
|
+
mappings = []
|
37
|
+
to.drop(1).each do |mapping|
|
38
|
+
if mapping.kind_of? Array
|
39
|
+
mappings << "`#{from_table}`.`#{mapping.first}` = `#{to_table}_#{to_tables_count[to_table]}`.`#{mapping.last}`"
|
40
|
+
else
|
41
|
+
mappings << "`#{from_table}`.`#{mapping}` = `#{to_table}_#{to_tables_count[to_table]}`.`#{mapping}`"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
sql << "(#{mappings.join(' AND ')})"
|
45
|
+
to_tables_count[to_table] += 1
|
46
|
+
end
|
47
|
+
end
|
48
|
+
sql
|
49
|
+
end
|
50
|
+
|
51
|
+
def all_columns_in(*models)
|
52
|
+
models.map { |m| m.columns.map { |col| "#{m.table_name}.#{col.name}" } }.flatten.join ', '
|
53
|
+
end
|
54
|
+
|
15
55
|
class DynamicQueryInstance
|
16
|
-
include
|
56
|
+
include CombinedQuery, Validator
|
17
57
|
|
18
58
|
def initialize(*models, opt)
|
19
59
|
@reveal_keys = false
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 2.2.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: will_paginate
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
63
|
name: sqlite3
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,6 +75,22 @@ dependencies:
|
|
59
75
|
- - ~>
|
60
76
|
- !ruby/object:Gem::Version
|
61
77
|
version: 1.3.7
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: mysql2
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 0.3.11
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.3.11
|
62
94
|
- !ruby/object:Gem::Dependency
|
63
95
|
name: shoulda
|
64
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,7 +161,7 @@ executables: []
|
|
129
161
|
extensions: []
|
130
162
|
extra_rdoc_files: []
|
131
163
|
files:
|
132
|
-
- lib/dynamic_query/
|
164
|
+
- lib/dynamic_query/combined_query.rb
|
133
165
|
- lib/dynamic_query/helper.rb
|
134
166
|
- lib/dynamic_query/joiner.rb
|
135
167
|
- lib/dynamic_query/railtie.rb
|
@@ -159,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
191
|
version: '0'
|
160
192
|
segments:
|
161
193
|
- 0
|
162
|
-
hash:
|
194
|
+
hash: 310380793727581897
|
163
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
196
|
none: false
|
165
197
|
requirements:
|
@@ -168,11 +200,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
200
|
version: '0'
|
169
201
|
segments:
|
170
202
|
- 0
|
171
|
-
hash:
|
203
|
+
hash: 310380793727581897
|
172
204
|
requirements: []
|
173
205
|
rubyforge_project:
|
174
206
|
rubygems_version: 1.8.25
|
175
207
|
signing_key:
|
176
208
|
specification_version: 3
|
177
|
-
summary: dynamic_query-0.
|
209
|
+
summary: dynamic_query-0.7.0
|
178
210
|
test_files: []
|