mighty_grid 1.0.0 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/README.md +20 -12
  4. data/features/support/env.rb +1 -0
  5. data/lib/generators/mighty_grid/install_generator.rb +1 -1
  6. data/lib/generators/mighty_grid/templates/{mighty_grid_config.rb → mighty_grid.rb} +1 -1
  7. data/lib/mighty_grid/base.rb +31 -111
  8. data/lib/mighty_grid/column.rb +1 -1
  9. data/lib/mighty_grid/engine.rb +11 -8
  10. data/lib/mighty_grid/exceptions.rb +9 -0
  11. data/lib/mighty_grid/filter_renderer.rb +18 -34
  12. data/lib/mighty_grid/filters/base.rb +27 -0
  13. data/lib/mighty_grid/filters/boolean_filter.rb +6 -0
  14. data/lib/mighty_grid/filters/custom_filter.rb +19 -0
  15. data/lib/mighty_grid/filters/enum_filter.rb +15 -0
  16. data/lib/mighty_grid/filters/string_filter.rb +6 -0
  17. data/lib/mighty_grid/filters/text_filter.rb +6 -0
  18. data/lib/mighty_grid/filters.rb +113 -0
  19. data/lib/mighty_grid/grid_renderer.rb +1 -1
  20. data/lib/mighty_grid/helpers/{mighty_grid_view_helpers.rb → view_helpers.rb} +13 -11
  21. data/lib/mighty_grid/map_type.rb +16 -0
  22. data/lib/mighty_grid/parameters.rb +58 -0
  23. data/lib/mighty_grid/version.rb +12 -1
  24. data/lib/mighty_grid.rb +64 -15
  25. data/spec/dummy/app/controllers/users_controller.rb +1 -1
  26. data/spec/dummy/app/grids/users_grid.rb +7 -0
  27. data/spec/dummy/app/views/users/index.html +1 -1
  28. data/spec/dummy/config/database.yml +29 -0
  29. data/spec/dummy/db/mg_test.sqlite3 +0 -0
  30. data/spec/dummy/log/development.log +119680 -0
  31. data/spec/dummy/log/test.log +101833 -0
  32. data/spec/dummy/tmp/cache/assets/CCA/5E0/sprockets%2F48d34122dd1905305a6e63e3acb7c042 +0 -0
  33. data/spec/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  34. data/spec/dummy/tmp/cache/assets/CF9/100/sprockets%2F3d8f5e96de4712db63b98083f75739c3 +0 -0
  35. data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  36. data/spec/dummy/tmp/cache/assets/D3C/EE0/sprockets%2Fccfd910338b6dcf575467d3009b46a0d +0 -0
  37. data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  38. data/spec/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  39. data/spec/dummy/tmp/cache/assets/D60/BE0/sprockets%2F37b5ab8193eccd1a29a0c61374c6b51e +0 -0
  40. data/spec/dummy/tmp/cache/assets/D82/820/sprockets%2Ff7c8a51f7f95d66a9d5112f898bc1fd0 +0 -0
  41. data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  42. data/spec/dummy/tmp/cache/assets/DF2/A80/sprockets%2F14a708b3bb2a924afdebae773e4ba5d8 +0 -0
  43. data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  44. data/spec/dummy/tmp/cache/assets/E0C/4E0/sprockets%2Fed4e3dbab857786229b1bdf7bbd289fe +0 -0
  45. data/spec/dummy/tmp/cache/assets/E14/600/sprockets%2F34cd0de2e0cd2c01e1c91b0ece565deb +0 -0
  46. data/spec/dummy/tmp/cache/assets/development/sprockets/0e57c0129c0245eab3972d87efc6a9b9 +0 -0
  47. data/spec/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  48. data/spec/dummy/tmp/cache/assets/development/sprockets/14a708b3bb2a924afdebae773e4ba5d8 +0 -0
  49. data/spec/dummy/tmp/cache/assets/development/sprockets/1f8135a48e7ccbcb5361bb9e1f993929 +0 -0
  50. data/spec/dummy/tmp/cache/assets/development/sprockets/2479f30262de64213c228dc752440fd1 +0 -0
  51. data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  52. data/spec/dummy/tmp/cache/assets/development/sprockets/34cd0de2e0cd2c01e1c91b0ece565deb +0 -0
  53. data/spec/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  54. data/spec/dummy/tmp/cache/assets/development/sprockets/37b5ab8193eccd1a29a0c61374c6b51e +0 -0
  55. data/spec/dummy/tmp/cache/assets/development/sprockets/3cbb6c28c7bbcc7c9b547346a59b4e7f +0 -0
  56. data/spec/dummy/tmp/cache/assets/development/sprockets/3d8f5e96de4712db63b98083f75739c3 +0 -0
  57. data/spec/dummy/tmp/cache/assets/development/sprockets/422d4d422d090c103e6b198600a90dac +0 -0
  58. data/spec/dummy/tmp/cache/assets/development/sprockets/48d34122dd1905305a6e63e3acb7c042 +0 -0
  59. data/spec/dummy/tmp/cache/assets/development/sprockets/a8c1ee1faf09397765710724fe3ee140 +0 -0
  60. data/spec/dummy/tmp/cache/assets/development/sprockets/bbd98f4797053ba2accbbca7b81cbbaf +0 -0
  61. data/spec/dummy/tmp/cache/assets/development/sprockets/cc03506b8188f839058f41aa8a4e19da +0 -0
  62. data/spec/dummy/tmp/cache/assets/development/sprockets/cccf51971e81f175e527083a6ef4624d +0 -0
  63. data/spec/dummy/tmp/cache/assets/development/sprockets/ccfd910338b6dcf575467d3009b46a0d +0 -0
  64. data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  65. data/spec/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  66. data/spec/dummy/tmp/cache/assets/development/sprockets/d77d41650e5528f12146c728566cbf33 +0 -0
  67. data/spec/dummy/tmp/cache/assets/development/sprockets/ed4e3dbab857786229b1bdf7bbd289fe +0 -0
  68. data/spec/dummy/tmp/cache/assets/development/sprockets/f7c8a51f7f95d66a9d5112f898bc1fd0 +0 -0
  69. data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  70. data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  71. data/spec/dummy/tmp/cache/assets/test/sprockets/14a708b3bb2a924afdebae773e4ba5d8 +0 -0
  72. data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  73. data/spec/dummy/tmp/cache/assets/test/sprockets/34cd0de2e0cd2c01e1c91b0ece565deb +0 -0
  74. data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  75. data/spec/dummy/tmp/cache/assets/test/sprockets/37b5ab8193eccd1a29a0c61374c6b51e +0 -0
  76. data/spec/dummy/tmp/cache/assets/test/sprockets/3d8f5e96de4712db63b98083f75739c3 +0 -0
  77. data/spec/dummy/tmp/cache/assets/test/sprockets/422d4d422d090c103e6b198600a90dac +0 -0
  78. data/spec/dummy/tmp/cache/assets/test/sprockets/48d34122dd1905305a6e63e3acb7c042 +0 -0
  79. data/spec/dummy/tmp/cache/assets/test/sprockets/a8c1ee1faf09397765710724fe3ee140 +0 -0
  80. data/spec/dummy/tmp/cache/assets/test/sprockets/cccf51971e81f175e527083a6ef4624d +0 -0
  81. data/spec/dummy/tmp/cache/assets/test/sprockets/ccfd910338b6dcf575467d3009b46a0d +0 -0
  82. data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  83. data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  84. data/spec/dummy/tmp/cache/assets/test/sprockets/ed4e3dbab857786229b1bdf7bbd289fe +0 -0
  85. data/spec/dummy/tmp/cache/assets/test/sprockets/f7c8a51f7f95d66a9d5112f898bc1fd0 +0 -0
  86. data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  87. data/spec/lib/base_spec.rb +24 -15
  88. data/spec/lib/generators/install_generator_spec.rb +5 -3
  89. data/spec/lib/tmp/config/initializers/mighty_grid.rb +15 -0
  90. data/spec/lib/tmp/config/locales/mighty_grid.en.yml +6 -0
  91. data/spec/mighty_grid_spec.rb +17 -0
  92. data/spec/support/models.rb +1 -0
  93. metadata +154 -41
  94. data/.gitignore +0 -26
  95. data/.rspec +0 -2
  96. data/.travis.yml +0 -33
  97. data/Appraisals +0 -17
  98. data/gemfiles/rails_32.gemfile +0 -26
  99. data/gemfiles/rails_40.gemfile +0 -26
  100. data/gemfiles/rails_41.gemfile +0 -26
  101. data/lib/mighty_grid/config.rb +0 -49
  102. data/lib/mighty_grid/mighty_grid_controller.rb +0 -14
  103. data/lib/mighty_grid/mighty_grid_misc.rb +0 -11
  104. data/mighty_grid.gemspec +0 -27
  105. data/spec/config_spec.rb +0 -148
  106. data/spec/dummy/app/assets/images/.keep +0 -0
  107. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  108. data/spec/dummy/app/mailers/.keep +0 -0
  109. data/spec/dummy/app/models/.keep +0 -0
  110. data/spec/dummy/app/models/concerns/.keep +0 -0
  111. data/spec/dummy/lib/assets/.keep +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6f6866acde2692702bbbf6885eb459ed8960c43
