dynamic_query 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,7 +22,7 @@ rails g dynamic_query:helper
22
22
  ## controller of Rails 3
23
23
  dq = dynamic_query(:list, :entry) # list models you wish to be queried. e.g. List => :list, AbcDef => :abc_def
24
24
  @panel = dq.panel(params[:query])
25
- @lists = List.includes(:entries).where(dq.statement(params[:query]))
25
+ @lists = List.includes(:entries).where(dq.statement(params[:query])).all
26
26
 
27
27
  ## render query panel in the view:
28
28
  <%= dynamic_query @panel %>
@@ -39,6 +39,10 @@ dq = dynamic_query(:list, :entry, :reveal_keys => true)
39
39
  dq = dynamic_query(:list, :entry, :accept => { :list => [:name], :entry => [:title, :priority] }, :reject => { :entry => [:title] })
40
40
  # only lists.name and entries.priority can be seen on the query panel because the white list gets higher precedence than the black list
41
41
 
42
+ # column display names can be defined by following options
43
+ dq = dynamic_query(:list, :entry, :alias => { 'lists.name' => 'Full Name' })
44
+ # those names are used in the html select tag
45
+
42
46
  ## query panel is simply a rails form_tag which means it can accept the same hash options
43
47
  <%= dynamic_query @panel, :remote => true %>
