zable 0.0.6 → 0.0.7
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.
- data/README.md +68 -0
- data/app/helpers/zable_helper.rb +3 -44
- data/lib/zable/html.rb +18 -22
- data/lib/zable/sort.rb +4 -1
- data/lib/zable/view.rb +71 -0
- data/lib/zable.rb +1 -0
- metadata +13 -6
- data/README.rdoc +0 -8
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Zable
|
2
|
+
|
3
|
+
Zable lets you easily build sortable and searchable tables of your active record objects.
|
4
|
+
|
5
|
+
## zable view helper
|
6
|
+
|
7
|
+
The zable helper method will render the actual table in your view.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
zable collection, options = {} do
|
11
|
+
# define columns
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
* **collection** - (Array) An array of active_record objects
|
16
|
+
* **options** - (Hash)
|
17
|
+
* **:class** - (String) Html class
|
18
|
+
* **:id** - (String) Html id
|
19
|
+
|
20
|
+
Within the zable block, you can use the `column` method to define the columns of your table.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
column(attribute, options={})
|
24
|
+
column(attribute, options={}, &block)
|
25
|
+
|
26
|
+
# example
|
27
|
+
zable @items do
|
28
|
+
column :column_1
|
29
|
+
column :column_2 {|item| item.to_s}
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
* **attribute** - (Symbol) Name of the attribute on the active_record object for this column. When no block is supplied, this will be the content of this column.
|
34
|
+
* **options** - (Hash)
|
35
|
+
* **:title** - (String or Proc) You can use this to designate a custom header title string, or with a proc, supply completely custom header markup.
|
36
|
+
* **:sort** - (Boolean, default: true) By default, the header title will be a link that can be used to sort its respective column. However by setting this option to false, the title will not be a link.
|
37
|
+
|
38
|
+
|
39
|
+
## Example
|
40
|
+
|
41
|
+
user.rb:
|
42
|
+
```ruby
|
43
|
+
# basic sortable behavior on attribute
|
44
|
+
sortable :name, :email, :created_at
|
45
|
+
|
46
|
+
# sort on a non-attribute
|
47
|
+
scope :sort_age, -> criteria { includes(:profile).order("profile.age #{criteria[:order]}") }
|
48
|
+
```
|
49
|
+
|
50
|
+
users_controller.rb:
|
51
|
+
```ruby
|
52
|
+
def index
|
53
|
+
@users = User.populate(params)
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
index.html.erb:
|
58
|
+
```erb
|
59
|
+
<%=
|
60
|
+
zable @items, :table_class => ["users-table", "shiny-colorful-table"] do
|
61
|
+
column(:name)
|
62
|
+
column(:email)
|
63
|
+
column(:created_at, :title => "Join Date")
|
64
|
+
column(:age) {|user| user.profile.age}
|
65
|
+
column(:edit, :title => "") {|user| link_to "Edit", edit_user_path(user)}
|
66
|
+
end
|
67
|
+
%>
|
68
|
+
```
|
data/app/helpers/zable_helper.rb
CHANGED
@@ -5,36 +5,9 @@ module ZableHelper
|
|
5
5
|
base.send :include, Zable::Html
|
6
6
|
end
|
7
7
|
|
8
|
-
def zable(collection,
|
9
|
-
|
10
|
-
|
11
|
-
html = ''.html_safe #stylesheet_link_tag("zable")
|
12
|
-
html << pagination_element(collection, args.slice(:entry_name, :params)) if args[:paginate]
|
13
|
-
html << zable_element(args, block, collection, klass, args[:params])
|
14
|
-
html << pagination_element(collection, args.slice(:entry_name, :params)) if args[:paginate]
|
15
|
-
html
|
16
|
-
end
|
17
|
-
|
18
|
-
def zable_element(args, block, collection, klass, params)
|
19
|
-
cols = columns(&block)
|
20
|
-
cols.instance_variable_set :@search_params, controller.request.params[:search]
|
21
|
-
cols.instance_variable_set :@_extra_params, params
|
22
|
-
content_tag(:table, tag_args(args)) do
|
23
|
-
table_header(klass, cols) << table_body(collection, cols, args)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def pagination_element(collection, options)
|
28
|
-
content_tag :div, :class => 'brownFilterResultsBox' do
|
29
|
-
page_entries_info(collection, options.slice(:entry_name)) <<
|
30
|
-
will_paginate(collection, :renderer => LinkWithParamsRenderer.new(options[:params] || {}))
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def columns
|
35
|
-
controller.request.instance_variable_set :@zable_columns, []
|
36
|
-
yield
|
37
|
-
controller.request.instance_variable_get :@zable_columns
|
8
|
+
def zable(collection, args={}, &block)
|
9
|
+
table = Zable::View.new(collection, self, args, &block)
|
10
|
+
table.render
|
38
11
|
end
|
39
12
|
|
40
13
|
def sorted_column?(name)
|
@@ -50,18 +23,4 @@ module ZableHelper
|
|
50
23
|
current_sort_order == :desc ? :asc : :desc
|
51
24
|
end
|
52
25
|
|
53
|
-
def column(name, options={}, &block)
|
54
|
-
col = {
|
55
|
-
:name => name,
|
56
|
-
:title => options[:title],
|
57
|
-
:sort => options.has_key?(:sort) ? options[:sort] : true,
|
58
|
-
:block => block,
|
59
|
-
:sorted? => sorted_column?(name),
|
60
|
-
:sort_order => link_sort_order(name)
|
61
|
-
}
|
62
|
-
|
63
|
-
zable_columns = controller.request.instance_variable_get :@zable_columns
|
64
|
-
zable_columns << col
|
65
|
-
end
|
66
|
-
|
67
26
|
end
|
data/lib/zable/html.rb
CHANGED
@@ -2,37 +2,37 @@ module Zable
|
|
2
2
|
module Html
|
3
3
|
|
4
4
|
## Table header methods
|
5
|
-
def table_header(
|
5
|
+
def table_header(columns)
|
6
6
|
content_tag :thead do
|
7
|
-
table_header_row(
|
7
|
+
table_header_row(columns)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def table_header_row(
|
11
|
+
def table_header_row(columns)
|
12
12
|
content_tag :tr do
|
13
|
-
table_header_cells(
|
13
|
+
table_header_cells(columns)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def table_header_cells(
|
17
|
+
def table_header_cells(columns)
|
18
18
|
columns.inject("".html_safe) do |str, attr|
|
19
|
-
str << table_header_cell(
|
19
|
+
str << table_header_cell(attr, columns)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def table_header_cell(
|
24
|
-
content_tag :th, :
|
23
|
+
def table_header_cell(attr, columns)
|
24
|
+
content_tag :th, :data => {:column => idify(attr[:name])} do
|
25
25
|
header_cell_content(attr, columns)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
def header_cell_content(attr, columns)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
if attr[:title] && attr[:title].respond_to?(:call)
|
31
|
+
attr[:title].call
|
32
|
+
else
|
33
|
+
str = link_to_if attr[:sort], header_cell_link_text(attr), header_cell_href(attr)
|
34
|
+
str << header_cell_sort_image(attr)
|
35
|
+
end
|
36
36
|
end
|
37
37
|
|
38
38
|
def current_url
|
@@ -60,10 +60,8 @@ module Zable
|
|
60
60
|
attr[:sort_order] == :desc ? "ascending.gif" : "descending.gif"
|
61
61
|
end
|
62
62
|
|
63
|
-
def header_cell_href(attr
|
64
|
-
|
65
|
-
extra_params = columns.instance_variable_get(:@_extra_params) || {}
|
66
|
-
all_params = [sort_params(attr), search_params(search), extra_params.to_query.html_safe].reject(&:blank?).join("&".html_safe)
|
63
|
+
def header_cell_href(attr)
|
64
|
+
all_params = [sort_params(attr), search_params(@search), (@_extra_params || {}).to_query.html_safe].reject(&:blank?).join("&".html_safe)
|
67
65
|
current_url << "?".html_safe << all_params
|
68
66
|
end
|
69
67
|
|
@@ -144,10 +142,8 @@ module Zable
|
|
144
142
|
val.to_s.demodulize.underscore
|
145
143
|
end
|
146
144
|
|
147
|
-
def tag_args(
|
148
|
-
|
149
|
-
tag_args[:class] = args[:table_class].join(" ".html_safe) if args[:table_class] and !args[:table_class].empty?
|
150
|
-
tag_args
|
145
|
+
def tag_args(options)
|
146
|
+
{class: options[:class], id: options[:id]}
|
151
147
|
end
|
152
148
|
|
153
149
|
end
|
data/lib/zable/sort.rb
CHANGED
@@ -6,7 +6,10 @@ module Zable
|
|
6
6
|
module ClassMethods
|
7
7
|
def inherited(subclass)
|
8
8
|
subclass.class_eval do
|
9
|
-
|
9
|
+
def self.for_sort_params(sort_params)
|
10
|
+
return self unless sort_params
|
11
|
+
inject_sort_scope(sort_params)
|
12
|
+
end
|
10
13
|
end
|
11
14
|
super(subclass)
|
12
15
|
end
|
data/lib/zable/view.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module Zable
|
2
|
+
|
3
|
+
class View
|
4
|
+
include Zable::Html
|
5
|
+
|
6
|
+
attr_reader :columns
|
7
|
+
|
8
|
+
def initialize(collection, template, options={}, &block)
|
9
|
+
@collection = collection
|
10
|
+
|
11
|
+
@template = template # this refers to the view context
|
12
|
+
raise "Must pass in valid view context" unless @template.kind_of? ActionView::Context
|
13
|
+
@search = @template.controller.request.params[:search]
|
14
|
+
@_extra_params = options[:params]
|
15
|
+
|
16
|
+
@options = options
|
17
|
+
@columns = []
|
18
|
+
instance_eval(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def render
|
22
|
+
reset_cycle("zable_cycle")
|
23
|
+
|
24
|
+
html = ''.html_safe #stylesheet_link_tag("zable")
|
25
|
+
html << pagination_element if @options[:paginate]
|
26
|
+
html << zable_element
|
27
|
+
html << pagination_element if @options[:paginate]
|
28
|
+
html
|
29
|
+
end
|
30
|
+
|
31
|
+
def column(name, options={}, &block)
|
32
|
+
col = {
|
33
|
+
:name => name,
|
34
|
+
:title => options[:title],
|
35
|
+
:sort => options.has_key?(:sort) ? options[:sort] : true,
|
36
|
+
:block => block,
|
37
|
+
:sorted? => sorted_column?(name),
|
38
|
+
:sort_order => link_sort_order(name)
|
39
|
+
}
|
40
|
+
|
41
|
+
@columns << col
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def h
|
47
|
+
@template
|
48
|
+
end
|
49
|
+
|
50
|
+
def method_missing(*args, &block)
|
51
|
+
# missing methods will be sent to the view context - these will generally be rails helper methods
|
52
|
+
h.send(*args, &block)
|
53
|
+
end
|
54
|
+
|
55
|
+
def zable_element
|
56
|
+
content_tag(:table, tag_args(@options)) do
|
57
|
+
table_header(@columns) << table_body(@collection, @columns, @options)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def pagination_element
|
62
|
+
content_tag :div, :class => 'brownFilterResultsBox' do
|
63
|
+
page_entries_info(@collection, @options.slice(:entry_name)) <<
|
64
|
+
will_paginate(@collection, :renderer => Zable::WillPaginate::LinkWithParamsRenderer.new(@options[:params] || {}))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/lib/zable.rb
CHANGED
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Derek Croft
|
9
|
+
- Joe Kurleto
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date:
|
13
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: will_paginate
|
16
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
19
|
requirements:
|
19
20
|
- - ! '>='
|
@@ -21,7 +22,12 @@ dependencies:
|
|
21
22
|
version: '0'
|
22
23
|
type: :runtime
|
23
24
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
25
31
|
description: HTML searching, sorting and pagination made dead simple
|
26
32
|
email:
|
27
33
|
executables: []
|
@@ -34,6 +40,7 @@ files:
|
|
34
40
|
- lib/zable/html.rb
|
35
41
|
- lib/zable/search.rb
|
36
42
|
- lib/zable/sort.rb
|
43
|
+
- lib/zable/view.rb
|
37
44
|
- lib/zable/will_paginate.rb
|
38
45
|
- lib/zable/zable_test_helper.rb
|
39
46
|
- lib/zable.rb
|
@@ -41,7 +48,7 @@ files:
|
|
41
48
|
- public/images/common/pagination_bg_dis.gif
|
42
49
|
- MIT-LICENSE
|
43
50
|
- Rakefile
|
44
|
-
- README.
|
51
|
+
- README.md
|
45
52
|
homepage:
|
46
53
|
licenses: []
|
47
54
|
post_install_message:
|
@@ -62,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
69
|
version: '0'
|
63
70
|
requirements: []
|
64
71
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.8.
|
72
|
+
rubygems_version: 1.8.23
|
66
73
|
signing_key:
|
67
74
|
specification_version: 3
|
68
75
|
summary: HTML tables
|