4
- data.tar.gz: c8af8d724f7cf2db8edda2fdeed240682b753ed6
3
+ metadata.gz: ae66ac068670599398526077c090a43897244668
4
+ data.tar.gz: 599f7821d5b8c9c008313738d1b3ccb19e10aa0a
5
5
  SHA512:
6
- metadata.gz: af481f7256ba0b787d976f53a0586250d1c1944d30db94f0b896a41a6f7835c455e0db346129564f8acbca155e4f2652c181bc4c496f0ee8494a1a79658c47f6
7
- data.tar.gz: 78cf52302e3c844da064560dca2adcb5ceb18f1fe5b452f6d770331568417c6f91f5a73ba01fbec2ec340dae7f915b2992fc586d2ba7691a391c659b5f3f869b
6
+ metadata.gz: 4a41aaade69e5ad4e51d84c46291288064f997abcd9a5d97a1c30a053bf9c5a8ba1b3e0e767195b3b089022075ce4f457999235b9124c072d26b1be2b972fd57
7
+ data.tar.gz: ae204f9bac3fb47080dc7c21a96f325121c44919f1ea86d98bb98c28c21b6b51d9d5058fff02e7e970e0ac348ab1a35fd7518d1ebf61099f7613babaa1a4181d
data/Gemfile CHANGED
@@ -19,4 +19,6 @@ group :test do
19
19
  gem "codeclimate-test-reporter", require: nil
