mighty_grid 0.0.4 → 0.0.5
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +1 -2
- data/Gemfile +5 -0
- data/README.md +16 -2
- data/gemfiles/3.2.gemfile +5 -0
- data/gemfiles/4.0.gemfile +5 -0
- data/gemfiles/4.1.gemfile +5 -0
- data/lib/generators/mighty_grid/templates/mighty_grid_config.rb +1 -1
- data/lib/mighty_grid/base.rb +93 -0
- data/lib/mighty_grid/engine.rb +20 -0
- data/lib/mighty_grid/filter_renderer.rb +50 -0
- data/lib/mighty_grid/helpers/mighty_grid_view_helpers.rb +11 -0
- data/lib/mighty_grid/mighty_grid_misc.rb +13 -0
- data/lib/mighty_grid/version.rb +1 -1
- data/lib/mighty_grid.rb +11 -73
- data/spec/lib/generators/config_generator_spec.rb +16 -0
- data/spec/mighty_grid_spec.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f238d7cb8977ec61853b4378529ef0329627acd4
|
4
|
+
data.tar.gz: ef31d746428504597ef2c373085f761ae63ccc3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3016e55015be83daf0615f0de291c798d7b7d37799b3d94d52822eb49b4b353beaf8eb1fa63dad3aaddb2a5ad9cd7cb18f83d1d6d183d72b7b002913721de3dc
|
7
|
+
data.tar.gz: 582d997991d83ae3f96afd2fc66d0693599ab98d09ac87bd970d9efe42356bea73d4405fb428d8269031b1d06bab2977afc986164924e4aa567eee62424f01fa
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -5,6 +5,7 @@ MightyGrid is a flexible grid solution for Rails.
|
|
5
5
|
[](http://badge.fury.io/rb/mighty_grid)
|
6
6
|
[](https://travis-ci.org/jurrick/mighty_grid)
|
7
7
|
[](https://coveralls.io/r/jurrick/mighty_grid)
|
8
|
+
[](http://inch-pages.github.io/github/jurrick/mighty_grid)
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
@@ -22,11 +23,24 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
## Usage
|
24
25
|
|
25
|
-
|
26
|
+
### General configuration options
|
27
|
+
|
28
|
+
You can configure the following default values by overriding these values using <tt>MightyGrid.configure</tt> method.
|
29
|
+
```
|
30
|
+
per_page # 15 by default
|
31
|
+
order_direction # 'asc' by default
|
32
|
+
grid_name # 'grid' by default
|
33
|
+
table_class # '' by default
|
34
|
+
```
|
35
|
+
|
36
|
+
There's a handy generator that generates the default configuration file into config/initializers directory.
|
37
|
+
Run the following generator command, then edit the generated file.
|
38
|
+
|
39
|
+
$ rails g mighty_grid:config
|
26
40
|
|
27
41
|
## Contributing
|
28
42
|
|
29
|
-
1. Fork it ( http://github.com
|
43
|
+
1. Fork it ( http://github.com/jurrick/mighty_grid/fork )
|
30
44
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
45
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
46
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/gemfiles/3.2.gemfile
CHANGED
data/gemfiles/4.0.gemfile
CHANGED
data/gemfiles/4.1.gemfile
CHANGED
@@ -0,0 +1,93 @@
|
|
1
|
+
module MightyGrid
|
2
|
+
|
3
|
+
class Base
|
4
|
+
|
5
|
+
attr_reader :klass, :name, :relation, :options, :mg_params
|
6
|
+
attr_accessor :output_buffer, :filters
|
7
|
+
|
8
|
+
def initialize(klass_or_relation, controller, opts = {}) #:nodoc:
|
9
|
+
@controller = controller
|
10
|
+
|
11
|
+
@filters = {}
|
12
|
+
|
13
|
+
@options = {
|
14
|
+
page: 1,
|
15
|
+
per_page: MightyGrid.config.per_page,
|
16
|
+
name: MightyGrid.config.grid_name
|
17
|
+
}
|
18
|
+
|
19
|
+
opts.assert_valid_keys(@options.keys)
|
20
|
+
@options.merge!(opts)
|
21
|
+
|
22
|
+
@name = @options[:name].to_s
|
23
|
+
|
24
|
+
load_grid_params
|
25
|
+
|
26
|
+
@relation = klass_or_relation
|
27
|
+
|
28
|
+
@klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def read
|
33
|
+
apply_filters
|
34
|
+
@relation = @relation.order(@mg_params[:order] => @mg_params[:order_direction].to_sym) if @mg_params[:order].present? && @mg_params[:order_direction].present?
|
35
|
+
@relation = @relation.page(@mg_params[:page]).per(@mg_params[:per_page])
|
36
|
+
end
|
37
|
+
|
38
|
+
def apply_filters
|
39
|
+
filter_params.each do |filter_name, filter_value|
|
40
|
+
next if filter_value.blank? || !klass.column_names.include?(filter_name)
|
41
|
+
if @filters.has_key?(filter_name.to_sym) && Array === @filters[filter_name.to_sym] ||
|
42
|
+
klass.columns_hash[filter_name].type == :boolean
|
43
|
+
@relation = @relation.where(filter_name => filter_value)
|
44
|
+
elsif [:string, :text].include?(klass.columns_hash[filter_name].type)
|
45
|
+
@relation = @relation.where("\"#{klass.table_name}\".\"#{filter_name}\" #{like_operator} ?", "%#{filter_value}%")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def params
|
51
|
+
@controller.params
|
52
|
+
end
|
53
|
+
|
54
|
+
def load_grid_params
|
55
|
+
@mg_params = {}
|
56
|
+
@mg_params.merge!(@options)
|
57
|
+
if current_grid_params
|
58
|
+
@mg_params.merge!(current_grid_params.symbolize_keys)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_current_grid_param(name)
|
63
|
+
current_grid_params.has_key?(name) ? current_grid_params[name] : nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def filter_params
|
67
|
+
get_current_grid_param(filter_param_name) || {}
|
68
|
+
end
|
69
|
+
|
70
|
+
def filter_param_name; 'f' end
|
71
|
+
|
72
|
+
def get_filter_name(filter_name)
|
73
|
+
"#{name}[#{filter_param_name}][#{filter_name}]"
|
74
|
+
end
|
75
|
+
|
76
|
+
def current_grid_params
|
77
|
+
params[name] || {}
|
78
|
+
end
|
79
|
+
|
80
|
+
def order_direction
|
81
|
+
(current_grid_params.has_key?('order_direction')) ? (['asc', 'desc'] - [current_grid_params['order_direction']]).first : MightyGrid.config.order_direction
|
82
|
+
end
|
83
|
+
|
84
|
+
def like_operator
|
85
|
+
if ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
86
|
+
'ILIKE'
|
87
|
+
else
|
88
|
+
'LIKE'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MightyGrid
|
2
|
+
|
3
|
+
class MightyGridEngine < ::Rails::Engine
|
4
|
+
initializer 'mighty_grid_railtie.configure_rails_initialization' do |app|
|
5
|
+
|
6
|
+
ActiveSupport.on_load :action_controller do
|
7
|
+
ActionController::Base.send(:include, MightyGrid::Controller)
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load :action_view do
|
11
|
+
::ActionView::Base.class_eval { include MightyGrid::GridViewHelper }
|
12
|
+
|
13
|
+
# It is here only until this pull request is pulled: https://github.com/amatsuda/kaminari/pull/267
|
14
|
+
require 'mighty_grid/kaminari_monkey_patching'
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module MightyGrid
|
2
|
+
class FilterRenderer
|
3
|
+
include ActionView::Helpers
|
4
|
+
|
5
|
+
def initialize(grid, view)
|
6
|
+
@grid = grid
|
7
|
+
end
|
8
|
+
|
9
|
+
def label(name, content_or_options = nil, options = nil, &block)
|
10
|
+
label_tag(name, content_or_options, options, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def text_field(name, options={})
|
14
|
+
text_field_tag(@grid.get_filter_name(name), get_filter_param(name), options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def select(name, option_tags=nil, options={})
|
18
|
+
|
19
|
+
@grid.filters[name] = option_tags
|
20
|
+
selected = nil
|
21
|
+
selected = options.delete(:selected) if options.has_key?(:selected)
|
22
|
+
selected = get_filter_param(name) if get_filter_param(name)
|
23
|
+
opts = options_for_select(option_tags, selected)
|
24
|
+
|
25
|
+
select_tag(@grid.get_filter_name(name), opts, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def check_box(name, value = '1', checked = false, options = {})
|
29
|
+
checked = true if get_filter_param(name)
|
30
|
+
|
31
|
+
check_box_tag(@grid.get_filter_name(name), value, checked, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def submit(content = "Apply changes", options = {})
|
35
|
+
options.merge!(type: :submit)
|
36
|
+
content_tag(:button, content, options)
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset(content = "Reset changes", options = {})
|
40
|
+
options.merge!(type: :reset)
|
41
|
+
content_tag(:button, content, options)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def get_filter_param(name)
|
47
|
+
@grid.filter_params.has_key?(name) ? @grid.get_current_grid_param(@grid.filter_param_name)[name] : nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -66,5 +66,16 @@ module MightyGrid
|
|
66
66
|
grid.output_buffer.html_safe
|
67
67
|
end
|
68
68
|
|
69
|
+
def mighty_filter_for(grid, options={}, &block)
|
70
|
+
html_options = options[:html] ||= {}
|
71
|
+
html_options[:method] = options.delete(:method) if options.has_key?(:method)
|
72
|
+
html_options[:method] ||= :get
|
73
|
+
|
74
|
+
filter = FilterRenderer.new(grid, self)
|
75
|
+
|
76
|
+
output = capture(filter, &block)
|
77
|
+
form_tag(options[:url] || {}, html_options){ output }
|
78
|
+
end
|
79
|
+
|
69
80
|
end
|
70
81
|
end
|
data/lib/mighty_grid/version.rb
CHANGED
data/lib/mighty_grid.rb
CHANGED
@@ -1,83 +1,21 @@
|
|
1
|
+
module MightyGrid; end
|
2
|
+
|
3
|
+
require 'action_view'
|
4
|
+
|
1
5
|
require 'mighty_grid/version'
|
2
6
|
require 'mighty_grid/config'
|
7
|
+
require 'mighty_grid/mighty_grid_misc'
|
3
8
|
require 'mighty_grid/mighty_grid_ext'
|
4
9
|
require 'mighty_grid/column'
|
10
|
+
require 'mighty_grid/filter_renderer'
|
5
11
|
require 'mighty_grid/grid_renderer'
|
6
12
|
require 'mighty_grid/helpers/mighty_grid_view_helpers'
|
7
13
|
require 'mighty_grid/mighty_grid_controller'
|
8
14
|
|
9
|
-
|
10
|
-
|
11
|
-
module MightyGrid
|
12
|
-
|
13
|
-
class MightyGridEngine < ::Rails::Engine
|
14
|
-
initializer 'mighty_grid_railtie.configure_rails_initialization' do |app|
|
15
|
-
|
16
|
-
ActiveSupport.on_load :action_controller do
|
17
|
-
ActionController::Base.send(:include, MightyGrid::Controller)
|
18
|
-
end
|
19
|
-
|
20
|
-
ActiveSupport.on_load :action_view do
|
21
|
-
::ActionView::Base.class_eval { include MightyGrid::GridViewHelper }
|
22
|
-
|
23
|
-
# It is here only until this pull request is pulled: https://github.com/amatsuda/kaminari/pull/267
|
24
|
-
require 'mighty_grid/kaminari_monkey_patching'
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class Base
|
31
|
-
|
32
|
-
attr_reader :klass, :name, :relation, :options, :mg_params
|
33
|
-
attr_accessor :output_buffer
|
34
|
-
|
35
|
-
def initialize(klass_or_relation, controller, opts = {}) #:nodoc:
|
36
|
-
@controller = controller
|
15
|
+
# GENERATORS
|
16
|
+
require 'generators/mighty_grid/config_generator'
|
37
17
|
|
38
|
-
|
39
|
-
page: 1,
|
40
|
-
per_page: MightyGrid.config.per_page,
|
41
|
-
name: MightyGrid.config.grid_name
|
42
|
-
}
|
43
|
-
|
44
|
-
opts.assert_valid_keys(@options.keys)
|
45
|
-
@options.merge!(opts)
|
46
|
-
|
47
|
-
@name = @options[:name].to_s
|
48
|
-
|
49
|
-
load_grid_params
|
50
|
-
|
51
|
-
@relation = klass_or_relation
|
52
|
-
|
53
|
-
@klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def read
|
58
|
-
@relation = @relation.order(@mg_params[:order] => @mg_params[:order_direction].to_sym) if @mg_params[:order].present? && @mg_params[:order_direction].present?
|
59
|
-
@relation = @relation.page(@mg_params[:page]).per(@mg_params[:per_page])
|
60
|
-
end
|
61
|
-
|
62
|
-
def params
|
63
|
-
@controller.params
|
64
|
-
end
|
65
|
-
|
66
|
-
def load_grid_params
|
67
|
-
@mg_params = {}
|
68
|
-
@mg_params.merge!(@options)
|
69
|
-
if current_grid_params
|
70
|
-
@mg_params.merge!(current_grid_params.symbolize_keys)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def current_grid_params
|
75
|
-
params[name] || {}
|
76
|
-
end
|
77
|
-
|
78
|
-
def order_direction
|
79
|
-
(current_grid_params.has_key?('order_direction')) ? (['asc', 'desc'] - [current_grid_params['order_direction']]).first : MightyGrid.config.order_direction
|
80
|
-
end
|
18
|
+
require 'kaminari.rb'
|
81
19
|
|
82
|
-
|
83
|
-
|
20
|
+
require 'mighty_grid/engine'
|
21
|
+
require 'mighty_grid/base'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require "generator_spec"
|
4
|
+
|
5
|
+
describe MightyGrid::Generators::ConfigGenerator do
|
6
|
+
destination File.expand_path("../../tmp", __FILE__)
|
7
|
+
|
8
|
+
before(:all) do
|
9
|
+
prepare_destination
|
10
|
+
run_generator
|
11
|
+
end
|
12
|
+
|
13
|
+
it "creates config initializer" do
|
14
|
+
assert_file "config/initializers/mighty_grid_config.rb"
|
15
|
+
end
|
16
|
+
end
|
data/spec/mighty_grid_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -3,12 +3,16 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
3
3
|
|
4
4
|
begin
|
5
5
|
require 'rails'
|
6
|
+
require 'rails/generators'
|
6
7
|
rescue LoadError
|
7
8
|
end
|
8
9
|
|
9
10
|
require 'bundler/setup'
|
10
11
|
Bundler.require
|
11
12
|
|
13
|
+
require 'coveralls'
|
14
|
+
Coveralls.wear!
|
15
|
+
|
12
16
|
RSpec.configure do |config|
|
13
17
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
14
18
|
config.run_all_when_everything_filtered = true
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mighty_grid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jurrick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,16 +108,21 @@ files:
|
|
108
108
|
- lib/generators/mighty_grid/config_generator.rb
|
109
109
|
- lib/generators/mighty_grid/templates/mighty_grid_config.rb
|
110
110
|
- lib/mighty_grid.rb
|
111
|
+
- lib/mighty_grid/base.rb
|
111
112
|
- lib/mighty_grid/column.rb
|
112
113
|
- lib/mighty_grid/config.rb
|
114
|
+
- lib/mighty_grid/engine.rb
|
115
|
+
- lib/mighty_grid/filter_renderer.rb
|
113
116
|
- lib/mighty_grid/grid_renderer.rb
|
114
117
|
- lib/mighty_grid/helpers/mighty_grid_view_helpers.rb
|
115
118
|
- lib/mighty_grid/kaminari_monkey_patching.rb
|
116
119
|
- lib/mighty_grid/mighty_grid_controller.rb
|
117
120
|
- lib/mighty_grid/mighty_grid_ext.rb
|
121
|
+
- lib/mighty_grid/mighty_grid_misc.rb
|
118
122
|
- lib/mighty_grid/version.rb
|
119
123
|
- mighty_grid.gemspec
|
120
124
|
- spec/config_spec.rb
|
125
|
+
- spec/lib/generators/config_generator_spec.rb
|
121
126
|
- spec/mighty_grid_spec.rb
|
122
127
|
- spec/spec_helper.rb
|
123
128
|
- vendor/assets/stylesheets/mighty_grid.css
|
@@ -147,5 +152,6 @@ specification_version: 4
|
|
147
152
|
summary: Flexible grid for Rails
|
148
153
|
test_files:
|
149
154
|
- spec/config_spec.rb
|
155
|
+
- spec/lib/generators/config_generator_spec.rb
|
150
156
|
- spec/mighty_grid_spec.rb
|
151
157
|
- spec/spec_helper.rb
|