data_grid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/.gitignore +18 -0
  2. data/README.md +202 -0
  3. metadata +146 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # DataGrid
2
+
3
+ This gem generates configurable grids for Rails applications using as a data source an array or ORM scope (tested only with ActiveRecord). Some basic features:
4
+
5
+ * Data source can by Array or ActiveRecord::Relation
6
+ * Columns have sorting and filtering options
7
+ * Paging and initial sorting
8
+ * Page or global column summary
9
+ * Export to CSV (customizable file name)
10
+ * Saving state (like page/per page/sorting) in cookies
11
+
12
+ ## Requisites
13
+
14
+ * Rails 3.0 (works on Rails 4.0 with some deprecation warnings).
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'data_grid'
21
+
22
+ Execute:
23
+
24
+ $ bundle install
25
+ $ rails generate date_grid:install
26
+
27
+ If Rails version >= 3.1:
28
+
29
+ Add to app/assets/javascripts/application.js:
30
+
31
+ //= require data_grid/data_grid
32
+ //= require data_grid/grid_calendar/calendar
33
+ //= require data_grid/grid_calendar/calendar-setup
34
+ //= require data_grid/grid_calendar/lang/calendar-en
35
+
36
+
37
+ Add to app/assets/stylesheets/application.css:
38
+
39
+ *= require data_grid/data_grid
40
+ *= require data_grid/grid_calendar/calendar-blue
41
+
42
+ If Rails version < 3.1:
43
+
44
+ You don't need to do anything. Installation script already copied all assets (images, javascripts, stylesheets) to public/ directory.
45
+
46
+
47
+ ## Customization
48
+
49
+ Installation script added initializer "config/initializers/data\_grid.rb". Check this file to modify some configuration settings.
50
+
51
+ To customize grid layout execute:
52
+
53
+ $ rails generate data_grid:copy_view
54
+
55
+ and modify file app/views/data\_grid/\_data\_grid.html.erb
56
+
57
+ ## Usage
58
+
59
+ Definition of data\_grid have to be put into action definition in controller.
60
+
61
+ class UsersController < ApplicationController
62
+ def index
63
+ @data_grid = prepare_grid do |grid|
64
+ grid.add_column :auto
65
+ grid.add_column :name, :title => 'Name', :sortable => true
66
+ grid.add_column :surname, :title => 'Surname', :sortable => true, :filter => :text
67
+ grid.add_column :email, :title => 'E-mail'
68
+ grid.add_column :birth_date, :title => 'Date of birth', :formatter => :date, :filter => :date, :sortable => true
69
+ grid.add_column :admin, :title => 'Admin?', :filter => :boolean
70
+
71
+ grid.initial_sort = "users.created_at DESC"
72
+
73
+ grid.data = User.scoped
74
+ end
75
+ end
76
+ end
77
+
78
+
79
+ and then in view index.html.erb
80
+
81
+ <%= show_grid @data_grid %>
82
+
83
+
84
+ ## DataGrid definition methods/attributes
85
+
86
+ * add\_column(data\_source, column\_params) - add new column
87
+ * initial\_sort - initial sorting
88
+ * export\_enabled = 'csv' - shows icon in grid footer, which clicked generates CSV from grid data
89
+ * export\_filename = 'users.csv' - file name of CSV
90
+ * per\_page - initial per page
91
+ * state\_saver = 'cookies' - page, per\_page and sorting is stored in cookies. When user display page with grid this options are restored from cookies
92
+ * data = ... - provides data to display. It can be an array (User.all) or ActiveRecord::Relation (User.scoped / User.where(:name => 'John').load in Rails4)
93
+ * name - grid name (optional). Has to be provided if more than one data grid is on the page
94
+ * hidden\_row = :method - method with provided name is called on every object. Output of this function is displayed below row which represents object from data source
95
+
96
+
97
+ ## Column attributes
98
+
99
+ * First attribute is column data source. It can be symbol which is model field, or it can be lambda with two parameters. First is each data object, second is view context, which gives possibility of running helpers. For example:
100
+
101
+ `grid.add_column lambda{|obj, h| h.link_to(obj.name, users_path(obj), :class => 'user') }, :title => 'Link to user'`
102
+
103
+ * title - column header title
104
+ * sortable - boolean value
105
+ * sort\_by - symbol or SQL statement which gives base to sort
106
+
107
+ `grid.add_column :name, :title => 'Name', :sortable => true, :sort_by => :surname`
108
+
109
+ * filter - column filtering method
110
+ * filter\_by - symbol or SQL statement which gives base to filter
111
+
112
+ `grid.add_column :name, :title => 'Name', :filter => :text, :filter_by => '(SELECT comments.author FROM comments WHERE comments.user_id = users.id LIMIT 1)'`
113
+
114
+ * style - defines CSS for each cell in this column
115
+ * summary - defines method of summaraizing data on current page in this column. Available methods: :average, :sum, :sum\_price
116
+ * global\_summary - defines method of summaraizing data from all pages in this column
117
+ * formatter - defines method of formatting data from this column. Available methods: :plain\_text, :bold, :money\_pl, :datetime, :date
118
+ * summary\_formatter - defines method of formatting data in summaries
119
+ * hide\_in\_export - boolean value, if column is hidden in export
120
+ * css\_class - defines CSS class for each cell in this column
121
+ * auto\_filter\_hash - provides hash used to override auto filter options
122
+
123
+ `grid.add_column :name, :title => 'Name', :filter => :auto, :auto_filter_hash => {:"John" => "John D."}`
124
+
125
+
126
+ ## Column filter types
127
+
128
+ * :auto - takes all uniq values from column and display in filters section as select. Displayed values can be overwritten by auto\_filter\_hash option in column definition
129
+ * :text - simple input text filter
130
+ * :boolean - All/Yes/No values to select
131
+ * :number - text input to enter a number
132
+ * :range - two inputs to enter range values (from - to)
133
+ * :date - two calendar icons for from and to dates. After clicking on icon javascript calendar shows. Calendar comes from www.dynarch.com
134
+
135
+
136
+ ## Special column types
137
+
138
+ * First option in column definition is a data source. There is a special column defined by putting as a first parameter :auto. It prints column with consecutive numbers on current page.
139
+ * If you put :row\_styler as column title, column will be not shown, but data generated by this column will be using as row CSS styling:
140
+
141
+ `grid.add_column lambda{|x, h| x.price > 50 ? 'color: #f00;' : 'color: #0f0;'}, :title => :row_styler`
142
+
143
+
144
+ ## Adding own summaraizing functions
145
+
146
+ Add file config/initializers/data\_grid\_summaries.rb:
147
+
148
+ module DataGrid
149
+ class Summaries
150
+
151
+ def self.size_of_column(data_as_array)
152
+ data_as_array.size.to_s
153
+ end
154
+
155
+ end
156
+ end
157
+
158
+
159
+ Any method is a summary method. It has one parameter - column data on current page. It has to return a string.
160
+
161
+
162
+ ## Adding own formatters
163
+
164
+ Add file config/initializers/data\_grid\_formatters.rb:
165
+
166
+ module DataGrid
167
+ class ViewHelpers
168
+
169
+ def data_grid_formatter_em(txt)
170
+ "<em>#{txt}</em>".html_safe
171
+ end
172
+
173
+ end
174
+ end
175
+
176
+
177
+ Any method is a formatter method. It has one parameter - cell data. Method name has to begin with `data\_grid\_formatter\_`. When you using this formatter you hasve to skip this beginning. It has to return a string.
178
+
179
+ ## Integration of the data grid with other forms on page
180
+
181
+ DataGrid provides view helper
182
+
183
+ data_grid_dump_as_hidden_fields(data_grid, except = [])
184
+
185
+ which renders all necessary fields from data\_grid as input with type hidden.
186
+ With this simpled method you can have other forms on page which sends data\_grid state.
187
+
188
+ ## TODO
189
+
190
+ * More tests
191
+
192
+ ## Contributing
193
+
194
+ 1. Fork it
195
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
196
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
197
+ 4. Push to the branch (`git push origin my-new-feature`)
198
+ 5. Create new Pull Request
199
+
200
+ ## License
201
+
202
+ This gem is licensed under the MIT License.
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Krzysztof Kempiński
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.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: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fastercsv
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.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: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.6'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.6'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
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'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: This gem generates configurable grids for Rails applications
111
+ email:
112
+ - kkempin@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - README.md
119
+ homepage: https://github.com/kkempin/data_grid
120
+ licenses: []
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ segments:
132
+ - 0
133
+ hash: 3704065723008953533
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: 1.3.6
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.23
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: data grid tool for Rails applications
146
+ test_files: []