20
20
  gem 'pg', require: false
21
21
  gem 'mysql2', require: false
22
+
23
+ gem 'pry'
22
24
  end
data/README.md CHANGED
@@ -8,23 +8,34 @@ Add this line to your application's Gemfile:
8
8
 
9
9
  gem 'mighty_grid'
10
10
 
11
- And then execute:
11
+ Then run the following generator command in order to generate files for gem customization:
12
12
 
13
- $ bundle
13
+ $ rails g mighty_grid:install
14
14
 
15
- Or install it yourself as:
15
+ After generation you will see the following files:
16
16
 
17
- $ gem install mighty_grid
17
+ * `config/initializers/mighty_grid.rb`
18
+ * `config/locales/mighty_grid.en.yml`
18
19
 
19
20
  ## Quick Start
20
21
 
22
+ ### Grid
23
+
24
+ Any grid can be defined in a folder `app/grids` for example as follows:
25
+
26
+ ```
27
+ Class ProductsGrid < MightyGrid::Base
28
+ scope { Product }
29
+ end
30
+ ```
31
+
21
32
  ### Controller
22
33
 
23
- You can define class or relation in <tt>init_grid</tt> method.
34
+ You can initialize grid like this:
24
35
 
25
36
  ```
26
37
  def index
27
- @products_grid = init_grid(Product)
38
+ @products_grid = ProductsGrid.new(params)
28
39
  end
29
40
  ```
30
41
 
@@ -42,7 +53,7 @@ end
42
53
 
