iquest-simple_table 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/iquest-simple_table.gemspec +24 -0
- data/lib/iquest/simple_table.rb +12 -0
- data/lib/iquest/simple_table/table_builder.rb +299 -0
- data/lib/iquest/simple_table/table_helper.rb +20 -0
- data/lib/iquest/simple_table/version.rb +5 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a3df3945062bc5892ec44bcd007001960221e03
|
4
|
+
data.tar.gz: c7bc81df5e5d0fb178355662189da297102a9f57
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e5ea99f0afc341737c72f4fd330b818f44401591bf098b51bdf23a20183e60ed434f18b2ae41719be57626f3acce6aa2f3ef593917f425a592e7ed1f083d360
|
7
|
+
data.tar.gz: 8338e40016be3fa5262cfaf4b709bc5c0a5a49ae5998b9944bcba130cd255de1227a33839c97b5e2271969092c4a23cd4432c0fafa7e54fc64c0dc6a7402ca11
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p353
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Pavel Dusanek
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Iquest::SimpleTable
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'iquest-simple_table'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install iquest-simple_table
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'iquest/simple_table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "iquest-simple_table"
|
8
|
+
spec.version = Iquest::SimpleTable::VERSION
|
9
|
+
spec.authors = ["Pavel Dusanek"]
|
10
|
+
spec.email = ["dusanek@iquest.cz"]
|
11
|
+
spec.description = %q{Simple table helper}
|
12
|
+
spec.summary = %q{Simple table helper, taht supports filtering through Ransack}
|
13
|
+
spec.homepage = "https://github.com/iquest/iquest-simple_table"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'ransack_simple_form', '>= 0.1.0'
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "iquest/simple_table/version"
|
2
|
+
require "iquest/simple_table/table_builder"
|
3
|
+
require "iquest/simple_table/table_helper"
|
4
|
+
|
5
|
+
module Iquest
|
6
|
+
module SimpleTable
|
7
|
+
include TableHelper
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
ActionController::Base.helper Iquest::SimpleTable::TableHelper
|
12
|
+
|
@@ -0,0 +1,299 @@
|
|
1
|
+
require 'ransack_simple_form'
|
2
|
+
|
3
|
+
module Iquest
|
4
|
+
module SimpleTable
|
5
|
+
class TableBuilder
|
6
|
+
attr_reader :parent, :table_id, :columns, :collection, :search_form, :actions, :new_link
|
7
|
+
delegate :capture, :content_tag, :link_to, :paginate, :params, to: :parent
|
8
|
+
delegate :sort_link, :search_form_for, to: :parent
|
9
|
+
delegate :polymorphic_path, :polymorphic_url, :new_polymorphic_path, :new_polymorphic_url, to: :parent
|
10
|
+
delegate :l, :t, to: :parent
|
11
|
+
|
12
|
+
def initialize(parent, collection_or_search, options = {})
|
13
|
+
@parent = parent
|
14
|
+
if collection_or_search.is_a? Ransack::Search
|
15
|
+
@collection = collection_or_search.result
|
16
|
+
@search = collection_or_search
|
17
|
+
@klass = @search.klass
|
18
|
+
elsif collection_or_search.is_a? ActiveRecord::Relation
|
19
|
+
@collection = collection_or_search
|
20
|
+
@klass = @collection.klass
|
21
|
+
else
|
22
|
+
raise TypeError, 'ActiveRecord::Relation or Ransack::Search expected'
|
23
|
+
end
|
24
|
+
apply_pagination
|
25
|
+
#draper
|
26
|
+
@collection = @collection.decorate if @collection.respond_to?(:decorate)
|
27
|
+
@options = options
|
28
|
+
@table_id = "table_#{self.object_id}".parameterize
|
29
|
+
@columns = {}.with_indifferent_access
|
30
|
+
@actions = []
|
31
|
+
@search_input_default_options = {label: false, placeholder: false}.with_indifferent_access
|
32
|
+
end
|
33
|
+
|
34
|
+
def column(*args, &block)
|
35
|
+
attr = args.first
|
36
|
+
options = args.extract_options!
|
37
|
+
search = options.delete(:search)
|
38
|
+
@columns[attr] = options
|
39
|
+
if @search
|
40
|
+
@columns[attr][:label] ||= Ransack::Translate.attribute(attr.to_s.tr('.','_'), context: @search.context)
|
41
|
+
else
|
42
|
+
@columns[attr][:label] ||= @klass.human_attribute_name(attr)
|
43
|
+
end
|
44
|
+
#iniciaizce search options
|
45
|
+
if search.is_a?(Symbol) || search.is_a?(String)
|
46
|
+
@columns[attr][:search] = {search.to_sym => {}}
|
47
|
+
elsif search.is_a? Array
|
48
|
+
@columns[attr][:search] = search.inject({}) {|h, s| h[s.to_sym] = {}; h}
|
49
|
+
elsif search.is_a? Hash
|
50
|
+
@columns[attr][:search] = search
|
51
|
+
end
|
52
|
+
@columns[attr][:formatter] ||= block if block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
def action(*args, &block)
|
56
|
+
action = args.first
|
57
|
+
options = args.extract_options!
|
58
|
+
options[:proc] = block if block_given?
|
59
|
+
@actions << options
|
60
|
+
end
|
61
|
+
|
62
|
+
def new_link(*args, &block)
|
63
|
+
@new_link = block if block_given?
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def search_link(*args, &block)
|
68
|
+
@search_button = block if block_given?
|
69
|
+
end
|
70
|
+
|
71
|
+
def reset_link(*args, &block)
|
72
|
+
@reset_button = block if block_given?
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_s
|
76
|
+
if @search
|
77
|
+
render_table_with_search
|
78
|
+
else
|
79
|
+
render_table_without_search
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
def render_table_without_search
|
85
|
+
table = content_tag :table, class: @options[:html][:class] << %w(table table-hover table-striped) do
|
86
|
+
render_table_header + render_table_body + render_table_footer
|
87
|
+
end
|
88
|
+
|
89
|
+
if @options[:responsive]
|
90
|
+
content_tag :div, class: 'table-responsive' do
|
91
|
+
table
|
92
|
+
end
|
93
|
+
else
|
94
|
+
table
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
include RansackSimpleForm::FormHelper
|
99
|
+
|
100
|
+
def render_table_with_search
|
101
|
+
ransack_simple_form_for @search do |f|
|
102
|
+
@search_form = f
|
103
|
+
render_table_without_search
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def render_table_header
|
108
|
+
content_tag :thead, class: 'header' do
|
109
|
+
render_column_labels + render_search_inputs
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def render_column_labels
|
114
|
+
content_tag :tr, class:'labels' do
|
115
|
+
rendered_columns = columns.map do |col, opts|
|
116
|
+
render_column_label(col)
|
117
|
+
end.join.html_safe
|
118
|
+
render_new_link + rendered_columns
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def render_new_link
|
123
|
+
content_tag :th, class:'new-link' do
|
124
|
+
@new_link || (link_to t('new'), new_polymorphic_url(@klass), class: "btn btn-primary")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def render_search_inputs
|
129
|
+
content_tag :tr, class:'filters' do
|
130
|
+
rendered_columns = columns.map do |col, opts|
|
131
|
+
render_column_search_inputs(col, opts)
|
132
|
+
end.join.html_safe
|
133
|
+
render_buttons + rendered_columns
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def render_column_search_inputs(column, options)
|
138
|
+
content_tag :th, class: '' do
|
139
|
+
if options[:search]
|
140
|
+
options[:search].map do |search, options|
|
141
|
+
render_search_input(search, options)
|
142
|
+
end.join.html_safe
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def render_search_input(search, options = {})
|
148
|
+
input_options = @search_input_default_options.merge(options)
|
149
|
+
search_form.input search.dup, input_options
|
150
|
+
end
|
151
|
+
|
152
|
+
def render_buttons
|
153
|
+
content_tag :th, class:'search-action' do
|
154
|
+
out = content_tag :div, class:'btn-group' do
|
155
|
+
link_to("<i class=\"fa fa-times\" title=\"#{t('search.reset')}\"></i>".html_safe, '?' , class: 'search-reset btn btn-default') +
|
156
|
+
search_form.button( :submit, t('search.search'), class: 'search-button btn btn-default')
|
157
|
+
end
|
158
|
+
# FIXME change link_to url
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def render_table_body
|
163
|
+
content_tag :tbody do
|
164
|
+
collection.map do |item|
|
165
|
+
render_table_row(item)
|
166
|
+
end.join.html_safe
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def render_table_row(item)
|
171
|
+
content_tag :tr do
|
172
|
+
rendered_columns = columns.map do |col|
|
173
|
+
render_value_cell(col, item)
|
174
|
+
end.join.html_safe
|
175
|
+
render_actions(item) + rendered_columns
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
def render_column_label(column)
|
181
|
+
options = @columns[:column]
|
182
|
+
|
183
|
+
content_tag :th do
|
184
|
+
render_label(column)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def render_label(column)
|
189
|
+
attr = column
|
190
|
+
options = @columns[attr]
|
191
|
+
|
192
|
+
label = options[:label] || attr.to_s
|
193
|
+
if @search
|
194
|
+
sort_link(@search, attr, label, method: search_action)
|
195
|
+
else
|
196
|
+
label
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def render_value_cell(*args)
|
201
|
+
col = args.first
|
202
|
+
attr = col.first
|
203
|
+
options = col.second
|
204
|
+
obj = args.second
|
205
|
+
value = get_value(attr, obj)
|
206
|
+
formatter = options[:formatter]
|
207
|
+
content_tag :td do
|
208
|
+
render_value(value, &formatter)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def get_value(attr, obj)
|
213
|
+
if attr.is_a? Symbol
|
214
|
+
obj.try(attr)
|
215
|
+
elsif attr.is_a? String
|
216
|
+
attr.split('.').inject(obj, :try)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def render_value(*args, &block)
|
221
|
+
value = args.first
|
222
|
+
if block_given?
|
223
|
+
block.call(value)
|
224
|
+
else
|
225
|
+
format_value(value)
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
def format_value(value)
|
231
|
+
case value
|
232
|
+
when Time
|
233
|
+
l(value)
|
234
|
+
when Date
|
235
|
+
l(value)
|
236
|
+
else
|
237
|
+
value
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def render_actions(item)
|
242
|
+
content_tag :td do
|
243
|
+
@actions.map do |action|
|
244
|
+
render_action(item, action)
|
245
|
+
end.join.html_safe
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
def render_action(*args)
|
250
|
+
obj = args.first
|
251
|
+
options = args.extract_options!
|
252
|
+
options[:proc].call(obj) if options[:proc].is_a? Proc
|
253
|
+
end
|
254
|
+
|
255
|
+
def render_table_footer
|
256
|
+
content_tag :tfoot, class: '' do
|
257
|
+
content_tag :tr, class: '' do
|
258
|
+
render_pagination + render_footer_actions
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def render_pagination
|
264
|
+
content_tag :td, colspan: column_count do
|
265
|
+
paginate @collection if @collection.respond_to?(:current_page)
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
def render_footer_actions
|
271
|
+
content_tag(:td, '', class: '')
|
272
|
+
end
|
273
|
+
|
274
|
+
private
|
275
|
+
def column_class(col)
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
def column_value(col, obj)
|
280
|
+
col.to_s.split('.').inject(obj, :try)
|
281
|
+
end
|
282
|
+
|
283
|
+
def column_count
|
284
|
+
@columns.count
|
285
|
+
end
|
286
|
+
|
287
|
+
def apply_pagination
|
288
|
+
page = params[:page] || 1
|
289
|
+
per_page = params[:per_page]
|
290
|
+
@collection = @collection.page(page).per(per_page)
|
291
|
+
end
|
292
|
+
|
293
|
+
def search_action
|
294
|
+
:get
|
295
|
+
end
|
296
|
+
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Iquest
|
2
|
+
module SimpleTable
|
3
|
+
module TableHelper
|
4
|
+
def simple_table_for(*args, &block)
|
5
|
+
collection = args.first
|
6
|
+
opts = args.extract_options!
|
7
|
+
|
8
|
+
opts[:html] ||= {}
|
9
|
+
opts[:html][:class] ||= ['filter-table']
|
10
|
+
opts[:html][:class] << ' ' if opts[:html][:class].is_a? String
|
11
|
+
opts[:responsive] ||= true
|
12
|
+
|
13
|
+
builder = SimpleTable::TableBuilder.new self, collection, opts
|
14
|
+
|
15
|
+
yield builder
|
16
|
+
builder.to_s
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iquest-simple_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel Dusanek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ransack_simple_form
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Simple table helper
|
56
|
+
email:
|
57
|
+
- dusanek@iquest.cz
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .ruby-version
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- iquest-simple_table.gemspec
|
69
|
+
- lib/iquest/simple_table.rb
|
70
|
+
- lib/iquest/simple_table/table_builder.rb
|
71
|
+
- lib/iquest/simple_table/table_helper.rb
|
72
|
+
- lib/iquest/simple_table/version.rb
|
73
|
+
homepage: https://github.com/iquest/iquest-simple_table
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.14
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Simple table helper, taht supports filtering through Ransack
|
97
|
+
test_files: []
|