44
48
  ```
@@ -8,7 +8,7 @@
8
8
  <%= submit_tag 'ǁ', :name => "query[action][add_or]", :style => 'color: purple;', :title => 'OR' if idx1 == 0 && idx2 == 0 %>
9
9
  <%= submit_tag '-', :name => "query[action][remove_#{or_key}]", :style => 'color: red;' if idx1 != 0 && idx2 == 0 %>
10
10
  </td>
11
- <td><%= select_tag "query[#{or_key}][#{and_key}][column]", options_for_select(panel[:columns].keys, and_val[:column]) %></td>
11
+ <td><%= select_tag "query[#{or_key}][#{and_key}][column]", options_for_select(panel[:columns], and_val[:column]) %></td>
12
12
  <td><%= select_tag "query[#{or_key}][#{and_key}][operator]", options_for_select(DynamicQuery::OPERATOR, and_val[:operator]), :class => 'query_op' %></td>
13
13
  <td>
14
14
  <% if ['IS NULL', 'IS NOT NULL'].include? and_val[:operator] %>
@@ -1,9 +1,7 @@
1
1
  module DynamicQueryHelper
2
-
3
2
  def dynamic_query(panel, opt = {})
4
3
  template = ERB.new(File.read(File.dirname(__FILE__) + '/dynamic_query.erb'))
5
4
  content = template.result(binding)
6
5
  content.html_safe
7
6
  end
8
-
9
7
  end
data/lib/dynamic_query.rb CHANGED
@@ -11,18 +11,18 @@ module DynamicQuery
11
11
  end
12
12
 
13
13
  class DynamicQueryInstance
14
-
15
14
  def initialize(*models, opt)
16
15
  @reveal_keys = false
17
16
  @white_list = []
18
17
  @black_list = []
18
+ @aliases = {}
19
19
 
20
- if opt.kind_of?(Array)
20
+ if opt.kind_of? Array
21
21
  models = models + opt
22
22
  opt = {}
23
23
  end
24
24
 
25
- unless opt.kind_of?(Hash)
25
+ unless opt.kind_of? Hash
26
26
  models << opt
27
27
  opt = {}
28
28
  end
@@ -32,7 +32,7 @@ module DynamicQuery
32
32
 
33
33
  if key == :accept
34
34
  val.each do |model, columns|
35
- if models.include?(model)
35
+ if models.include? model
36
36
  model = model.to_s.split(/_/).map { |word| word.capitalize }.join.constantize
37
37
  columns.each { |col| @white_list << "#{model.table_name}.#{col}" }
38
38
  end
@@ -41,36 +41,60 @@ module DynamicQuery
41
41
 
42
42
  if key == :reject
43
43
  val.each do |model, columns|
44
- if models.include?(model)
44
+ if models.include? model
45
45
  model = model.to_s.split(/_/).map { |word| word.capitalize }.join.constantize
46
46
  columns.each { |col| @black_list << "#{model.table_name}.#{col}" }
47
47
  end
48
48
  end
49
49
  end
50
- end if opt.kind_of?(Hash)
50
+
51
+ if key == :alias
52
+ @aliases = val.kind_of?(Hash) ? val.clone : {}
53
+ end
54
+ end if opt.kind_of? Hash
51
55
 
52
56
  @columns = {}
53
57
  models.each do |model|
54
58
  model = model.to_s.split(/_/).map { |word| word.capitalize }.join.constantize
55
- model = model.columns.map { |col| { "#{model.table_name}.#{col.name}" => col.type } }.reduce(:merge)
56
- @columns.merge!(model)
59
+ model = Hash[model.columns.map { |col| ["#{model.table_name}.#{col.name}"] * 2 }]
60
+ @columns.merge! model
61
+ end
62
+
63
+ @column_types = {}
64
+ models.each do |model|
65
+ model = model.to_s.split(/_/).map { |word| word.capitalize }.join.constantize
66
+ model = Hash[model.columns.map { |col| ["#{model.table_name}.#{col.name}", col.type] }]
67
+ @column_types.merge! model
57
68
  end
58
69
 
59
70
  unless @reveal_keys
60
71
  @columns.keys.each do |key|
61
- @columns.delete(key) if key =~ /\.id$/ || key =~ /_id$/
72
+ @columns.delete key if key =~ /\.id$/ || key =~ /_id$/
62
73
  end
63
74
  end
64
75
 
65
76
  unless @white_list.empty?
66
77
  selected_columns = {}
67
78
  @white_list.each do |white|
68
- selected_columns[white] = @columns[white] if @columns.keys.include?(white)
79
+ selected_columns[white] = @columns[white] if @columns.keys.include? white
69
80
  end
70
81
  @columns = selected_columns
71
82
  end
72
83
 
73
- @black_list.each { |black| @columns.delete(black) }
84
+ @black_list.each { |black| @columns.delete black }
85
+
86
+ translate_columns = {}
87
+ used_aliases = {}
88
+ @columns.each do |option, value|
89
+ if @aliases[option]
90
+ used_aliases[option] = @aliases[option]
91
+ translate_columns[@aliases[option]] = value
92
+ else
93
+ translate_columns[option] = value
94
+ end
95
+ end
96
+ @columns = translate_columns
97
+ @aliases = used_aliases
74
98
  end
75
99
 
76
100
  def panel(query)
@@ -94,7 +118,7 @@ module DynamicQuery
94
118
  query.each do |or_key, or_val|
95
119
  and_stat = [[]]
96
120
  or_val.each do |and_key, and_val|
97
- if @columns.include?(and_val[:column])
121
+ if @columns.include?(and_val[:column]) || @aliases.include?(and_val[:column])
98
122
  case and_val[:operator]
99
123
  when '=', '>', '>=', '<', '<=', '!=', 'LIKE', 'NOT LIKE'
100
124
  unless and_val[:value1].blank?
@@ -130,10 +154,7 @@ module DynamicQuery
130
154
  params.unshift(stat.join(' OR '))
131
155
  end
132
156
 
133
-
134
157
  private
135
-
136
-
137
158
  def filter_valid_info(query)
138
159
  output = {}
139
160
 
@@ -179,7 +200,5 @@ module DynamicQuery
179
200
  count = and_conditions.keys.map { |key| key.match(/\d+/)[0].to_i }.max.to_i + 1
180
201
  "and_#{count}"
181
202
  end
182
-
183
203
  end
184
-
185
204
  end
@@ -1,13 +1,9 @@
1
1
  module DynamicQuery
2
-
3
2
  module Helper
4
-
5
3
  def dynamic_query(panel, opt = {})
6
- template = ERB.new(File.read(File.dirname(__FILE__) + '/dynamic_query.erb'))
4
+ template = ERB.new(File.read(File.dirname(__FILE__) + '/../../app/helpers/dynamic_query.erb'))
7
5
  content = template.result(binding)
8
6
  content.html_safe
9
7
  end
10
-
11
8
  end
12
-
13
9
  end
@@ -1,17 +1,13 @@
1
1
  require 'dynamic_query/helper'
2
2
 
3
3
  module DynamicQuery
4
-
5
4
  class Railtie < Rails::Railtie
6
-
7
- initializer "dynamic_query" do
5
+ initializer 'dynamic_query' do
8
6
  ActionController::Base.send :include, DynamicQuery
9
7
  end
10
8
 
11
- initializer "dynamic_query.helper" do
9
+ initializer 'dynamic_query.helper' do
12
10
  ActionView::Base.send :include, Helper
13
11
  end
14
-
15
12
  end
16
-
17
13
  end
@@ -1,11 +1,9 @@
1
1
  class DynamicQuery
2
-
3
2
  module Version
4
3
  MAJOR = 0
5
4
  MINOR = 3
6
- PATCH = 1
5
+ PATCH = 2
7
6
 
8
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
9
8
  end
10
-
11
9
  end
@@ -4,7 +4,6 @@ class DynamicQueryGenerator < Rails::Generators::Base
4
4
  source_root File.expand_path("../../../", __FILE__)
5
5
 
6
6
  def manifest
7
- copy_file "app/assets/javascripts/dynamic_query.js", "app/assets/javascripts/dynamic_query.js"
7
+ copy_file 'app/assets/javascripts/dynamic_query.js', 'app/assets/javascripts/dynamic_query.js'
8
8
  end
9
-
10
9
  end
@@ -1,15 +1,12 @@
1
1
  require 'rails/generators'
2
2
 
3
3
  module DynamicQuery
4
-
5
4
  class HelperGenerator < Rails::Generators::Base
6
- source_root File.expand_path("../../../", __FILE__)
5
+ source_root File.expand_path('../../../', __FILE__)
7
6
 
8
7
  def manifest
9
- copy_file "app/helpers/dynamic_query_helper.rb", "app/helpers/dynamic_query_helper.rb"
10
- copy_file "app/helpers/dynamic_query.erb", "app/helpers/dynamic_query.erb"
8
+ copy_file 'app/helpers/dynamic_query_helper.rb', 'app/helpers/dynamic_query_helper.rb'
9
+ copy_file 'app/helpers/dynamic_query.erb', 'app/helpers/dynamic_query.erb'
11
10
  end
12
-
13
11
  end
14
-
15
12
  end
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.3.1
4
+ version: 0.3.2
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: 2012-10-25 00:00:00.000000000 Z
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -122,7 +122,6 @@ files:
122
122
  - app/helpers/dynamic_query.erb
123
123
  - app/helpers/dynamic_query_helper.rb
124
124
  - lib/dynamic_query.rb
125
- - lib/dynamic_query/dynamic_query.erb
126
125
  - lib/dynamic_query/helper.rb
127
126
  - lib/dynamic_query/railtie.rb
128
127
  - lib/dynamic_query/version.rb
@@ -143,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
142
  version: '0'
144
143
  segments:
145
144
  - 0
146
- hash: 1729559994982098119
145
+ hash: 608810096619642963
147
146
  required_rubygems_version: !ruby/object:Gem::Requirement
148
147
  none: false
149
148
  requirements:
@@ -155,5 +154,5 @@ rubyforge_project:
155
154
  rubygems_version: 1.8.24
156
155
  signing_key:
157
156
  specification_version: 3
158
- summary: dynamic_query-0.3.1
157
+ summary: dynamic_query-0.3.2
159
158
  test_files: []
@@ -1,39 +0,0 @@
1
- <%= form_tag(request.path, { :method => :get, :style => 'text-align: center;' }.merge(opt)) %>
2
- <% if panel %>
3
- <table style="margin-left: auto; margin-right: auto;">
4
- <% panel.select { |k, _| k =~ /^or_\d+$/ }.each_with_index do |(or_key, or_val), idx1| %>
5
- <% or_val.each_with_index do |(and_key, and_val), idx2| %>
6
- <tr>
7
- <td>
8
- <%= submit_tag 'ǁ', :name => "query[action][add_or]", :style => 'color: purple;', :title => 'OR' if idx1 == 0 && idx2 == 0 %>
9
- <%= submit_tag '-', :name => "query[action][remove_#{or_key}]", :style => 'color: red;' if idx1 != 0 && idx2 == 0 %>
10
- </td>
11
- <td><%= select_tag "query[#{or_key}][#{and_key}][column]", options_for_select(panel[:columns].keys, and_val[:column]) %></td>
12
- <td><%= select_tag "query[#{or_key}][#{and_key}][operator]", options_for_select(OPERATOR, and_val[:operator]), :class => 'query_op' %></td>
13
- <td>
14
- <% if ['IS NULL', 'IS NOT NULL'].include? and_val[:operator] %>
15
- <%= text_field_tag "query[#{or_key}][#{and_key}][value1]", '', :style => 'display: none;' %>
16
- <% else %>
17
- <%= text_field_tag "query[#{or_key}][#{and_key}][value1]", and_val[:value1] %>
18
- <% end %>
19
- </td>
20
- <td>
21
- <% if ['BETWEEN', 'NOT BETWEEN'].include? and_val[:operator] %>
22
- <%= text_field_tag "query[#{or_key}][#{and_key}][value2]", and_val[:value2] %>
23
- <% else %>
24
- <%= text_field_tag "query[#{or_key}][#{and_key}][value2]", '', :style => 'display: none;' %>
25
- <% end %>
26
- </td>
27
- <td>
28
- <%= submit_tag '+', :name => "query[action][add_and_to_#{or_key}]", :style => 'color: blue;', :title => 'AND' if idx2 == 0 %>
29
- <%= submit_tag 'x', :name => "query[action][remove_#{and_key}_from_#{or_key}]", :style => 'color: green;' if idx2 != 0 %>
30
- </td>
31
- </tr>
32
- <% end %>
33
- <tr style="height: 10px;"></tr>
34
- <% end %>
35
- <% end %>
36
- </table>
37
- <br />
38
- <%= submit_tag 'Query', :style => 'width: 150px;'%>
39
- </form>