43
54
  ### General configuration options
44
55
 
45
- You can configure the following default values by overriding these values using <tt>MightyGrid.configure</tt> method.
56
+ You can configure the following default values by overriding these values using <tt>MightyGrid.setup</tt> method.
46
57
 
47
58
  ```
48
59
  per_page # 15 by default
@@ -61,20 +72,17 @@ pagination_theme # 'mighty_grid' by default
61
72
  ```
62
73
 
63
74
  There's a handy generator that generates the default configuration file into config/initializers directory.
64
- Run the following generator command, then edit the generated file.
65
-
66
- $ rails g mighty_grid:install
67
75
 
68
76
  ## Running tests
69
77
 
70
78
  To run the tests you need specify database and Rails version.
71
79
 
72
- * List of available Rails versions: 3.2, 4.0, 4.1.
80
+ * List of available Rails versions: 4.0, 4.1, 4.2.
73
81
  * List of DB: sqlite, postgresql, mysql.
74
82
 
75
83
  Example run:
76
84
 
77
- $ DB=postgresql appraisal rails_32 rake spec cucumber
85
+ $ DB=postgresql appraisal rails_41 rake spec cucumber
78
86
 
79
87
  ## Contributing
80
88
 
@@ -7,6 +7,7 @@ end
7
7
  require 'bundler/setup'
8
8
  Bundler.require
9
9
 
10
+ require 'rspec/matchers'
10
11
  require 'capybara/cucumber'
11
12
 
12
13
  if defined? Rails
@@ -8,7 +8,7 @@ module MightyGrid
8
8
  desc 'Copies MightyGrid configuration and locale files to your application.'
9
9
 
10
10
  def copy_config_file
11
- template 'mighty_grid_config.rb', 'config/initializers/mighty_grid_config.rb'
11
+ template 'mighty_grid.rb', 'config/initializers/mighty_grid.rb'
12
12
  end
13
13
 
14
14
  def copy_locale_file
@@ -1,4 +1,4 @@
1
- MightyGrid.configure do |config|
1
+ MightyGrid.setup do |config|
2
2
  # config.per_page = 15
3
3
  # config.order_direction = 'asc'
4
4
  # config.order_type = 'single'
@@ -1,17 +1,25 @@
1
1
  module MightyGrid
2
2
  class Base
3
- attr_reader :klass, :name, :relation, :options, :mg_params, :controller
3
+ include MightyGrid::Filters
4
+ include MightyGrid::Parameters
5
+
6
+ attr_reader :klass, :name, :relation, :options, :mg_params, :params, :controller
4
7
  attr_accessor :output_buffer, :filters
5
8
 
6
- def initialize(klass_or_relation, controller, opts = {}) #:nodoc:
7
- @controller = controller
9
+ def initialize(params, opts = {}) #:nodoc:
10
+ @controller_params = params
11
+
12
+ # Get active controller through params
13
+ if controller = "#{params[:controller].camelize}Controller".safe_constantize
14
+ @controller = ObjectSpace.each_object(controller).first
15
+ end
8
16
 
9
- @filters = {}
17
+ @filters = self.class.filters.dup
10
18
 
11
19
  @options = {
12
20
  page: 1,
13
- per_page: MightyGrid.config.per_page,
14
- name: MightyGrid.config.grid_name,
21
+ per_page: MightyGrid.per_page,
22
+ name: MightyGrid.grid_name,
15
23
  include: nil,
16
24
  joins: nil,
17
25
  conditions: nil,
@@ -22,15 +30,28 @@ module MightyGrid
22
30
  opts.assert_valid_keys(@options.keys)
23
31
  @options.merge!(opts)
24
32
 
25
- @name = @options[:name].to_s
33
+ @klass = self.class.klass
34
+ @relation = self.class.relation
26
35
 
27
- @relation = klass_or_relation
36
+ @relation = yield(@relation) if block_given?
28
37
 
29
- @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
38
+ @name = @options[:name].to_s
30
39
 
31
40
  load_grid_params
32
41
  end
33
42
 
43
+ class << self
44
+ attr_reader :klass, :relation
45
+
46
+ def scope(&block)
47
+ if block_given?
48
+ klass_or_relation = yield
49
+ @relation = klass_or_relation
50
+ @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
51
+ end
52
+ end
53
+ end
54
+
34
55
  def read
35
56
  apply_filters
36
57
  if @mg_params[:order].present? && current_order_direction.present?
@@ -48,110 +69,9 @@ module MightyGrid
48
69
  .group(@options[:group])
49
70
  end
50
71
 
51
- # Apply filters
52
- def apply_filters
53
- filter_params.each do |filter_name, filter_value|
54
- name, table_name = filter_name.split('.').reverse
55
-
56
- if table_name && Object.const_defined?(table_name.classify)
57
- model = table_name.classify.constantize
58
- else
59
- model = klass
60
- end
61
-
62
- next if filter_value.blank? || !model.column_names.include?(name)
63
-
64
- if model && model.superclass == ActiveRecord::Base
65
- field_type = model.columns_hash[name].type
66
- else
67
- next
68
- end
69
-
70
- table_name = model.table_name
71
- if @filters.key?(filter_name.to_sym) && @filters[filter_name.to_sym].is_a?(Array)
72
- @relation = @relation.where(table_name => { filter_name => filter_value })
73
- elsif field_type == :boolean
74
- value = %w(true 1 t).include?(filter_value) ? true : false
75
- @relation = @relation.where(table_name => { filter_name => value })
76
- elsif [:string, :text].include?(field_type)
77
- @relation = @relation.where("#{table_name}.#{name} #{like_operator} ?", "%#{filter_value}%")
78
- end
79
- end
80
- end
81
-
82
72
  # Get controller parameters
83
73
  def params
84
- @controller.params
85
- end
86
-
87
- # Load grid parameters
88
- def load_grid_params
89
- @mg_params = {}
90
- @mg_params[filter_param_name.to_sym] = {}
91
- @mg_params.merge!(@options)
92
- if current_grid_params
93
- @mg_params.merge!(current_grid_params.symbolize_keys)
94
- if @mg_params[:order].present? && !@mg_params[:order].to_s.include?('.')
95
- @mg_params[:order] = "#{klass.table_name}.#{@mg_params[:order]}"
96
- end
97
- end
98
- end
99
-
100
- # Get current grid parameter by name
101
- def get_current_grid_param(name)
102
- current_grid_params.key?(name) ? current_grid_params[name] : nil
103
- end
104
-
105
- # Get filter parameters
106
- def filter_params
107
- @mg_params[filter_param_name.to_sym] || {}
108
- end
109
-
110
- # Get filter parameter name
111
- def filter_param_name
112
- 'f'
113
- end
114
-
115
- # Add param in filters
116
- def add_filter_param(param, value)
117
- @mg_params[filter_param_name.to_sym][param] = value unless @mg_params[filter_param_name.to_sym].key?(param)
118
- end
119
-
120
- # Get filter name by field
121
- def get_filter_name(field, model = nil)
122
- field_name = model.present? ? "#{model.table_name}.#{field}" : field
123
- "#{name}[#{filter_param_name}][#{field_name}]"
124
- end
125
-
126
- # Get current grid parameters
127
- def current_grid_params
128
- params[name] || {}
129
- end
130
-
131
- # Get order parameters
132
- def order_params(attribute, model = nil, direction = nil)
133
- order = model.present? ? "#{model.table_name}.#{attribute}" : attribute.to_s
134
- direction ||= order == current_grid_params['order'] ? another_order_direction : 'asc'
135
- { @name => { order: order, order_direction: direction } }
136
- end
137
-
138
- # Get current order direction if current order parameter coincides with the received parameter
139
- def get_active_order_direction(parameters)
140
- parameters[@name]['order'] == current_grid_params['order'] ? current_order_direction : nil
141
- end
142
-
143
- # Get current order direction
144
- def current_order_direction
145
- direction = nil
146
- if current_grid_params.key?('order_direction') && %w(asc desc).include?(current_grid_params['order_direction'].downcase)
147
- direction = current_grid_params['order_direction'].downcase
148
- end
149
- direction
150
- end
151
-
152
- # Get another order direction
153
- def another_order_direction
154
- current_grid_params.key?('order_direction') ? (%w(asc desc) - [current_grid_params['order_direction'].to_s]).first : MightyGrid.config.order_direction
74
+ @controller_params
155
75
  end
156
76
 
157
77
  # Get <tt>like</tt> or <tt>ilike</tt> operator depending on the database adapter
@@ -18,7 +18,7 @@ module MightyGrid
18
18
  end
19
19
 
20
20
  @model = @options[:model]
21
- fail MightyGridArgumentError.new('Model of field for filtering should have type ActiveRecord') if @model && @model.superclass != ActiveRecord::Base
21
+ fail MightyGrid::Exceptions::ArgumentError.new('Model of field for filtering should have type ActiveRecord') if @model && @model.superclass != ActiveRecord::Base
22
22
 
23
23
  @attrs = @options[:html] if @options.key?(:html)
24
24
  @th_attrs = @options[:th_html] if @options.key?(:th_html)
@@ -1,18 +1,21 @@
1
1
  module MightyGrid
2
- class MightyGridEngine < ::Rails::Engine
3
- initializer 'mighty_grid_railtie.configure_rails_initialization' do |app|
4
-
5
- ActiveSupport.on_load :action_controller do
6
- ActionController::Base.send(:include, MightyGrid::Controller)
7
- end
2
+ class Engine < ::Rails::Engine
3
+ config.eager_load_namespaces << MightyGrid
8
4
 
5
+ initializer 'mighty_grid.helpers' do |app|
9
6
  ActiveSupport.on_load :action_view do
10
- ::ActionView::Base.class_eval { include MightyGrid::GridViewHelper }
11
-
12
7
  # It is here only until this pull request is pulled: https://github.com/amatsuda/kaminari/pull/267
13
8
  require 'mighty_grid/kaminari_monkey_patching'
14
9
  end
10
+ end
15
11
 
12
+ config.after_initialize do
13
+ unless MightyGrid.configured?
14
+ warn """
15
+ [Mighty Grid] Mighty Grid is not configured in the application and will use the default values.
16
+ Use `rails generate mighty_grid:install` to generate the Mighty Grid configuration.
17
+ """
18
+ end
16
19
  end
17
20
  end
18
21
  end
@@ -0,0 +1,9 @@
1
+ module MightyGrid
2
+ module Exceptions #:nodoc:
3
+ class ArgumentError < ::ArgumentError #:nodoc:
4
+ def initialize(str) #:nodoc:
5
+ super("MightyGrid: #{str}")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -11,41 +11,35 @@ module MightyGrid
11
11
  options = content_or_options if content_or_options.is_a?(Hash)
12
12
  human_name = (content_or_options.is_a?(Hash) || content_or_options.nil?) ? @grid.klass.human_attribute_name(name) : content_or_options
13
13
 
14
- f_options = filter_options(name, options, false)
15
-
16
- label_tag(get_filter_id(f_options), human_name, options, &block)
14
+ label_tag(get_filter_id(name), human_name, options, &block)
17
15
  end
18
16
 
19
17
  # Get <tt>input</tt> HTML tag
20
18
  def text_field(name, options = {})
21
- f_options = filter_options(name, options)
22
- text_field_tag(@grid.get_filter_name(name, f_options[:model]), get_filter_param(name, f_options[:model]), options)
19
+ text_field_tag(@grid.get_filter_name(name), get_filter_param(name), options)
23
20
  end
24
21
 
25
22
  # Get <tt>select</tt> HTML tag
26
- def select(name, option_tags = nil, options = {})
27
- @grid.filters[name] = option_tags
28
- f_options = filter_options(name, options)
23
+ def select(name, options = {})
24
+ option_tags = @grid.filters[name].collection
29
25
 
30
26
  selected = nil
31
27
  if options.key?(:selected)
32
28
  selected = options.delete(:selected)
33
- @grid.add_filter_param(get_filter_param_name(name, f_options[:model]), selected)
29
+ @grid.add_filter_param(name, selected)
34
30
  end
35
31
 
36
- selected = get_filter_param(name, f_options[:model]) if get_filter_param(name, f_options[:model])
32
+ selected = get_filter_param(name) if get_filter_param(name)
37
33
  opts = options_for_select(option_tags, selected)
38
34
 
39
- select_tag(@grid.get_filter_name(name, f_options[:model]), opts, options)
35
+ select_tag(@grid.get_filter_name(name), opts, options)
40
36
  end
41
37
 
42
38
  # Get <tt>checkbox</tt>
43
39
  def check_box(name, value = '1', checked = false, options = {})
44
40
  checked = true if get_filter_param(name)
45
41
 
46
- f_options = filter_options(name, options)
47
-
48
- check_box_tag(@grid.get_filter_name(name, f_options[:model]), value, checked, options)
42
+ check_box_tag(@grid.get_filter_name(name), value, checked, options)
49
43
  end
50
44
 
51
45
  # Get button for Apply filter changes
@@ -64,28 +58,18 @@ module MightyGrid
64
58
 
65
59
  private
66
60
 
67
- def get_filter_param_name(name, model = nil)
68
- model ? "#{model.table_name}.#{name}" : name.to_s
69
- end
70
-
71
- def get_filter_param(name, model = nil)
72
- filter_name = get_filter_param_name(name, model)
73
- @grid.filter_params.key?(filter_name) ? @grid.filter_params[filter_name] : nil
74
- end
75
-
76
- def get_filter_id(name: nil, model: nil)
77
- @grid.get_filter_name(name, model).parameterize('_')
61
+ def get_filter_param(name)
62
+ if @grid.filter_params.key?(name.to_s)
63
+ @grid.filter_params[name.to_s]
64
+ elsif @grid.filter_params.blank? && @grid.filters[name.to_sym].default.present?
65
+ @grid.filters[name.to_sym].default
66
+ else
67
+ nil
68
+ end
78
69
  end
79
70
 
80
- def filter_options(name, options, with_id = true)
81
- opts = { name: name }
82
- if options.is_a?(Hash) && options.key?(:model)
83
- model = options.delete(:model)
84
- fail MightyGridArgumentError.new('Model of field for filtering should have type ActiveRecord') if model.present? && model.superclass != ActiveRecord::Base
85
- opts.merge!(model: model)
86
- options.merge!(id: get_filter_id(opts)) if with_id
87
- end
88
- opts
71
+ def get_filter_id(name)
72
+ @grid.get_filter_name(name).parameterize('_')
89
73
  end
90
74
  end
91
75
  end
@@ -0,0 +1,27 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class Base
4
+ attr_reader :options, :attribute, :model, :default
5
+
6
+ class_attribute :default_options
7
+ self.default_options = {
8
+ name: nil,
9
+ attribute: nil,
10
+ model: nil,
11
+ default: nil
12
+ }
13
+
14
+
15
+ def initialize(options = {})
16
+ options = options.clone
17
+
18
+ options.assert_valid_keys(self.class.default_options.keys)
19
+
20
+ @options = options.reverse_merge!(self.class.default_options)
21
+ @attribute = @options.delete(:attribute) || @options[:name]
22
+ @model = @options.delete(:model)
23
+ @default = @options.delete(:default)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class BooleanFilter < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,19 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class CustomFilter < Base
4
+ attr_reader :scope
5
+
6
+ def initialize(options = {}, &block)
7
+ self.class.default_options.merge!(scope: nil)
8
+
9
+ super(options)
10
+
11
+ if block_given?
12
+ @scope = block
13
+ else
14
+ @scope = @options.delete(:scope)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class EnumFilter < Base
4
+ attr_reader :collection
5
+
6
+ def initialize(options = {})
7
+ self.class.default_options.merge!(collection: nil)
8
+
9
+ super(options)
10
+
11
+ @collection = @options[:collection]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class StringFilter < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class TextFilter < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,113 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+ require 'mighty_grid/map_type'
3
+
4
+ module MightyGrid
5
+ module Filters
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :Base
9
+ autoload :StringFilter
10
+ autoload :TextFilter
11
+ autoload :EnumFilter
12
+ autoload :BooleanFilter
13
+ autoload :CustomFilter
14
+
15
+ def self.included(base)
16
+ base.class_eval do
17
+ class_attribute :filters
18
+ self.filters = {}
19
+
20
+ extend MapType
21
+ map_type :string, to: MightyGrid::Filters::StringFilter
22
+ map_type :text, to: MightyGrid::Filters::TextFilter
23
+ map_type :enum, to: MightyGrid::Filters::EnumFilter
24
+ map_type :boolean, to: MightyGrid::Filters::BooleanFilter
25
+ map_type :custom, to: MightyGrid::Filters::CustomFilter
26
+ end
27
+
28
+ base.send :extend, ClassMethods
29
+ base.send :include, InstanceMethods
30
+ end
31
+
32
+ module InstanceMethods
33
+ # Get filter parameter name
34
+ def filter_param_name
35
+ 'f'
36
+ end
37
+
38
+ # Get filter parameters
39
+ def filter_params
40
+ @mg_params[filter_param_name.to_sym] || {}
41
+ end
42
+
43
+ # Add param to filters
44
+ def add_filter_param(param, value)
45
+ @mg_params[filter_param_name.to_sym][param] = value unless @mg_params[filter_param_name.to_sym].key?(param)
46
+ end
47
+
48
+ # Get filter name by field
49
+ def get_filter_name(field, model = nil)
50
+ field_name = model.present? ? "#{model.table_name}.#{field}" : field
51
+ "#{name}[#{filter_param_name}][#{field_name}]"
52
+ end
53
+
54
+ # Apply filters
55
+ def apply_filters
56
+ @filters.each_pair do |filter_name, filter|
57
+ next if (filter_params.blank? && filter.default.blank? ||
58
+ filter_params.present? && filter_params[filter_name.to_s].blank?)
59
+
60
+ filter_value =
61
+ if filter_params.present?
62
+ filter_params[filter_name.to_s]
63
+ else
64
+ filter.default.to_s
65
+ end
66
+
67
+ table_name = filter.model.table_name
68
+
69
+ case filter
70
+ when EnumFilter
71
+ @relation = @relation.where(table_name => { filter.attribute => filter_value })
72
+ when BooleanFilter
73
+ value = %w(true 1 t).include?(filter_value) ? true : false
74
+ @relation = @relation.where(table_name => { filter.attribute => value })
75
+ when StringFilter, TextFilter
76
+ @relation = @relation.where("#{table_name}.#{filter.attribute} #{like_operator} ?", "%#{filter_value}%")
77
+ when CustomFilter
78
+ if filter.scope.kind_of?(Proc)
79
+ @relation = filter.scope.call(@relation, filter_value)
80
+ else
81
+ @relation = @relation.send(filter.scope, filter_value)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ module ClassMethods
89
+ def filter(name, type = :string, options = {}, &block)
90
+ name = name.to_sym
91
+
92
+ unless self.mappings.key?(type)
93
+ fail MightyGrid::Exceptions::ArgumentError.new("filter for the specified type isn't supported.")
94
+ end
95
+
96
+ if name.present? && self.filters.key?(name)
97
+ fail MightyGrid::Exceptions::ArgumentError.new("filter with the specified name already exists.")
98
+ else
99
+ options.merge!(name: name)
100
+ options.merge!(model: @klass) unless options.key?(:model)
101
+ self.filters[name] = self.mappings[type].new(options, &block)
102
+ end
103
+ end
104
+
105
+ protected
106
+
107
+ def inherited(child_class)
108
+ super(child_class)
109
+ child_class.filters = self.filters.clone
110
+ end
111
+ end
112
+ end
113
+ end
@@ -55,7 +55,7 @@ module MightyGrid
55
55
  elsif html_or_opts.nil? && block_given?
56
56
  @blank_slate_handler = block
57
57
  else
58
- fail MightyGridArgumentError.new("blank_slate accepts only a string, a block, or :partial => 'path_to_partial' ")
58
+ fail MightyGrid::Exceptions::ArgumentError.new("blank_slate accepts only a string, a block, or :partial => 'path_to_partial' ")
59
59
  end
60
60
  end
61
61